quaziodevice.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef QUAZIP_QUAZIODEVICE_H
  2. #define QUAZIP_QUAZIODEVICE_H
  3. /*
  4. Copyright (C) 2005-2014 Sergey A. Tachenov
  5. This file is part of QuaZIP.
  6. QuaZIP is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation, either version 2.1 of the License, or
  9. (at your option) any later version.
  10. QuaZIP is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with QuaZIP. If not, see <http://www.gnu.org/licenses/>.
  16. See COPYING file for the full LGPL text.
  17. Original ZIP package is copyrighted by Gilles Vollant and contributors,
  18. see quazip/(un)zip.h files for details. Basically it's the zlib license.
  19. */
  20. #include <QIODevice>
  21. #include "quazip_global.h"
  22. #include <zlib.h>
  23. class QuaZIODevicePrivate;
  24. /// A class to compress/decompress QIODevice.
  25. /**
  26. This class can be used to compress any data written to QIODevice or
  27. decompress it back. Compressing data sent over a QTcpSocket is a good
  28. example.
  29. */
  30. class QUAZIP_EXPORT QuaZIODevice: public QIODevice {
  31. Q_OBJECT
  32. public:
  33. /// Constructor.
  34. /**
  35. \param io The QIODevice to read/write.
  36. \param parent The parent object, as per QObject logic.
  37. */
  38. QuaZIODevice(QIODevice *io, QObject *parent = NULL);
  39. /// Destructor.
  40. ~QuaZIODevice();
  41. /// Flushes data waiting to be written.
  42. /**
  43. Unfortunately, as QIODevice doesn't support flush() by itself, the
  44. only thing this method does is write the compressed data into the
  45. device using Z_SYNC_FLUSH mode. If you need the compressed data to
  46. actually be flushed from the buffer of the underlying QIODevice, you
  47. need to call its flush() method as well, providing it supports it
  48. (like QTcpSocket does). Example:
  49. \code
  50. QuaZIODevice dev(&sock);
  51. dev.open(QIODevice::Write);
  52. dev.write(yourDataGoesHere);
  53. dev.flush();
  54. sock->flush(); // this actually sends data to network
  55. \endcode
  56. This may change in the future versions of QuaZIP by implementing an
  57. ugly hack: trying to cast the QIODevice using qobject_cast to known
  58. flush()-supporting subclasses, and calling flush if the resulting
  59. pointer is not zero.
  60. */
  61. virtual bool flush();
  62. /// Opens the device.
  63. /**
  64. \param mode Neither QIODevice::ReadWrite nor QIODevice::Append are
  65. not supported.
  66. */
  67. virtual bool open(QIODevice::OpenMode mode);
  68. /// Closes this device, but not the underlying one.
  69. /**
  70. The underlying QIODevice is not closed in case you want to write
  71. something else to it.
  72. */
  73. virtual void close();
  74. /// Returns the underlying device.
  75. QIODevice *getIoDevice() const;
  76. /// Returns true.
  77. virtual bool isSequential() const;
  78. /// Returns true iff the end of the compressed stream is reached.
  79. virtual bool atEnd() const;
  80. /// Returns the number of the bytes buffered.
  81. virtual qint64 bytesAvailable() const;
  82. protected:
  83. /// Implementation of QIODevice::readData().
  84. virtual qint64 readData(char *data, qint64 maxSize);
  85. /// Implementation of QIODevice::writeData().
  86. virtual qint64 writeData(const char *data, qint64 maxSize);
  87. private:
  88. QuaZIODevicePrivate *d;
  89. };
  90. #endif // QUAZIP_QUAZIODEVICE_H