File indexing completed on 2025-01-26 04:24:54
0001 #ifndef QUA_ZIPFILE_H 0002 #define QUA_ZIPFILE_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, see 0025 quazip/(un)zip.h files for details, basically it's zlib license. 0026 **/ 0027 0028 #include <QIODevice> 0029 0030 #include "quazip_global.h" 0031 #include "quazip.h" 0032 #include "quazipnewinfo.h" 0033 0034 class QuaZipFilePrivate; 0035 0036 /// A file inside ZIP archive. 0037 /** \class QuaZipFile quazipfile.h <quazip/quazipfile.h> 0038 * This is the most interesting class. Not only it provides C++ 0039 * interface to the ZIP/UNZIP package, but also integrates it with Qt by 0040 * subclassing QIODevice. This makes possible to access files inside ZIP 0041 * archive using QTextStream or QDataStream, for example. Actually, this 0042 * is the main purpose of the whole QuaZIP library. 0043 * 0044 * You can either use existing QuaZip instance to create instance of 0045 * this class or pass ZIP archive file name to this class, in which case 0046 * it will create internal QuaZip object. See constructors' descriptions 0047 * for details. Writing is only possible with the existing instance. 0048 * 0049 * Note that due to the underlying library's limitation it is not 0050 * possible to use multiple QuaZipFile instances to open several files 0051 * in the same archive at the same time. If you need to write to 0052 * multiple files in parallel, then you should write to temporary files 0053 * first, then pack them all at once when you have finished writing. If 0054 * you need to read multiple files inside the same archive in parallel, 0055 * you should extract them all into a temporary directory first. 0056 * 0057 * \section quazipfile-sequential Sequential or random-access? 0058 * 0059 * At the first thought, QuaZipFile has fixed size, the start and the 0060 * end and should be therefore considered random-access device. But 0061 * there is one major obstacle to making it random-access: ZIP/UNZIP API 0062 * does not support seek() operation and the only way to implement it is 0063 * through reopening the file and re-reading to the required position, 0064 * but this is prohibitively slow. 0065 * 0066 * Therefore, QuaZipFile is considered to be a sequential device. This 0067 * has advantage of availability of the ungetChar() operation (QIODevice 0068 * does not implement it properly for non-sequential devices unless they 0069 * support seek()). Disadvantage is a somewhat strange behaviour of the 0070 * size() and pos() functions. This should be kept in mind while using 0071 * this class. 0072 * 0073 **/ 0074 class QUAZIP_EXPORT QuaZipFile: public QIODevice { 0075 friend class QuaZipFilePrivate; 0076 Q_OBJECT 0077 private: 0078 QuaZipFilePrivate *p; 0079 // these are not supported nor implemented 0080 QuaZipFile(const QuaZipFile& that); 0081 QuaZipFile& operator=(const QuaZipFile& that); 0082 protected: 0083 /// Implementation of the QIODevice::readData(). 0084 qint64 readData(char *data, qint64 maxSize); 0085 /// Implementation of the QIODevice::writeData(). 0086 qint64 writeData(const char *data, qint64 maxSize); 0087 public: 0088 /// Constructs a QuaZipFile instance. 0089 /** You should use setZipName() and setFileName() or setZip() before 0090 * trying to call open() on the constructed object. 0091 **/ 0092 QuaZipFile(); 0093 /// Constructs a QuaZipFile instance. 0094 /** \a parent argument specifies this object's parent object. 0095 * 0096 * You should use setZipName() and setFileName() or setZip() before 0097 * trying to call open() on the constructed object. 0098 **/ 0099 QuaZipFile(QObject *parent); 0100 /// Constructs a QuaZipFile instance. 0101 /** \a parent argument specifies this object's parent object and \a 0102 * zipName specifies ZIP archive file name. 0103 * 0104 * You should use setFileName() before trying to call open() on the 0105 * constructed object. 0106 * 0107 * QuaZipFile constructed by this constructor can be used for read 0108 * only access. Use QuaZipFile(QuaZip*,QObject*) for writing. 0109 **/ 0110 QuaZipFile(const QString& zipName, QObject *parent =NULL); 0111 /// Constructs a QuaZipFile instance. 0112 /** \a parent argument specifies this object's parent object, \a 0113 * zipName specifies ZIP archive file name and \a fileName and \a cs 0114 * specify a name of the file to open inside archive. 0115 * 0116 * QuaZipFile constructed by this constructor can be used for read 0117 * only access. Use QuaZipFile(QuaZip*,QObject*) for writing. 0118 * 0119 * \sa QuaZip::setCurrentFile() 0120 **/ 0121 QuaZipFile(const QString& zipName, const QString& fileName, 0122 QuaZip::CaseSensitivity cs =QuaZip::csDefault, QObject *parent =NULL); 0123 /// Constructs a QuaZipFile instance. 0124 /** \a parent argument specifies this object's parent object. 0125 * 0126 * \a zip is the pointer to the existing QuaZip object. This 0127 * QuaZipFile object then can be used to read current file in the 0128 * \a zip or to write to the file inside it. 0129 * 0130 * \warning Using this constructor for reading current file can be 0131 * tricky. Let's take the following example: 0132 * \code 0133 * QuaZip zip("archive.zip"); 0134 * zip.open(QuaZip::mdUnzip); 0135 * zip.setCurrentFile("file-in-archive"); 0136 * QuaZipFile file(&zip); 0137 * file.open(QIODevice::ReadOnly); 0138 * // ok, now we can read from the file 0139 * file.read(somewhere, some); 0140 * zip.setCurrentFile("another-file-in-archive"); // oops... 0141 * QuaZipFile anotherFile(&zip); 0142 * anotherFile.open(QIODevice::ReadOnly); 0143 * anotherFile.read(somewhere, some); // this is still ok... 0144 * file.read(somewhere, some); // and this is NOT 0145 * \endcode 0146 * So, what exactly happens here? When we change current file in the 0147 * \c zip archive, \c file that references it becomes invalid 0148 * (actually, as far as I understand ZIP/UNZIP sources, it becomes 0149 * closed, but QuaZipFile has no means to detect it). 0150 * 0151 * Summary: do not close \c zip object or change its current file as 0152 * long as QuaZipFile is open. Even better - use another constructors 0153 * which create internal QuaZip instances, one per object, and 0154 * therefore do not cause unnecessary trouble. This constructor may 0155 * be useful, though, if you already have a QuaZip instance and do 0156 * not want to access several files at once. Good example: 0157 * \code 0158 * QuaZip zip("archive.zip"); 0159 * zip.open(QuaZip::mdUnzip); 0160 * // first, we need some information about archive itself 0161 * QByteArray comment=zip.getComment(); 0162 * // and now we are going to access files inside it 0163 * QuaZipFile file(&zip); 0164 * for(bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) { 0165 * file.open(QIODevice::ReadOnly); 0166 * // do something cool with file here 0167 * file.close(); // do not forget to close! 0168 * } 0169 * zip.close(); 0170 * \endcode 0171 **/ 0172 QuaZipFile(QuaZip *zip, QObject *parent =NULL); 0173 /// Destroys a QuaZipFile instance. 0174 /** Closes file if open, destructs internal QuaZip object (if it 0175 * exists and \em is internal, of course). 0176 **/ 0177 virtual ~QuaZipFile(); 0178 /// Returns the ZIP archive file name. 0179 /** If this object was created by passing QuaZip pointer to the 0180 * constructor, this function will return that QuaZip's file name 0181 * (or null string if that object does not have file name yet). 0182 * 0183 * Otherwise, returns associated ZIP archive file name or null 0184 * string if there are no name set yet. 0185 * 0186 * \sa setZipName() getFileName() 0187 **/ 0188 QString getZipName()const; 0189 /// Returns a pointer to the associated QuaZip object. 0190 /** Returns \c NULL if there is no associated QuaZip or it is 0191 * internal (so you will not mess with it). 0192 **/ 0193 QuaZip* getZip()const; 0194 /// Returns file name. 0195 /** This function returns file name you passed to this object either 0196 * by using 0197 * QuaZipFile(const QString&,const QString&,QuaZip::CaseSensitivity,QObject*) 0198 * or by calling setFileName(). Real name of the file may differ in 0199 * case if you used case-insensitivity. 0200 * 0201 * Returns null string if there is no file name set yet. This is the 0202 * case when this QuaZipFile operates on the existing QuaZip object 0203 * (constructor QuaZipFile(QuaZip*,QObject*) or setZip() was used). 0204 * 0205 * \sa getActualFileName 0206 **/ 0207 QString getFileName() const; 0208 /// Returns case sensitivity of the file name. 0209 /** This function returns case sensitivity argument you passed to 0210 * this object either by using 0211 * QuaZipFile(const QString&,const QString&,QuaZip::CaseSensitivity,QObject*) 0212 * or by calling setFileName(). 0213 * 0214 * Returns unpredictable value if getFileName() returns null string 0215 * (this is the case when you did not used setFileName() or 0216 * constructor above). 0217 * 0218 * \sa getFileName 0219 **/ 0220 QuaZip::CaseSensitivity getCaseSensitivity() const; 0221 /// Returns the actual file name in the archive. 0222 /** This is \em not a ZIP archive file name, but a name of file inside 0223 * archive. It is not necessary the same name that you have passed 0224 * to the 0225 * QuaZipFile(const QString&,const QString&,QuaZip::CaseSensitivity,QObject*), 0226 * setFileName() or QuaZip::setCurrentFile() - this is the real file 0227 * name inside archive, so it may differ in case if the file name 0228 * search was case-insensitive. 0229 * 0230 * Equivalent to calling getCurrentFileName() on the associated 0231 * QuaZip object. Returns null string if there is no associated 0232 * QuaZip object or if it does not have a current file yet. And this 0233 * is the case if you called setFileName() but did not open the 0234 * file yet. So this is perfectly fine: 0235 * \code 0236 * QuaZipFile file("somezip.zip"); 0237 * file.setFileName("somefile"); 0238 * QString name=file.getName(); // name=="somefile" 0239 * QString actual=file.getActualFileName(); // actual is null string 0240 * file.open(QIODevice::ReadOnly); 0241 * QString actual=file.getActualFileName(); // actual can be "SoMeFiLe" on Windows 0242 * \endcode 0243 * 0244 * \sa getZipName(), getFileName(), QuaZip::CaseSensitivity 0245 **/ 0246 QString getActualFileName()const; 0247 /// Sets the ZIP archive file name. 0248 /** Automatically creates internal QuaZip object and destroys 0249 * previously created internal QuaZip object, if any. 0250 * 0251 * Will do nothing if this file is already open. You must close() it 0252 * first. 0253 **/ 0254 void setZipName(const QString& zipName); 0255 /// Returns \c true if the file was opened in raw mode. 0256 /** If the file is not open, the returned value is undefined. 0257 * 0258 * \sa open(OpenMode,int*,int*,bool,const char*) 0259 **/ 0260 bool isRaw() const; 0261 /// Binds to the existing QuaZip instance. 0262 /** This function destroys internal QuaZip object, if any, and makes 0263 * this QuaZipFile to use current file in the \a zip object for any 0264 * further operations. See QuaZipFile(QuaZip*,QObject*) for the 0265 * possible pitfalls. 0266 * 0267 * Will do nothing if the file is currently open. You must close() 0268 * it first. 0269 **/ 0270 void setZip(QuaZip *zip); 0271 /// Sets the file name. 0272 /** Will do nothing if at least one of the following conditions is 0273 * met: 0274 * - ZIP name has not been set yet (getZipName() returns null 0275 * string). 0276 * - This QuaZipFile is associated with external QuaZip. In this 0277 * case you should call that QuaZip's setCurrentFile() function 0278 * instead! 0279 * - File is already open so setting the name is meaningless. 0280 * 0281 * \sa QuaZip::setCurrentFile 0282 **/ 0283 void setFileName(const QString& fileName, QuaZip::CaseSensitivity cs =QuaZip::csDefault); 0284 /// Opens a file for reading. 0285 /** Returns \c true on success, \c false otherwise. 0286 * Call getZipError() to get error code. 0287 * 0288 * \note Since ZIP/UNZIP API provides buffered reading only, 0289 * QuaZipFile does not support unbuffered reading. So do not pass 0290 * QIODevice::Unbuffered flag in \a mode, or open will fail. 0291 **/ 0292 virtual bool open(OpenMode mode); 0293 /// Opens a file for reading. 0294 /** \overload 0295 * Argument \a password specifies a password to decrypt the file. If 0296 * it is NULL then this function behaves just like open(OpenMode). 0297 **/ 0298 inline bool open(OpenMode mode, const char *password) 0299 {return open(mode, NULL, NULL, false, password);} 0300 /// Opens a file for reading. 0301 /** \overload 0302 * Argument \a password specifies a password to decrypt the file. 0303 * 0304 * An integers pointed by \a method and \a level will receive codes 0305 * of the compression method and level used. See unzip.h. 0306 * 0307 * If raw is \c true then no decompression is performed. 0308 * 0309 * \a method should not be \c NULL. \a level can be \c NULL if you 0310 * don't want to know the compression level. 0311 **/ 0312 bool open(OpenMode mode, int *method, int *level, bool raw, const char *password =NULL); 0313 /// Opens a file for writing. 0314 /** \a info argument specifies information about file. It should at 0315 * least specify a correct file name. Also, it is a good idea to 0316 * specify correct timestamp (by default, current time will be 0317 * used). See QuaZipNewInfo. 0318 * 0319 * The \a password argument specifies the password for crypting. Pass NULL 0320 * if you don't need any crypting. The \a crc argument was supposed 0321 * to be used for crypting too, but then it turned out that it's 0322 * false information, so you need to set it to 0 unless you want to 0323 * use the raw mode (see below). 0324 * 0325 * Arguments \a method and \a level specify compression method and 0326 * level. The only method supported is Z_DEFLATED, but you may also 0327 * specify 0 for no compression. If all of the files in the archive 0328 * use both method 0 and either level 0 is explicitly specified or 0329 * data descriptor writing is disabled with 0330 * QuaZip::setDataDescriptorWritingEnabled(), then the 0331 * resulting archive is supposed to be compatible with the 1.0 ZIP 0332 * format version, should you need that. Except for this, \a level 0333 * has no other effects with method 0. 0334 * 0335 * If \a raw is \c true, no compression is performed. In this case, 0336 * \a crc and uncompressedSize field of the \a info are required. 0337 * 0338 * Arguments \a windowBits, \a memLevel, \a strategy provide zlib 0339 * algorithms tuning. See deflateInit2() in zlib. 0340 **/ 0341 bool open(OpenMode mode, const QuaZipNewInfo& info, 0342 const char *password =NULL, quint32 crc =0, 0343 int method =Z_DEFLATED, int level =Z_DEFAULT_COMPRESSION, bool raw =false, 0344 int windowBits =-MAX_WBITS, int memLevel =DEF_MEM_LEVEL, int strategy =Z_DEFAULT_STRATEGY); 0345 /// Returns \c true, but \ref quazipfile-sequential "beware"! 0346 virtual bool isSequential()const; 0347 /// Returns current position in the file. 0348 /** Implementation of the QIODevice::pos(). When reading, this 0349 * function is a wrapper to the ZIP/UNZIP unztell(), therefore it is 0350 * unable to keep track of the ungetChar() calls (which is 0351 * non-virtual and therefore is dangerous to reimplement). So if you 0352 * are using ungetChar() feature of the QIODevice, this function 0353 * reports incorrect value until you get back characters which you 0354 * ungot. 0355 * 0356 * When writing, pos() returns number of bytes already written 0357 * (uncompressed unless you use raw mode). 0358 * 0359 * \note Although 0360 * \ref quazipfile-sequential "QuaZipFile is a sequential device" 0361 * and therefore pos() should always return zero, it does not, 0362 * because it would be misguiding. Keep this in mind. 0363 * 0364 * This function returns -1 if the file or archive is not open. 0365 * 0366 * Error code returned by getZipError() is not affected by this 0367 * function call. 0368 **/ 0369 virtual qint64 pos()const; 0370 /// Returns \c true if the end of file was reached. 0371 /** This function returns \c false in the case of error. This means 0372 * that you called this function on either not open file, or a file 0373 * in the not open archive or even on a QuaZipFile instance that 0374 * does not even have QuaZip instance associated. Do not do that 0375 * because there is no means to determine whether \c false is 0376 * returned because of error or because end of file was reached. 0377 * Well, on the other side you may interpret \c false return value 0378 * as "there is no file open to check for end of file and there is 0379 * no end of file therefore". 0380 * 0381 * When writing, this function always returns \c true (because you 0382 * are always writing to the end of file). 0383 * 0384 * Error code returned by getZipError() is not affected by this 0385 * function call. 0386 **/ 0387 virtual bool atEnd()const; 0388 /// Returns file size. 0389 /** This function returns csize() if the file is open for reading in 0390 * raw mode, usize() if it is open for reading in normal mode and 0391 * pos() if it is open for writing. 0392 * 0393 * Returns -1 on error, call getZipError() to get error code. 0394 * 0395 * \note This function returns file size despite that 0396 * \ref quazipfile-sequential "QuaZipFile is considered to be sequential device", 0397 * for which size() should return bytesAvailable() instead. But its 0398 * name would be very misguiding otherwise, so just keep in mind 0399 * this inconsistence. 0400 **/ 0401 virtual qint64 size()const; 0402 /// Returns compressed file size. 0403 /** Equivalent to calling getFileInfo() and then getting 0404 * compressedSize field, but more convenient and faster. 0405 * 0406 * File must be open for reading before calling this function. 0407 * 0408 * Returns -1 on error, call getZipError() to get error code. 0409 **/ 0410 qint64 csize()const; 0411 /// Returns uncompressed file size. 0412 /** Equivalent to calling getFileInfo() and then getting 0413 * uncompressedSize field, but more convenient and faster. See 0414 * getFileInfo() for a warning. 0415 * 0416 * File must be open for reading before calling this function. 0417 * 0418 * Returns -1 on error, call getZipError() to get error code. 0419 **/ 0420 qint64 usize()const; 0421 /// Gets information about current file. 0422 /** This function does the same thing as calling 0423 * QuaZip::getCurrentFileInfo() on the associated QuaZip object, 0424 * but you can not call getCurrentFileInfo() if the associated 0425 * QuaZip is internal (because you do not have access to it), while 0426 * you still can call this function in that case. 0427 * 0428 * File must be open for reading before calling this function. 0429 * 0430 * \return \c false in the case of an error. 0431 * 0432 * This function doesn't support zip64, but will still work fine on zip64 0433 * archives if file sizes are below 4 GB, otherwise the values will be set 0434 * as if converted using QuaZipFileInfo64::toQuaZipFileInfo(). 0435 * 0436 * \sa getFileInfo(QuaZipFileInfo64*) 0437 **/ 0438 bool getFileInfo(QuaZipFileInfo *info); 0439 /// Gets information about current file with zip64 support. 0440 /** 0441 * @overload 0442 * 0443 * \sa getFileInfo(QuaZipFileInfo*) 0444 */ 0445 bool getFileInfo(QuaZipFileInfo64 *info); 0446 /// Closes the file. 0447 /** Call getZipError() to determine if the close was successful. 0448 **/ 0449 virtual void close(); 0450 /// Returns the error code returned by the last ZIP/UNZIP API call. 0451 int getZipError() const; 0452 /// Returns the number of bytes available for reading. 0453 virtual qint64 bytesAvailable() const; 0454 }; 0455 0456 #endif