FFSM++  1.1.0
French Forest Sector Model ++
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

Enumerator
NoCompression 
Deflated 
UnknownCompression 

Definition at line 79 of file unzip.h.

enum ErrorCode

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

UnZip::Skip Internal use only. 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.

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  };
Ignores paths and extracts all the files to the same directory.
Definition: unzip.h:75
Extracts paths (default)
Definition: unzip.h:73
enum FileType
Enumerator
File 
Directory 

Definition at line 84 of file unzip.h.

85  {
87  };

Constructor & Destructor Documentation

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 ( )
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
UnzipPrivate * d
Definition: unzip.h:139

Here is the call graph for this function:

Member Function Documentation

QString archiveComment ( ) const

Definition at line 231 of file unzip.cpp.

Referenced by listFiles().

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

Here is the caller graph for this function:

void closeArchive ( )

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

Definition at line 226 of file unzip.cpp.

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

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
UnzipPrivate * d
Definition: unzip.h:139
QList< UnZip::ZipEntry > entryList ( ) const

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

Definition at line 289 of file unzip.cpp.

Referenced by listFiles().

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 }
quint16 compMethod
Definition: zipentry_p.h:64
QDateTime convertDateTime(const unsigned char date[2], const unsigned char time[2]) const
Definition: unzip.cpp:1345
bool isEncrypted() const
Definition: zipentry_p.h:74
unsigned char modTime[2]
Definition: zipentry_p.h:65
QMap< QString, ZipEntryP * > * headers
Definition: unzip_p.h:61
UnzipPrivate * d
Definition: unzip.h:139
QString comment
Definition: zipentry_p.h:70
quint32 szUncomp
Definition: zipentry_p.h:69
unsigned char modDate[2]
Definition: zipentry_p.h:66
quint32 szComp
Definition: zipentry_p.h:68
quint32 crc
Definition: zipentry_p.h:67

Here is the call graph for this function:

Here is the caller graph for this function:

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

Extracts the whole archive to a directory.

Definition at line 325 of file unzip.cpp.

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

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

Here is the caller graph for this function:

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 isEncrypted() const
Definition: zipentry_p.h:74
bool skipAllEncrypted
Definition: unzip_p.h:59
QMap< QString, ZipEntryP * > * headers
Definition: unzip_p.h:61
UnzipPrivate * d
Definition: unzip.h:139
QIODevice * device
Definition: unzip_p.h:63
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:

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.

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

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

Here is the caller graph for this function:

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 }
QMap< QString, ZipEntryP * > * headers
Definition: unzip_p.h:61
UnzipPrivate * d
Definition: unzip.h:139
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:

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 }
QMap< QString, ZipEntryP * > * headers
Definition: unzip_p.h:61
UnzipPrivate * d
Definition: unzip.h:139
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:

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 }
ErrorCode
Definition: unzip.h:48
ErrorCode extractFile(const QString &filename, const QString &dirname, ExtractionOptions options=ExtractPaths)
Definition: unzip.cpp:380

Here is the call graph for this function:

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:48
ErrorCode extractFile(const QString &filename, const QString &dirname, ExtractionOptions options=ExtractPaths)
Definition: unzip.cpp:380

Here is the call graph for this function:

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 }
QMap< QString, ZipEntryP * > * headers
Definition: unzip_p.h:61
UnzipPrivate * d
Definition: unzip.h:139
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.

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

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 }

Here is the caller graph for this function:

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 }
UnzipPrivate * d
Definition: unzip.h:139
QIODevice * device
Definition: unzip_p.h:63
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.

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

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

Here is the caller graph for this function:

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.";
217  return UnZip::InvalidDevice;
218  }
219 
220  return d->openArchive(device);
221 }
UnzipPrivate * d
Definition: unzip.h:139
UnZip::ErrorCode openArchive(QIODevice *device)
Definition: unzip.cpp:500

Here is the call graph for this function:

void setPassword ( const QString &  pwd)

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

Definition at line 463 of file unzip.cpp.

Referenced by decompress(), and listFiles().

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

Here is the caller graph for this function:

Member Data Documentation


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