quazip.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #ifndef QUA_ZIP_H
  2. #define QUA_ZIP_H
  3. /*
  4. Copyright (C) 2005-2011 Sergey A. Tachenov
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or (at
  8. your option) any later version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  12. General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. See COPYING file for the full LGPL text.
  17. Original ZIP package is copyrighted by Gilles Vollant, see
  18. quazip/(un)zip.h files for details, basically it's zlib license.
  19. **/
  20. #include <QString>
  21. #include <QStringList>
  22. #include <QTextCodec>
  23. #include "zip.h"
  24. #include "unzip.h"
  25. #include "quazip_global.h"
  26. #include "quazipfileinfo.h"
  27. // just in case it will be defined in the later versions of the ZIP/UNZIP
  28. #ifndef UNZ_OPENERROR
  29. // define additional error code
  30. #define UNZ_OPENERROR -1000
  31. #endif
  32. class QuaZipPrivate;
  33. /// ZIP archive.
  34. /** \class QuaZip quazip.h <quazip/quazip.h>
  35. * This class implements basic interface to the ZIP archive. It can be
  36. * used to read table contents of the ZIP archive and retreiving
  37. * information about the files inside it.
  38. *
  39. * You can also use this class to open files inside archive by passing
  40. * pointer to the instance of this class to the constructor of the
  41. * QuaZipFile class. But see QuaZipFile::QuaZipFile(QuaZip*, QObject*)
  42. * for the possible pitfalls.
  43. *
  44. * This class is indended to provide interface to the ZIP subpackage of
  45. * the ZIP/UNZIP package as well as to the UNZIP subpackage. But
  46. * currently it supports only UNZIP.
  47. *
  48. * The use of this class is simple - just create instance using
  49. * constructor, then set ZIP archive file name using setFile() function
  50. * (if you did not passed the name to the constructor), then open() and
  51. * then use different functions to work with it! Well, if you are
  52. * paranoid, you may also wish to call close before destructing the
  53. * instance, to check for errors on close.
  54. *
  55. * You may also use getUnzFile() and getZipFile() functions to get the
  56. * ZIP archive handle and use it with ZIP/UNZIP package API directly.
  57. *
  58. * This class supports localized file names inside ZIP archive, but you
  59. * have to set up proper codec with setCodec() function. By default,
  60. * locale codec will be used, which is probably ok for UNIX systems, but
  61. * will almost certainly fail with ZIP archives created in Windows. This
  62. * is because Windows ZIP programs have strange habit of using DOS
  63. * encoding for file names in ZIP archives. For example, ZIP archive
  64. * with cyrillic names created in Windows will have file names in \c
  65. * IBM866 encoding instead of \c WINDOWS-1251. I think that calling one
  66. * function is not much trouble, but for true platform independency it
  67. * would be nice to have some mechanism for file name encoding auto
  68. * detection using locale information. Does anyone know a good way to do
  69. * it?
  70. **/
  71. class QUAZIP_EXPORT QuaZip {
  72. friend class QuaZipPrivate;
  73. public:
  74. /// Useful constants.
  75. enum Constants {
  76. MAX_FILE_NAME_LENGTH=256 /**< Maximum file name length. Taken from
  77. \c UNZ_MAXFILENAMEINZIP constant in
  78. unzip.c. */
  79. };
  80. /// Open mode of the ZIP file.
  81. enum Mode {
  82. mdNotOpen, ///< ZIP file is not open. This is the initial mode.
  83. mdUnzip, ///< ZIP file is open for reading files inside it.
  84. mdCreate, ///< ZIP file was created with open() call.
  85. mdAppend, /**< ZIP file was opened in append mode. This refers to
  86. * \c APPEND_STATUS_CREATEAFTER mode in ZIP/UNZIP package
  87. * and means that zip is appended to some existing file
  88. * what is useful when that file contains
  89. * self-extractor code. This is obviously \em not what
  90. * you whant to use to add files to the existing ZIP
  91. * archive.
  92. **/
  93. mdAdd ///< ZIP file was opened for adding files in the archive.
  94. };
  95. /// Case sensitivity for the file names.
  96. /** This is what you specify when accessing files in the archive.
  97. * Works perfectly fine with any characters thanks to Qt's great
  98. * unicode support. This is different from ZIP/UNZIP API, where
  99. * only US-ASCII characters was supported.
  100. **/
  101. enum CaseSensitivity {
  102. csDefault=0, ///< Default for platform. Case sensitive for UNIX, not for Windows.
  103. csSensitive=1, ///< Case sensitive.
  104. csInsensitive=2 ///< Case insensitive.
  105. };
  106. private:
  107. QuaZipPrivate *p;
  108. // not (and will not be) implemented
  109. QuaZip(const QuaZip& that);
  110. // not (and will not be) implemented
  111. QuaZip& operator=(const QuaZip& that);
  112. public:
  113. /// Constructs QuaZip object.
  114. /** Call setName() before opening constructed object. */
  115. QuaZip();
  116. /// Constructs QuaZip object associated with ZIP file \a zipName.
  117. QuaZip(const QString& zipName);
  118. /// Constructs QuaZip object associated with ZIP file represented by \a ioDevice.
  119. /** The IO device must be seekable, otherwise an error will occur when opening. */
  120. QuaZip(QIODevice *ioDevice);
  121. /// Destroys QuaZip object.
  122. /** Calls close() if necessary. */
  123. ~QuaZip();
  124. /// Opens ZIP file.
  125. /**
  126. * Argument \a mode specifies open mode of the ZIP archive. See Mode
  127. * for details. Note that there is zipOpen2() function in the
  128. * ZIP/UNZIP API which accepts \a globalcomment argument, but it
  129. * does not use it anywhere, so this open() function does not have this
  130. * argument. See setComment() if you need to set global comment.
  131. *
  132. * If the ZIP file is accessed via explicitly set QIODevice, then
  133. * this device is opened in the necessary mode. If the device was
  134. * already opened by some other means, then the behaviour is defined by
  135. * the device implementation, but generally it is not a very good
  136. * idea. For example, QFile will at least issue a warning.
  137. *
  138. * \return \c true if successful, \c false otherwise.
  139. *
  140. * \note ZIP/UNZIP API open calls do not return error code - they
  141. * just return \c NULL indicating an error. But to make things
  142. * easier, quazip.h header defines additional error code \c
  143. * UNZ_ERROROPEN and getZipError() will return it if the open call
  144. * of the ZIP/UNZIP API returns \c NULL.
  145. *
  146. * Argument \a ioApi specifies IO function set for ZIP/UNZIP
  147. * package to use. See unzip.h, zip.h and ioapi.h for details. Note
  148. * that IO API for QuaZip is different from the original package.
  149. * The file path argument was changed to be of type \c voidpf, and
  150. * QuaZip passes a QIODevice pointer there. This QIODevice is either
  151. * set explicitly via setIoDevice() or the QuaZip(QIODevice*)
  152. * constructor, or it is created internally when opening the archive
  153. * by its file name. The default API (qioapi.cpp) just delegates
  154. * everything to the QIODevice API. Not only this allows to use a
  155. * QIODevice instead of file name, but also has a nice side effect
  156. * of raising the file size limit from 2G to 4G.
  157. *
  158. * In short: just forget about the \a ioApi argument and you'll be
  159. * fine.
  160. **/
  161. bool open(Mode mode, zlib_filefunc_def *ioApi =NULL);
  162. /// Closes ZIP file.
  163. /** Call getZipError() to determine if the close was successful. The
  164. * underlying QIODevice is also closed, regardless of whether it was
  165. * set explicitly or not. */
  166. void close();
  167. /// Sets the codec used to encode/decode file names inside archive.
  168. /** This is necessary to access files in the ZIP archive created
  169. * under Windows with non-latin characters in file names. For
  170. * example, file names with cyrillic letters will be in \c IBM866
  171. * encoding.
  172. **/
  173. void setFileNameCodec(QTextCodec *fileNameCodec);
  174. /// Sets the codec used to encode/decode file names inside archive.
  175. /** \overload
  176. * Equivalent to calling setFileNameCodec(QTextCodec::codecForName(codecName));
  177. **/
  178. void setFileNameCodec(const char *fileNameCodecName);
  179. /// Returns the codec used to encode/decode comments inside archive.
  180. QTextCodec* getFileNameCodec() const;
  181. /// Sets the codec used to encode/decode comments inside archive.
  182. /** This codec defaults to locale codec, which is probably ok.
  183. **/
  184. void setCommentCodec(QTextCodec *commentCodec);
  185. /// Sets the codec used to encode/decode comments inside archive.
  186. /** \overload
  187. * Equivalent to calling setCommentCodec(QTextCodec::codecForName(codecName));
  188. **/
  189. void setCommentCodec(const char *commentCodecName);
  190. /// Returns the codec used to encode/decode comments inside archive.
  191. QTextCodec* getCommentCodec() const;
  192. /// Returns the name of the ZIP file.
  193. /** Returns null string if no ZIP file name has been set, for
  194. * example when the QuaZip instance is set up to use a QIODevice
  195. * instead.
  196. * \sa setZipName(), setIoDevice(), getIoDevice()
  197. **/
  198. QString getZipName() const;
  199. /// Sets the name of the ZIP file.
  200. /** Does nothing if the ZIP file is open.
  201. *
  202. * Does not reset error code returned by getZipError().
  203. * \sa setIoDevice(), getIoDevice(), getZipName()
  204. **/
  205. void setZipName(const QString& zipName);
  206. /// Returns the device representing this ZIP file.
  207. /** Returns null string if no device has been set explicitly, for
  208. * example when opening a ZIP file by name.
  209. * \sa setIoDevice(), getZipName(), setZipName()
  210. **/
  211. QIODevice *getIoDevice() const;
  212. /// Sets the device representing the ZIP file.
  213. /** Does nothing if the ZIP file is open.
  214. *
  215. * Does not reset error code returned by getZipError().
  216. * \sa getIoDevice(), getZipName(), setZipName()
  217. **/
  218. void setIoDevice(QIODevice *ioDevice);
  219. /// Returns the mode in which ZIP file was opened.
  220. Mode getMode() const;
  221. /// Returns \c true if ZIP file is open, \c false otherwise.
  222. bool isOpen() const;
  223. /// Returns the error code of the last operation.
  224. /** Returns \c UNZ_OK if the last operation was successful.
  225. *
  226. * Error code resets to \c UNZ_OK every time you call any function
  227. * that accesses something inside ZIP archive, even if it is \c
  228. * const (like getEntriesCount()). open() and close() calls reset
  229. * error code too. See documentation for the specific functions for
  230. * details on error detection.
  231. **/
  232. int getZipError() const;
  233. /// Returns number of the entries in the ZIP central directory.
  234. /** Returns negative error code in the case of error. The same error
  235. * code will be returned by subsequent getZipError() call.
  236. **/
  237. int getEntriesCount() const;
  238. /// Returns global comment in the ZIP file.
  239. QString getComment() const;
  240. /// Sets global comment in the ZIP file.
  241. /** Comment will be written to the archive on close operation.
  242. *
  243. * \sa open()
  244. **/
  245. void setComment(const QString& comment);
  246. /// Sets the current file to the first file in the archive.
  247. /** Returns \c true on success, \c false otherwise. Call
  248. * getZipError() to get the error code.
  249. **/
  250. bool goToFirstFile();
  251. /// Sets the current file to the next file in the archive.
  252. /** Returns \c true on success, \c false otherwise. Call
  253. * getZipError() to determine if there was an error.
  254. *
  255. * Should be used only in QuaZip::mdUnzip mode.
  256. *
  257. * \note If the end of file was reached, getZipError() will return
  258. * \c UNZ_OK instead of \c UNZ_END_OF_LIST_OF_FILE. This is to make
  259. * things like this easier:
  260. * \code
  261. * for(bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) {
  262. * // do something
  263. * }
  264. * if(zip.getZipError()==UNZ_OK) {
  265. * // ok, there was no error
  266. * }
  267. * \endcode
  268. **/
  269. bool goToNextFile();
  270. /// Sets current file by its name.
  271. /** Returns \c true if successful, \c false otherwise. Argument \a
  272. * cs specifies case sensitivity of the file name. Call
  273. * getZipError() in the case of a failure to get error code.
  274. *
  275. * This is not a wrapper to unzLocateFile() function. That is
  276. * because I had to implement locale-specific case-insensitive
  277. * comparison.
  278. *
  279. * Here are the differences from the original implementation:
  280. *
  281. * - If the file was not found, error code is \c UNZ_OK, not \c
  282. * UNZ_END_OF_LIST_OF_FILE (see also goToNextFile()).
  283. * - If this function fails, it unsets the current file rather than
  284. * resetting it back to what it was before the call.
  285. *
  286. * If \a fileName is null string then this function unsets the
  287. * current file and return \c true. Note that you should close the
  288. * file first if it is open! See
  289. * QuaZipFile::QuaZipFile(QuaZip*,QObject*) for the details.
  290. *
  291. * Should be used only in QuaZip::mdUnzip mode.
  292. *
  293. * \sa setFileNameCodec(), CaseSensitivity
  294. **/
  295. bool setCurrentFile(const QString& fileName, CaseSensitivity cs =csDefault);
  296. /// Returns \c true if the current file has been set.
  297. bool hasCurrentFile() const;
  298. /// Retrieves information about the current file.
  299. /** Fills the structure pointed by \a info. Returns \c true on
  300. * success, \c false otherwise. In the latter case structure pointed
  301. * by \a info remains untouched. If there was an error,
  302. * getZipError() returns error code.
  303. *
  304. * Should be used only in QuaZip::mdUnzip mode.
  305. *
  306. * Does nothing and returns \c false in any of the following cases.
  307. * - ZIP is not open;
  308. * - ZIP does not have current file;
  309. * - \a info is \c NULL;
  310. *
  311. * In all these cases getZipError() returns \c UNZ_OK since there
  312. * is no ZIP/UNZIP API call.
  313. **/
  314. bool getCurrentFileInfo(QuaZipFileInfo* info)const;
  315. /// Returns the current file name.
  316. /** Equivalent to calling getCurrentFileInfo() and then getting \c
  317. * name field of the QuaZipFileInfo structure, but faster and more
  318. * convenient.
  319. *
  320. * Should be used only in QuaZip::mdUnzip mode.
  321. **/
  322. QString getCurrentFileName()const;
  323. /// Returns \c unzFile handle.
  324. /** You can use this handle to directly call UNZIP part of the
  325. * ZIP/UNZIP package functions (see unzip.h).
  326. *
  327. * \warning When using the handle returned by this function, please
  328. * keep in mind that QuaZip class is unable to detect any changes
  329. * you make in the ZIP file state (e. g. changing current file, or
  330. * closing the handle). So please do not do anything with this
  331. * handle that is possible to do with the functions of this class.
  332. * Or at least return the handle in the original state before
  333. * calling some another function of this class (including implicit
  334. * destructor calls and calls from the QuaZipFile objects that refer
  335. * to this QuaZip instance!). So if you have changed the current
  336. * file in the ZIP archive - then change it back or you may
  337. * experience some strange behavior or even crashes.
  338. **/
  339. unzFile getUnzFile();
  340. /// Returns \c zipFile handle.
  341. /** You can use this handle to directly call ZIP part of the
  342. * ZIP/UNZIP package functions (see zip.h). Warnings about the
  343. * getUnzFile() function also apply to this function.
  344. **/
  345. zipFile getZipFile();
  346. /// Changes the data descriptor writing mode.
  347. /**
  348. According to the ZIP format specification, a file inside archive
  349. may have a data descriptor immediately following the file
  350. data. This is reflected by a special flag in the local file header
  351. and in the central directory. By default, QuaZIP sets this flag
  352. and writes the data descriptor unless both method and level were
  353. set to 0, in which case it operates in 1.0-compatible mode and
  354. never writes data descriptors.
  355. By setting this flag to false, it is possible to disable data
  356. descriptor writing, thus increasing compatibility with archive
  357. readers that don't understand this feature of the ZIP file format.
  358. Setting this flag affects all the QuaZipFile instances that are
  359. opened after this flag is set.
  360. The data descriptor writing mode is enabled by default.
  361. \param enabled If \c true, enable local descriptor writing,
  362. disable it otherwise.
  363. \sa QuaZipFile::setDataDescriptorWritingEnabled()
  364. */
  365. void setDataDescriptorWritingEnabled(bool enabled);
  366. /// Returns the data descriptor default writing mode.
  367. /**
  368. \sa setDataDescriptorWritingEnabled()
  369. */
  370. bool isDataDescriptorWritingEnabled() const;
  371. /// Returns a list of files inside the archive.
  372. /**
  373. \return A list of file names or an empty list if there
  374. was an error or if the archive is empty (call getZipError() to
  375. figure out which).
  376. \sa getFileInfoList()
  377. */
  378. QStringList getFileNameList() const;
  379. /// Returns information list about all files inside the archive.
  380. /**
  381. \return A list of QuaZipFileInfo objects or an empty list if there
  382. was an error or if the archive is empty (call getZipError() to
  383. figure out which).
  384. \sa getFileNameList()
  385. */
  386. QList<QuaZipFileInfo> getFileInfoList() const;
  387. };
  388. #endif