FFSM++  1.1.0
French Forest Sector Model ++
Zip Class Reference

Zip file compression. More...

#include <zip.h>

Collaboration diagram for Zip:

Public Types

enum  ErrorCode {
  Ok, ZlibInit, ZlibError, FileExists,
  OpenFailed, NoOpenArchive, FileNotFound, ReadFailed,
  WriteFailed, SeekFailed
}
 
enum  CompressionLevel {
  Store, Deflate1 = 1, Deflate2, Deflate3,
  Deflate4, Deflate5, Deflate6, Deflate7,
  Deflate8, Deflate9, AutoCPU, AutoMIME,
  AutoFull
}
 
enum  CompressionOption { RelativePaths = 0x0001, AbsolutePaths = 0x0002, IgnorePaths = 0x0004 }
 

Public Member Functions

 Zip ()
 
virtual ~Zip ()
 
bool isOpen () const
 
void setPassword (const QString &pwd)
 
void clearPassword ()
 Convenience method, clears the current password. More...
 
QString password () const
 Returns the currently used password. More...
 
ErrorCode createArchive (const QString &file, bool overwrite=true)
 
ErrorCode createArchive (QIODevice *device)
 
QString archiveComment () const
 
void setArchiveComment (const QString &comment)
 
ErrorCode addDirectoryContents (const QString &path, CompressionLevel level=AutoFull)
 
ErrorCode addDirectoryContents (const QString &path, const QString &root, CompressionLevel level=AutoFull)
 
ErrorCode addDirectory (const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
 
ErrorCode addDirectory (const QString &path, const QString &root, CompressionLevel level=AutoFull)
 
ErrorCode addDirectory (const QString &path, const QString &root, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
 
ErrorCode closeArchive ()
 
QString formatError (ErrorCode c) const
 

Private Attributes

ZipPrivated
 

Detailed Description

Zip file compression.

Some quick usage examples.

Suppose you have this directory structure:

/root/dir1/
/root/dir1/file1.1
/root/dir1/file1.2
/root/dir1/dir1.1/
/root/dir1/dir1.2/file1.2.1

EXAMPLE 1:
myZipInstance.addDirectory("/root/dir1");

RESULT:
Beheaves like any common zip software and creates a zip file with this structure:

dir1/
dir1/file1.1
dir1/file1.2
dir1/dir1.1/
dir1/dir1.2/file1.2.1

EXAMPLE 2:
myZipInstance.addDirectory("/root/dir1", "myRoot/myFolder");

RESULT:
Adds a custom root to the paths and creates a zip file with this structure:

myRoot/myFolder/dir1/
myRoot/myFolder/dir1/file1.1
myRoot/myFolder/dir1/file1.2
myRoot/myFolder/dir1/dir1.1/
myRoot/myFolder/dir1/dir1.2/file1.2.1

EXAMPLE 3:
myZipInstance.addDirectory("/root/dir1", Zip::AbsolutePaths);

NOTE:
Same as calling addDirectory(SOME_PATH, PARENT_PATH_of_SOME_PATH).

RESULT:
Preserves absolute paths and creates a zip file with this structure:

/root/dir1/
/root/dir1/file1.1
/root/dir1/file1.2
/root/dir1/dir1.1/
/root/dir1/dir1.2/file1.2.1

EXAMPLE 4:
myZipInstance.setPassword("hellopass");
myZipInstance.addDirectory("/root/dir1", "/");

RESULT:
Adds and encrypts the files in /root/dir1, creating the following zip structure:

/dir1/
/dir1/file1.1
/dir1/file1.2
/dir1/dir1.1/
/dir1/dir1.2/file1.2.1

Definition at line 45 of file zip.h.

Member Enumeration Documentation

Returns the result of a decompression operation. Zip::Store No compression. Zip::Deflate1 Deflate compression level 1(lowest compression). Zip::Deflate1 Deflate compression level 2. Zip::Deflate1 Deflate compression level 3. Zip::Deflate1 Deflate compression level 4. Zip::Deflate1 Deflate compression level 5. Zip::Deflate1 Deflate compression level 6. Zip::Deflate1 Deflate compression level 7. Zip::Deflate1 Deflate compression level 8. Zip::Deflate1 Deflate compression level 9 (maximum compression). Zip::AutoCPU Adapt compression level to CPU speed (faster CPU => better compression). Zip::AutoMIME Adapt compression level to MIME type of the file being compressed. Zip::AutoFull Use both CPU and MIME type detection.

Enumerator
Store 
Deflate1 
Deflate2 
Deflate3 
Deflate4 
Deflate5 
Deflate6 
Deflate7 
Deflate8 
Deflate9 
AutoCPU 
AutoMIME 
AutoFull 

Definition at line 62 of file zip.h.

Enumerator
RelativePaths 

Does not preserve absolute paths in the zip file when adding a file/directory (default)

AbsolutePaths 

Preserve absolute paths.

IgnorePaths 

Do not store paths. All the files are put in the (evtl. user defined) root of the zip file.

Definition at line 70 of file zip.h.

71  {
72  //! Does not preserve absolute paths in the zip file when adding a file/directory (default)
73  RelativePaths = 0x0001,
74  //! Preserve absolute paths
75  AbsolutePaths = 0x0002,
76  //! Do not store paths. All the files are put in the (evtl. user defined) root of the zip file
77  IgnorePaths = 0x0004
78  };
Do not store paths. All the files are put in the (evtl. user defined) root of the zip file...
Definition: zip.h:77
Preserve absolute paths.
Definition: zip.h:75
Does not preserve absolute paths in the zip file when adding a file/directory (default) ...
Definition: zip.h:73
enum ErrorCode

The result of a compression operation. Zip::Ok No error occurred. Zip::ZlibInit Failed to init or load the zlib library. Zip::ZlibError The zlib library returned some error. Zip::FileExists The file already exists and will not be overwritten. Zip::OpenFailed Unable to create or open a device. Zip::NoOpenArchive CreateArchive() has not been called yet. Zip::FileNotFound File or directory does not exist. Zip::ReadFailed Reading of a file failed. Zip::WriteFailed Writing of a file failed. Zip::SeekFailed Seek failed.

Enumerator
Ok 
ZlibInit 
ZlibError 
FileExists 
OpenFailed 
NoOpenArchive 
FileNotFound 
ReadFailed 
WriteFailed 
SeekFailed 

Definition at line 48 of file zip.h.

49  {
50  Ok,
51  ZlibInit,
52  ZlibError,
53  FileExists,
54  OpenFailed,
57  ReadFailed,
60  };
Definition: zip.h:50

Constructor & Destructor Documentation

Zip ( )

Creates a new Zip file compressor.

Definition at line 218 of file zip.cpp.

219 {
220  d = new ZipPrivate;
221 }
ZipPrivate * d
Definition: zip.h:108
~Zip ( )
virtual

Closes any open archive and releases used resources.

Definition at line 226 of file zip.cpp.

227 {
228  closeArchive();
229  delete d;
230 }
ErrorCode closeArchive()
Definition: zip.cpp:465
ZipPrivate * d
Definition: zip.h:108

Here is the call graph for this function:

Member Function Documentation

Zip::ErrorCode addDirectory ( const QString &  path,
CompressionOptions  options = RelativePaths,
CompressionLevel  level = AutoFull 
)

Convenience method, same as calling Zip::addDirectory(const QString&,const QString&,CompressionLevel) with an empty root parameter (or with the parent directory of path if the AbsolutePaths options is set).

The ExtractionOptions are checked in the order they are defined in the zip.h heaser file. This means that the last one overwrites the previous one (if some conflict occurs), i.e. Zip::IgnorePaths | Zip::AbsolutePaths would be interpreted as Zip::IgnorePaths.

Definition at line 333 of file zip.cpp.

Referenced by addDirectory(), addDirectoryContents(), and compress().

334 {
335  return addDirectory(path, QString(), options, level);
336 }
ErrorCode addDirectory(const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
Definition: zip.cpp:333

Here is the caller graph for this function:

Zip::ErrorCode addDirectory ( const QString &  path,
const QString &  root,
CompressionLevel  level = AutoFull 
)

Convenience method, same as calling Zip::addDirectory(const QString&,const QString&,CompressionOptions,CompressionLevel) with the Zip::RelativePaths flag as compression option.

Definition at line 342 of file zip.cpp.

343 {
344  return addDirectory(path, root, Zip::RelativePaths, level);
345 }
ErrorCode addDirectory(const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
Definition: zip.cpp:333
Does not preserve absolute paths in the zip file when adding a file/directory (default) ...
Definition: zip.h:73

Here is the call graph for this function:

Zip::ErrorCode addDirectory ( const QString &  path,
const QString &  root,
CompressionOptions  options = RelativePaths,
CompressionLevel  level = AutoFull 
)

Recursively adds files contained in dir to the archive, using root as name for the root folder. Stops adding files if some error occurs.

The ExtractionOptions are checked in the order they are defined in the zip.h heaser file. This means that the last one overwrites the previous one (if some conflict occurs), i.e. Zip::IgnorePaths | Zip::AbsolutePaths would be interpreted as Zip::IgnorePaths.

The root parameter is ignored with the Zip::IgnorePaths parameter and used as path prefix (a trailing / is always added as directory separator!) otherwise (even with Zip::AbsolutePaths set!).

Definition at line 376 of file zip.cpp.

377 {
378  // qDebug() << QString("addDir(path=%1, root=%2)").arg(path, root);
379 
380  // Bad boy didn't call createArchive() yet :)
381  if (d->device == 0)
382  return Zip::NoOpenArchive;
383 
384  QDir dir(path);
385  if (!dir.exists())
386  return Zip::FileNotFound;
387 
388  // Remove any trailing separator
389  QString actualRoot = root.trimmed();
390 
391  // Preserve Unix root
392  if (actualRoot != "/")
393  {
394  while (actualRoot.endsWith("/") || actualRoot.endsWith("\\"))
395  actualRoot.truncate(actualRoot.length() - 1);
396  }
397 
398  // QDir::cleanPath() fixes some issues with QDir::dirName()
399  QFileInfo current(QDir::cleanPath(path));
400 
401  if (!actualRoot.isEmpty() && actualRoot != "/")
402  actualRoot.append("/");
403 
404  /* This part is quite confusing and needs some test or check */
405  /* An attempt to compress the / root directory evtl. using a root prefix should be a good test */
406  if (options.testFlag(AbsolutePaths) && !options.testFlag(IgnorePaths))
407  {
408  QString absolutePath = d->extractRoot(path);
409  if (!absolutePath.isEmpty() && absolutePath != "/")
410  absolutePath.append("/");
411  actualRoot.append(absolutePath);
412  }
413 
414  if (!options.testFlag(IgnorePaths))
415  {
416  actualRoot = actualRoot.append(QDir(current.absoluteFilePath()).dirName());
417  actualRoot.append("/");
418  }
419 
420  // actualRoot now contains the path of the file relative to the zip archive
421  // with a trailing /
422 
423  QFileInfoList list = dir.entryInfoList(
424  QDir::Files |
425  QDir::Dirs |
426  QDir::NoDotAndDotDot |
427  QDir::NoSymLinks);
428 
429  ErrorCode ec = Zip::Ok;
430  bool filesAdded = false;
431 
432  CompressionOptions recursionOptions;
433  if (options.testFlag(IgnorePaths))
434  recursionOptions |= IgnorePaths;
435  else recursionOptions |= RelativePaths;
436 
437  for (int i = 0; i < list.size() && ec == Zip::Ok; ++i)
438  {
439  QFileInfo info = list.at(i);
440 
441  if (info.isDir())
442  {
443  // Recursion :)
444  ec = addDirectory(info.absoluteFilePath(), actualRoot, recursionOptions, level);
445  }
446  else
447  {
448  ec = d->createEntry(info, actualRoot, level);
449  filesAdded = true;
450  }
451  }
452 
453 
454  // We need an explicit record for this dir
455  // Non-empty directories don't need it because they have a path component in the filename
456  if (!filesAdded && !options.testFlag(IgnorePaths))
457  ec = d->createEntry(current, actualRoot, level);
458 
459  return ec;
460 }
Definition: zip.h:50
Zip::ErrorCode createEntry(const QFileInfo &file, const QString &root, Zip::CompressionLevel level)
Definition: zip.cpp:541
Do not store paths. All the files are put in the (evtl. user defined) root of the zip file...
Definition: zip.h:77
QIODevice * device
Definition: zip_p.h:62
ZipPrivate * d
Definition: zip.h:108
ErrorCode
Definition: zip.h:48
QString extractRoot(const QString &p)
Definition: zip.cpp:1213
ErrorCode addDirectory(const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
Definition: zip.cpp:333
Preserve absolute paths.
Definition: zip.h:75
Does not preserve absolute paths in the zip file when adding a file/directory (default) ...
Definition: zip.h:73

Here is the call graph for this function:

Zip::ErrorCode addDirectoryContents ( const QString &  path,
CompressionLevel  level = AutoFull 
)

Convenience method, same as calling Zip::addDirectory(const QString&,const QString&,CompressionOptions,CompressionLevel) with the Zip::IgnorePaths flag as compression option and an empty root parameter.

Definition at line 351 of file zip.cpp.

352 {
353  return addDirectory(path, QString(), IgnorePaths, level);
354 }
Do not store paths. All the files are put in the (evtl. user defined) root of the zip file...
Definition: zip.h:77
ErrorCode addDirectory(const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
Definition: zip.cpp:333

Here is the call graph for this function:

Zip::ErrorCode addDirectoryContents ( const QString &  path,
const QString &  root,
CompressionLevel  level = AutoFull 
)

Convenience method, same as calling Zip::addDirectory(const QString&,const QString&,CompressionOptions,CompressionLevel) with the Zip::IgnorePaths flag as compression option.

Definition at line 360 of file zip.cpp.

361 {
362  return addDirectory(path, root, IgnorePaths, level);
363 }
Do not store paths. All the files are put in the (evtl. user defined) root of the zip file...
Definition: zip.h:77
ErrorCode addDirectory(const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
Definition: zip.cpp:333

Here is the call graph for this function:

QString archiveComment ( ) const

Returns the current archive comment.

Definition at line 308 of file zip.cpp.

309 {
310  return d->comment;
311 }
QString comment
Definition: zip_p.h:71
ZipPrivate * d
Definition: zip.h:108
void clearPassword ( )

Convenience method, clears the current password.

Definition at line 252 of file zip.cpp.

253 {
254  d->password.clear();
255 }
ZipPrivate * d
Definition: zip.h:108
QString password
Definition: zip_p.h:72
Zip::ErrorCode closeArchive ( )

Closes the archive and writes any pending data.

Definition at line 465 of file zip.cpp.

Referenced by compress(), ZipPrivate::createArchive(), ~Zip(), and ZipPrivate::~ZipPrivate().

466 {
467  Zip::ErrorCode ec = d->closeArchive();
468  d->reset();
469  return ec;
470 }
ZipPrivate * d
Definition: zip.h:108
ErrorCode
Definition: zip.h:48
void reset()
Definition: zip.cpp:1198
Zip::ErrorCode closeArchive()
Definition: zip.cpp:1014

Here is the call graph for this function:

Here is the caller graph for this function:

Zip::ErrorCode createArchive ( const QString &  filename,
bool  overwrite = true 
)

Attempts to create a new Zip archive. If overwrite is true and the file already exist it will be overwritten. Any open archive will be closed.

Definition at line 268 of file zip.cpp.

Referenced by compress().

269 {
270  QFile* file = new QFile(filename);
271 
272  if (file->exists() && !overwrite) {
273  delete file;
274  return Zip::FileExists;
275  }
276 
277  if (!file->open(QIODevice::WriteOnly)) {
278  delete file;
279  return Zip::OpenFailed;
280  }
281 
282  Zip::ErrorCode ec = createArchive(file);
283  if (ec != Zip::Ok) {
284  file->remove();
285  }
286 
287  return ec;
288 }
Definition: zip.h:50
ErrorCode createArchive(const QString &file, bool overwrite=true)
Definition: zip.cpp:268
ErrorCode
Definition: zip.h:48

Here is the caller graph for this function:

Zip::ErrorCode createArchive ( QIODevice *  device)

Attempts to create a new Zip archive. If there is another open archive this will be closed.

Warning
The class takes ownership of the device!

Definition at line 294 of file zip.cpp.

295 {
296  if (device == 0)
297  {
298  qDebug() << "Invalid device.";
299  return Zip::OpenFailed;
300  }
301 
302  return d->createArchive(device);
303 }
Zip::ErrorCode createArchive(QIODevice *device)
Definition: zip.cpp:517
ZipPrivate * d
Definition: zip.h:108

Here is the call graph for this function:

QString formatError ( Zip::ErrorCode  c) const

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

Definition at line 475 of file zip.cpp.

Referenced by compress().

476 {
477  switch (c)
478  {
479  case Ok: return QCoreApplication::translate("Zip", "ZIP operation completed successfully."); break;
480  case ZlibInit: return QCoreApplication::translate("Zip", "Failed to initialize or load zlib library."); break;
481  case ZlibError: return QCoreApplication::translate("Zip", "zlib library error."); break;
482  case OpenFailed: return QCoreApplication::translate("Zip", "Unable to create or open file."); break;
483  case NoOpenArchive: return QCoreApplication::translate("Zip", "No archive has been created yet."); break;
484  case FileNotFound: return QCoreApplication::translate("Zip", "File or directory does not exist."); break;
485  case ReadFailed: return QCoreApplication::translate("Zip", "File read error."); break;
486  case WriteFailed: return QCoreApplication::translate("Zip", "File write error."); break;
487  case SeekFailed: return QCoreApplication::translate("Zip", "File seek error."); break;
488  default: ;
489  }
490 
491  return QCoreApplication::translate("Zip", "Unknown error.");
492 }
Definition: zip.h:50

Here is the caller graph for this function:

bool isOpen ( ) const

Returns true if there is an open archive.

Definition at line 235 of file zip.cpp.

236 {
237  return d->device != 0;
238 }
QIODevice * device
Definition: zip_p.h:62
ZipPrivate * d
Definition: zip.h:108
QString password ( ) const

Returns the currently used password.

Definition at line 258 of file zip.cpp.

Referenced by ZipPrivate::createEntry(), and ZipPrivate::initKeys().

259 {
260  return d->password;
261 }
ZipPrivate * d
Definition: zip.h:108
QString password
Definition: zip_p.h:72

Here is the caller graph for this function:

void setArchiveComment ( const QString &  comment)

Sets the comment for this archive. Note: createArchive() should have been called before.

Definition at line 317 of file zip.cpp.

Referenced by compress().

318 {
319  if (d->device != 0)
320  d->comment = comment;
321 }
QString comment
Definition: zip_p.h:71
QIODevice * device
Definition: zip_p.h:62
ZipPrivate * d
Definition: zip.h:108

Here is the caller graph for this function:

void setPassword ( const QString &  pwd)

Sets the password to be used for the next files being added! Files added before calling this method will use the previously set password (if any). Closing the archive won't clear the password!

Definition at line 246 of file zip.cpp.

Referenced by compress().

247 {
248  d->password = pwd;
249 }
ZipPrivate * d
Definition: zip.h:108
QString password
Definition: zip_p.h:72

Here is the caller graph for this function:

Member Data Documentation


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