File indexing completed on 2025-01-26 04:24:53

0001 #ifndef QUAZIP_QUAZIODEVICE_H
0002 #define QUAZIP_QUAZIODEVICE_H
0003 
0004 /*
0005 Copyright (C) 2005-2014 Sergey A. Tachenov
0006 
0007 This file is part of QuaZIP.
0008 
0009 QuaZIP is free software: you can redistribute it and/or modify
0010 it under the terms of the GNU Lesser General Public License as published by
0011 the Free Software Foundation, either version 2.1 of the License, or
0012 (at your option) any later version.
0013 
0014 QuaZIP is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU Lesser General Public License for more details.
0018 
0019 You should have received a copy of the GNU Lesser General Public License
0020 along with QuaZIP.  If not, see <http://www.gnu.org/licenses/>.
0021 
0022 See COPYING file for the full LGPL text.
0023 
0024 Original ZIP package is copyrighted by Gilles Vollant and contributors,
0025 see quazip/(un)zip.h files for details. Basically it's the zlib license.
0026 */
0027 
0028 #include <QIODevice>
0029 #include "quazip_global.h"
0030 
0031 #include <zlib.h>
0032 
0033 class QuaZIODevicePrivate;
0034 
0035 /// A class to compress/decompress QIODevice.
0036 /**
0037   This class can be used to compress any data written to QIODevice or
0038   decompress it back. Compressing data sent over a QTcpSocket is a good
0039   example.
0040   */
0041 class QUAZIP_EXPORT QuaZIODevice: public QIODevice {
0042   Q_OBJECT
0043 public:
0044   /// Constructor.
0045   /**
0046     \param io The QIODevice to read/write.
0047     \param parent The parent object, as per QObject logic.
0048     */
0049   QuaZIODevice(QIODevice *io, QObject *parent = NULL);
0050   /// Destructor.
0051   ~QuaZIODevice();
0052   /// Flushes data waiting to be written.
0053   /**
0054     Unfortunately, as QIODevice doesn't support flush() by itself, the
0055     only thing this method does is write the compressed data into the
0056     device using Z_SYNC_FLUSH mode. If you need the compressed data to
0057     actually be flushed from the buffer of the underlying QIODevice, you
0058     need to call its flush() method as well, providing it supports it
0059     (like QTcpSocket does). Example:
0060     \code
0061     QuaZIODevice dev(&sock);
0062     dev.open(QIODevice::Write);
0063     dev.write(yourDataGoesHere);
0064     dev.flush();
0065     sock->flush(); // this actually sends data to network
0066     \endcode
0067 
0068     This may change in the future versions of QuaZIP by implementing an
0069     ugly hack: trying to cast the QIODevice using qobject_cast to known
0070     flush()-supporting subclasses, and calling flush if the resulting
0071     pointer is not zero.
0072     */
0073   virtual bool flush();
0074   /// Opens the device.
0075   /**
0076     \param mode Neither QIODevice::ReadWrite nor QIODevice::Append are
0077     not supported.
0078     */
0079   virtual bool open(QIODevice::OpenMode mode);
0080   /// Closes this device, but not the underlying one.
0081   /**
0082     The underlying QIODevice is not closed in case you want to write
0083     something else to it.
0084     */
0085   virtual void close();
0086   /// Returns the underlying device.
0087   QIODevice *getIoDevice() const;
0088   /// Returns true.
0089   virtual bool isSequential() const;
0090   /// Returns true iff the end of the compressed stream is reached.
0091   virtual bool atEnd() const;
0092   /// Returns the number of the bytes buffered.
0093   virtual qint64 bytesAvailable() const;
0094 protected:
0095   /// Implementation of QIODevice::readData().
0096   virtual qint64 readData(char *data, qint64 maxSize);
0097   /// Implementation of QIODevice::writeData().
0098   virtual qint64 writeData(const char *data, qint64 maxSize);
0099 private:
0100   QuaZIODevicePrivate *d;
0101 };
0102 #endif // QUAZIP_QUAZIODEVICE_H