FFSM++ 1.1.0
French Forest Sector Model ++
Loading...
Searching...
No Matches
UnZip Class Reference

PKZip 2.0 file decompression. Compatibility with later versions is not ensured as they may use unsupported compression algorithms. Versions after 2.7 may have an incompatible header format and thus be completely incompatible. More...

#include <unzip.h>

Collaboration diagram for UnZip:

Classes

struct  ZipEntry
 

Public Types

enum  ErrorCode {
  Ok , ZlibInit , ZlibError , OpenFailed ,
  PartiallyCorrupted , Corrupted , WrongPassword , NoOpenArchive ,
  FileNotFound , ReadFailed , WriteFailed , SeekFailed ,
  CreateDirFailed , InvalidDevice , InvalidArchive , HeaderConsistencyError ,
  Skip , SkipAll
}
 
enum  ExtractionOption { ExtractPaths = 0x0001 , SkipPaths = 0x0002 }
 
enum  CompressionMethod { NoCompression , Deflated , UnknownCompression }
 
enum  FileType { File , Directory }
 

Public Member Functions

 UnZip ()
 
virtual ~UnZip ()
 
bool isOpen () const
 
ErrorCode openArchive (const QString &filename)
 
ErrorCode openArchive (QIODevice *device)
 
void closeArchive ()
 
QString archiveComment () const
 
QString formatError (UnZip::ErrorCode c) const
 
bool contains (const QString &file) const
 
QStringList fileList () const
 
QList< ZipEntryentryList () const
 
ErrorCode extractAll (const QString &dirname, ExtractionOptions options=ExtractPaths)
 
ErrorCode extractAll (const QDir &dir, ExtractionOptions options=ExtractPaths)
 
ErrorCode extractFile (const QString &filename, const QString &dirname, ExtractionOptions options=ExtractPaths)
 
ErrorCode extractFile (const QString &filename, const QDir &dir, ExtractionOptions options=ExtractPaths)
 
ErrorCode extractFile (const QString &filename, QIODevice *device, ExtractionOptions options=ExtractPaths)
 
ErrorCode extractFiles (const QStringList &filenames, const QString &dirname, ExtractionOptions options=ExtractPaths)
 
ErrorCode extractFiles (const QStringList &filenames, const QDir &dir, ExtractionOptions options=ExtractPaths)
 
void setPassword (const QString &pwd)
 

Private Attributes

UnzipPrivated
 

Detailed Description

PKZip 2.0 file decompression. Compatibility with later versions is not ensured as they may use unsupported compression algorithms. Versions after 2.7 may have an incompatible header format and thus be completely incompatible.

Definition at line 45 of file unzip.h.

Member Enumeration Documentation

◆ CompressionMethod

Enumerator
NoCompression 
Deflated 
UnknownCompression 

Definition at line 79 of file unzip.h.

80 {
82 };
@ NoCompression
Definition unzip.h:81
@ Deflated
Definition unzip.h:81
@ UnknownCompression
Definition unzip.h:81

◆ ErrorCode

enum ErrorCode

The result of a decompression operation. \value UnZip::Ok No error occurred. \value UnZip::ZlibInit Failed to init or load the zlib library. \value UnZip::ZlibError The zlib library returned some error. \value UnZip::OpenFailed Unable to create or open a device. \value UnZip::PartiallyCorrupted Corrupted zip archive - some files could be extracted. \value UnZip::Corrupted Corrupted or invalid zip archive. \value UnZip::WrongPassword Unable to decrypt a password protected file. \value UnZip::NoOpenArchive No archive has been opened yet. \value UnZip::FileNotFound Unable to find the requested file in the archive. \value UnZip::ReadFailed Reading of a file failed. \value UnZip::WriteFailed Writing of a file failed. \value UnZip::SeekFailed Seek failed. \value UnZip::CreateDirFailed Could not create a directory. \value UnZip::InvalidDevice A null device has been passed as parameter. \value UnZip::InvalidArchive This is not a valid (or supported) ZIP archive. \value UnZip::HeaderConsistencyError Local header record info does not match with the central directory record info. The archive may be corrupted.

\value UnZip::Skip Internal use only. \value UnZip::SkipAll Internal use only.

Enumerator
Ok 
ZlibInit 
ZlibError 
OpenFailed 
PartiallyCorrupted 
Corrupted 
WrongPassword 
NoOpenArchive 
FileNotFound 
ReadFailed 
WriteFailed 
SeekFailed 
CreateDirFailed 
InvalidDevice 
InvalidArchive 
HeaderConsistencyError 
Skip 
SkipAll 

Definition at line 48 of file unzip.h.

49 {
50 Ok,
66
67 Skip, SkipAll // internal use only
68 };
@ OpenFailed
Definition unzip.h:53
@ ZlibError
Definition unzip.h:52
@ Ok
Definition unzip.h:50
@ NoOpenArchive
Definition unzip.h:57
@ ReadFailed
Definition unzip.h:59
@ SeekFailed
Definition unzip.h:61
@ FileNotFound
Definition unzip.h:58
@ HeaderConsistencyError
Definition unzip.h:65
@ WrongPassword
Definition unzip.h:56
@ InvalidDevice
Definition unzip.h:63
@ Skip
Definition unzip.h:67
@ Corrupted
Definition unzip.h:55
@ SkipAll
Definition unzip.h:67
@ WriteFailed
Definition unzip.h:60
@ ZlibInit
Definition unzip.h:51
@ CreateDirFailed
Definition unzip.h:62
@ PartiallyCorrupted
Definition unzip.h:54
@ InvalidArchive
Definition unzip.h:64

◆ ExtractionOption

Enumerator
ExtractPaths 

Extracts paths (default)

SkipPaths 

Ignores paths and extracts all the files to the same directory.

Definition at line 70 of file unzip.h.

71 {
72 //! Extracts paths (default)
73 ExtractPaths = 0x0001,
74 //! Ignores paths and extracts all the files to the same directory
75 SkipPaths = 0x0002
76 };
@ SkipPaths
Ignores paths and extracts all the files to the same directory.
Definition unzip.h:75
@ ExtractPaths
Extracts paths (default)
Definition unzip.h:73

◆ FileType

enum FileType
Enumerator
File 
Directory 

Definition at line 84 of file unzip.h.

85 {
87 };
@ Directory
Definition unzip.h:86
@ File
Definition unzip.h:86

Constructor & Destructor Documentation

◆ UnZip()

UnZip ( )

Creates a new Zip file decompressor.

Definition at line 165 of file unzip.cpp.

166{
167 d = new UnzipPrivate;
168}
UnzipPrivate * d
Definition unzip.h:139

◆ ~UnZip()

~UnZip ( )
virtual

Closes any open archive and releases used resources.

Definition at line 173 of file unzip.cpp.

174{
175 closeArchive();
176 delete d;
177}
void closeArchive()
Definition unzip.cpp:226
Here is the call graph for this function:

Member Function Documentation

◆ archiveComment()

QString archiveComment ( ) const

Definition at line 231 of file unzip.cpp.

232{
233 if (d->device == 0)
234 return QString();
235 return d->comment;
236}
QIODevice * device
Definition unzip_p.h:63
QString comment
Definition unzip_p.h:82

Referenced by listFiles().

Here is the caller graph for this function:

◆ closeArchive()

void closeArchive ( )

Closes the archive and releases all the used resources (like cached passwords).

Definition at line 226 of file unzip.cpp.

227{
228 d->closeArchive();
229}
void closeArchive()
Definition unzip.cpp:948

Referenced by decompress(), listFiles(), ModelData::loadInput(), UnzipPrivate::openArchive(), and ~UnZip().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ contains()

bool contains ( const QString &  file) const

Returns true if the archive contains a file with the given path and name.

Definition at line 270 of file unzip.cpp.

271{
272 if (d->headers == 0)
273 return false;
274
275 return d->headers->contains(file);
276}
QMap< QString, ZipEntryP * > * headers
Definition unzip_p.h:61

◆ entryList()

QList< UnZip::ZipEntry > entryList ( ) const

Returns information for each (correctly parsed) entry of this archive.

Definition at line 289 of file unzip.cpp.

290{
291 QList<UnZip::ZipEntry> list;
292
293 if (d->headers != 0)
294 {
295 for (QMap<QString,ZipEntryP*>::ConstIterator it = d->headers->constBegin(); it != d->headers->constEnd(); ++it)
296 {
297 const ZipEntryP* entry = it.value();
298 Q_ASSERT(entry != 0);
299
300 ZipEntry z;
301
302 z.filename = it.key();
303 if (!entry->comment.isEmpty())
304 z.comment = entry->comment;
305 z.compressedSize = entry->szComp;
306 z.uncompressedSize = entry->szUncomp;
307 z.crc32 = entry->crc;
308 z.lastModified = d->convertDateTime(entry->modDate, entry->modTime);
309
310 z.compression = entry->compMethod == 0 ? NoCompression : entry->compMethod == 8 ? Deflated : UnknownCompression;
311 z.type = z.filename.endsWith("/") ? Directory : File;
312
313 z.encrypted = entry->isEncrypted();
314
315 list.append(z);
316 }
317 }
318
319 return list;
320}
QDateTime convertDateTime(const unsigned char date[2], const unsigned char time[2]) const
Definition unzip.cpp:1345
QString comment
Definition zipentry_p.h:70
quint32 crc
Definition zipentry_p.h:67
quint32 szComp
Definition zipentry_p.h:68
quint32 szUncomp
Definition zipentry_p.h:69
unsigned char modTime[2]
Definition zipentry_p.h:65
bool isEncrypted() const
Definition zipentry_p.h:74
quint16 compMethod
Definition zipentry_p.h:64
unsigned char modDate[2]
Definition zipentry_p.h:66

Referenced by listFiles().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ extractAll() [1/2]

UnZip::ErrorCode extractAll ( const QDir &  dir,
ExtractionOptions  options = ExtractPaths 
)

Extracts the whole archive to a directory.

Definition at line 333 of file unzip.cpp.

334{
335 // this should only happen if we didn't call openArchive() yet
336 if (d->device == 0)
337 return NoOpenArchive;
338
339 if (d->headers == 0)
340 return Ok;
341
342 bool end = false;
343 for (QMap<QString,ZipEntryP*>::Iterator itr = d->headers->begin(); itr != d->headers->end(); ++itr)
344 {
345 ZipEntryP* entry = itr.value();
346 Q_ASSERT(entry != 0);
347
348 if ((entry->isEncrypted()) && d->skipAllEncrypted)
349 continue;
350
351 switch (d->extractFile(itr.key(), *entry, dir, options))
352 {
353 case Corrupted:
354 qDebug() << "Removing corrupted entry" << itr.key();
355 d->headers->erase(itr++);
356 if (itr == d->headers->end())
357 end = true;
358 break;
359 case CreateDirFailed:
360 break;
361 case Skip:
362 break;
363 case SkipAll:
364 d->skipAllEncrypted = true;
365 break;
366 default:
367 ;
368 }
369
370 if (end)
371 break;
372 }
373
374 return Ok;
375}
bool skipAllEncrypted
Definition unzip_p.h:59
UnZip::ErrorCode extractFile(const QString &path, ZipEntryP &entry, const QDir &dir, UnZip::ExtractionOptions options)
Definition unzip.cpp:972
Here is the call graph for this function:

◆ extractAll() [2/2]

UnZip::ErrorCode extractAll ( const QString &  dirname,
ExtractionOptions  options = ExtractPaths 
)

Extracts the whole archive to a directory.

Definition at line 325 of file unzip.cpp.

326{
327 return extractAll(QDir(dirname), options);
328}
ErrorCode extractAll(const QString &dirname, ExtractionOptions options=ExtractPaths)
Definition unzip.cpp:325

Referenced by decompress(), extractAll(), and ModelData::loadInput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ extractFile() [1/3]

UnZip::ErrorCode extractFile ( const QString &  filename,
const QDir &  dir,
ExtractionOptions  options = ExtractPaths 
)

Extracts a single file to a directory.

Definition at line 388 of file unzip.cpp.

389{
390 QMap<QString,ZipEntryP*>::Iterator itr = d->headers->find(filename);
391 if (itr != d->headers->end())
392 {
393 ZipEntryP* entry = itr.value();
394 Q_ASSERT(entry != 0);
395 return d->extractFile(itr.key(), *entry, dir, options);
396 }
397
398 return FileNotFound;
399}
Here is the call graph for this function:

◆ extractFile() [2/3]

UnZip::ErrorCode extractFile ( const QString &  filename,
const QString &  dirname,
ExtractionOptions  options = ExtractPaths 
)

Extracts a single file to a directory.

Definition at line 380 of file unzip.cpp.

381{
382 return extractFile(filename, QDir(dirname), options);
383}
ErrorCode extractFile(const QString &filename, const QString &dirname, ExtractionOptions options=ExtractPaths)
Definition unzip.cpp:380

Referenced by extractFile(), UnzipPrivate::extractFile(), extractFiles(), and extractFiles().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ extractFile() [3/3]

UnZip::ErrorCode extractFile ( const QString &  filename,
QIODevice *  dev,
ExtractionOptions  options = ExtractPaths 
)

Extracts a single file to a directory.

Definition at line 404 of file unzip.cpp.

405{
406 if (dev == 0)
407 return InvalidDevice;
408
409 QMap<QString,ZipEntryP*>::Iterator itr = d->headers->find(filename);
410 if (itr != d->headers->end()) {
411 ZipEntryP* entry = itr.value();
412 Q_ASSERT(entry != 0);
413 return d->extractFile(itr.key(), *entry, dev, options);
414 }
415
416 return FileNotFound;
417}
Here is the call graph for this function:

◆ extractFiles() [1/2]

UnZip::ErrorCode extractFiles ( const QStringList &  filenames,
const QDir &  dir,
ExtractionOptions  options = ExtractPaths 
)

Extracts a list of files. Stops extraction at the first error (but continues if a file does not exist in the archive).

Definition at line 444 of file unzip.cpp.

445{
446 ErrorCode ec;
447
448 for (QStringList::ConstIterator itr = filenames.constBegin(); itr != filenames.constEnd(); ++itr)
449 {
450 ec = extractFile(*itr, dir, options);
451 if (ec == FileNotFound)
452 continue;
453 if (ec != Ok)
454 return ec;
455 }
456
457 return Ok;
458}
ErrorCode
Definition unzip.h:49
Here is the call graph for this function:

◆ extractFiles() [2/2]

UnZip::ErrorCode extractFiles ( const QStringList &  filenames,
const QString &  dirname,
ExtractionOptions  options = ExtractPaths 
)

Extracts a list of files. Stops extraction at the first error (but continues if a file does not exist in the archive).

Definition at line 423 of file unzip.cpp.

424{
425 QDir dir(dirname);
426 ErrorCode ec;
427
428 for (QStringList::ConstIterator itr = filenames.constBegin(); itr != filenames.constEnd(); ++itr)
429 {
430 ec = extractFile(*itr, dir, options);
431 if (ec == FileNotFound)
432 continue;
433 if (ec != Ok)
434 return ec;
435 }
436
437 return Ok;
438}
Here is the call graph for this function:

◆ fileList()

QStringList fileList ( ) const

Returns complete paths of files and directories in this archive.

Definition at line 281 of file unzip.cpp.

282{
283 return d->headers == 0 ? QStringList() : d->headers->keys();
284}

◆ formatError()

QString formatError ( UnZip::ErrorCode  c) const

Returns a locale translated error string for a given error code.

Definition at line 241 of file unzip.cpp.

242{
243 switch (c)
244 {
245 case Ok: return QCoreApplication::translate("UnZip", "ZIP operation completed successfully."); break;
246 case ZlibInit: return QCoreApplication::translate("UnZip", "Failed to initialize or load zlib library."); break;
247 case ZlibError: return QCoreApplication::translate("UnZip", "zlib library error."); break;
248 case OpenFailed: return QCoreApplication::translate("UnZip", "Unable to create or open file."); break;
249 case PartiallyCorrupted: return QCoreApplication::translate("UnZip", "Partially corrupted archive. Some files might be extracted."); break;
250 case Corrupted: return QCoreApplication::translate("UnZip", "Corrupted archive."); break;
251 case WrongPassword: return QCoreApplication::translate("UnZip", "Wrong password."); break;
252 case NoOpenArchive: return QCoreApplication::translate("UnZip", "No archive has been created yet."); break;
253 case FileNotFound: return QCoreApplication::translate("UnZip", "File or directory does not exist."); break;
254 case ReadFailed: return QCoreApplication::translate("UnZip", "File read error."); break;
255 case WriteFailed: return QCoreApplication::translate("UnZip", "File write error."); break;
256 case SeekFailed: return QCoreApplication::translate("UnZip", "File seek error."); break;
257 case CreateDirFailed: return QCoreApplication::translate("UnZip", "Unable to create a directory."); break;
258 case InvalidDevice: return QCoreApplication::translate("UnZip", "Invalid device."); break;
259 case InvalidArchive: return QCoreApplication::translate("UnZip", "Invalid or incompatible zip archive."); break;
260 case HeaderConsistencyError: return QCoreApplication::translate("UnZip", "Inconsistent headers. Archive might be corrupted."); break;
261 default: ;
262 }
263
264 return QCoreApplication::translate("UnZip", "Unknown error.");
265}

Referenced by decompress(), listFiles(), and ModelData::loadInput().

Here is the caller graph for this function:

◆ isOpen()

bool isOpen ( ) const

Returns true if there is an open archive.

Definition at line 182 of file unzip.cpp.

183{
184 return d->device != 0;
185}

◆ openArchive() [1/2]

UnZip::ErrorCode openArchive ( const QString &  filename)

Opens a zip archive and reads the files list. Closes any previously opened archive.

Definition at line 190 of file unzip.cpp.

191{
192 QFile* file = new QFile(filename);
193
194 if (!file->exists()) {
195 delete file;
196 return UnZip::FileNotFound;
197 }
198
199 if (!file->open(QIODevice::ReadOnly)) {
200 delete file;
201 return UnZip::OpenFailed;
202 }
203
204 return openArchive(file);
205}
ErrorCode openArchive(const QString &filename)
Definition unzip.cpp:190

Referenced by decompress(), listFiles(), ModelData::loadInput(), and openArchive().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ openArchive() [2/2]

UnZip::ErrorCode openArchive ( QIODevice *  device)

Opens a zip archive and reads the entries list. Closes any previously opened archive.

Warning
The class takes ownership of the device so don't delete it!

Definition at line 212 of file unzip.cpp.

213{
214 if (device == 0)
215 {
216 qDebug() << "Invalid device.";
218 }
219
220 return d->openArchive(device);
221}
UnZip::ErrorCode openArchive(QIODevice *device)
Definition unzip.cpp:500
Here is the call graph for this function:

◆ setPassword()

void setPassword ( const QString &  pwd)

Remove/replace this method to add your own password retrieval routine.

Definition at line 463 of file unzip.cpp.

464{
465 d->password = pwd;
466}
QString password
Definition unzip_p.h:57

Referenced by decompress(), and listFiles().

Here is the caller graph for this function:

Member Data Documentation

◆ d


The documentation for this class was generated from the following files: