zip.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. ** Filename: zip.h
  3. ** Last updated [dd/mm/yyyy]: 27/03/2011
  4. **
  5. ** pkzip 2.0 file compression.
  6. **
  7. ** Some of the code has been inspired by other open source projects,
  8. ** (mainly Info-Zip and Gilles Vollant's minizip).
  9. ** Compression and decompression actually uses the zlib library.
  10. **
  11. ** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
  12. **
  13. ** This file is part of the OSDaB project (http://osdab.42cows.org/).
  14. **
  15. ** This file may be distributed and/or modified under the terms of the
  16. ** GNU General Public License version 2 as published by the Free Software
  17. ** Foundation and appearing in the file LICENSE.GPL included in the
  18. ** packaging of this file.
  19. **
  20. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  21. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22. **
  23. ** See the file LICENSE.GPL that came with this software distribution or
  24. ** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
  25. **
  26. **********************************************************************/
  27. #ifndef OSDAB_ZIP__H
  28. #define OSDAB_ZIP__H
  29. #include "zipglobal.h"
  30. #include <QtCore/QMap>
  31. #include <QtCore/QtGlobal>
  32. #include <zlib/zlib.h>
  33. class QIODevice;
  34. class QFile;
  35. class QDir;
  36. class QStringList;
  37. class QString;
  38. OSDAB_BEGIN_NAMESPACE(Zip)
  39. class ZipPrivate;
  40. class OSDAB_ZIP_EXPORT Zip
  41. {
  42. public:
  43. enum ErrorCode
  44. {
  45. Ok,
  46. ZlibInit,
  47. ZlibError,
  48. FileExists,
  49. OpenFailed,
  50. NoOpenArchive,
  51. FileNotFound,
  52. ReadFailed,
  53. WriteFailed,
  54. SeekFailed,
  55. InternalError
  56. };
  57. enum CompressionLevel
  58. {
  59. Store,
  60. Deflate1 = 1, Deflate2, Deflate3, Deflate4,
  61. Deflate5, Deflate6, Deflate7, Deflate8, Deflate9,
  62. AutoCPU, AutoMIME, AutoFull
  63. };
  64. enum CompressionOption
  65. {
  66. /*! Does not preserve absolute paths in the zip file when adding a
  67. file or directory (default) */
  68. RelativePaths = 0x0001,
  69. /*! Preserve absolute paths */
  70. AbsolutePaths = 0x0002,
  71. /*! Do not store paths. All the files are put in the (evtl. user defined)
  72. root of the zip file */
  73. IgnorePaths = 0x0004,
  74. /*! Works only with addDirectory(). Adds the directory's contents,
  75. including subdirectories, but does not add an entry for the root
  76. directory itself. */
  77. IgnoreRoot = 0x0008,
  78. /*! Used only when compressing a directory or multiple files.
  79. If set invalid or unreadable files are simply skipped.
  80. */
  81. SkipBadFiles = 0x0020,
  82. /*! Makes sure a file is never added twice to the same zip archive.
  83. This check is only necessary in certain usage scenarios and given
  84. that it slows down processing you need to enable it explicitly with
  85. this flag.
  86. */
  87. CheckForDuplicates = 0x0040
  88. };
  89. Q_DECLARE_FLAGS(CompressionOptions, CompressionOption)
  90. Zip();
  91. virtual ~Zip();
  92. bool isOpen() const;
  93. void setPassword(const QString& pwd);
  94. void clearPassword();
  95. QString password() const;
  96. ErrorCode createArchive(const QString& file, bool overwrite = true);
  97. ErrorCode createArchive(QIODevice* device);
  98. QString archiveComment() const;
  99. void setArchiveComment(const QString& comment);
  100. ErrorCode addDirectoryContents(const QString& path,
  101. CompressionLevel level = AutoFull);
  102. ErrorCode addDirectoryContents(const QString& path, const QString& root,
  103. CompressionLevel level = AutoFull);
  104. ErrorCode addDirectory(const QString& path,
  105. CompressionLevel level = AutoFull);
  106. ErrorCode addDirectory(const QString& path, const QString& root,
  107. CompressionLevel level = AutoFull);
  108. ErrorCode addDirectory(const QString& path, const QString& root,
  109. CompressionOptions options, CompressionLevel level = AutoFull,
  110. int* addedFiles = 0);
  111. ErrorCode addFile(const QString& path,
  112. CompressionLevel level = AutoFull);
  113. ErrorCode addFile(const QString& path, const QString& root,
  114. CompressionLevel level = AutoFull);
  115. ErrorCode addFile(const QString& path, const QString& root,
  116. CompressionOptions options,
  117. CompressionLevel level = AutoFull);
  118. ErrorCode addFiles(const QStringList& paths,
  119. CompressionLevel level = AutoFull);
  120. ErrorCode addFiles(const QStringList& paths, const QString& root,
  121. CompressionLevel level = AutoFull);
  122. ErrorCode addFiles(const QStringList& paths, const QString& root,
  123. CompressionOptions options,
  124. CompressionLevel level = AutoFull,
  125. int* addedFiles = 0);
  126. ErrorCode closeArchive();
  127. QString formatError(ErrorCode c) const;
  128. private:
  129. ZipPrivate* d;
  130. };
  131. Q_DECLARE_OPERATORS_FOR_FLAGS(Zip::CompressionOptions)
  132. OSDAB_END_NAMESPACE
  133. #endif // OSDAB_ZIP__H