FFSM++ 1.1.0
French Forest Sector Model ++
Loading...
Searching...
No Matches
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.
 
QString password () const
 Returns the currently used password.
 
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

◆ CompressionLevel

Returns the result of a decompression operation. \value Zip::Store No compression. \value Zip::Deflate1 Deflate compression level 1(lowest compression). \value Zip::Deflate1 Deflate compression level 2. \value Zip::Deflate1 Deflate compression level 3. \value Zip::Deflate1 Deflate compression level 4. \value Zip::Deflate1 Deflate compression level 5. \value Zip::Deflate1 Deflate compression level 6. \value Zip::Deflate1 Deflate compression level 7. \value Zip::Deflate1 Deflate compression level 8. \value Zip::Deflate1 Deflate compression level 9 (maximum compression). \value Zip::AutoCPU Adapt compression level to CPU speed (faster CPU => better compression). \value Zip::AutoMIME Adapt compression level to MIME type of the file being compressed. \value 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.

63 {
64 Store,
68 };
@ Deflate5
Definition zip.h:66
@ Deflate1
Definition zip.h:65
@ Deflate9
Definition zip.h:66
@ Deflate6
Definition zip.h:66
@ AutoMIME
Definition zip.h:67
@ Deflate2
Definition zip.h:65
@ Deflate7
Definition zip.h:66
@ Store
Definition zip.h:64
@ Deflate4
Definition zip.h:65
@ Deflate3
Definition zip.h:65
@ AutoCPU
Definition zip.h:67
@ Deflate8
Definition zip.h:66
@ AutoFull
Definition zip.h:67

◆ CompressionOption

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 };
@ IgnorePaths
Do not store paths. All the files are put in the (evtl. user defined) root of the zip file.
Definition zip.h:77
@ AbsolutePaths
Preserve absolute paths.
Definition zip.h:75
@ RelativePaths
Does not preserve absolute paths in the zip file when adding a file/directory (default)
Definition zip.h:73

◆ ErrorCode

enum ErrorCode

The result of a compression operation. \value Zip::Ok No error occurred. \value Zip::ZlibInit Failed to init or load the zlib library. \value Zip::ZlibError The zlib library returned some error. \value Zip::FileExists The file already exists and will not be overwritten. \value Zip::OpenFailed Unable to create or open a device. \value Zip::NoOpenArchive CreateArchive() has not been called yet. \value Zip::FileNotFound File or directory does not exist. \value Zip::ReadFailed Reading of a file failed. \value Zip::WriteFailed Writing of a file failed. \value 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,
60 };
@ OpenFailed
Definition zip.h:54
@ ZlibError
Definition zip.h:52
@ Ok
Definition zip.h:50
@ NoOpenArchive
Definition zip.h:55
@ ReadFailed
Definition zip.h:57
@ SeekFailed
Definition zip.h:59
@ FileNotFound
Definition zip.h:56
@ FileExists
Definition zip.h:53
@ WriteFailed
Definition zip.h:58
@ ZlibInit
Definition zip.h:51

Constructor & Destructor Documentation

◆ Zip()

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()

~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
Here is the call graph for this function:

Member Function Documentation

◆ addDirectory() [1/3]

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.

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

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

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

◆ addDirectory() [2/3]

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}
Here is the call graph for this function:

◆ addDirectory() [3/3]

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}
QIODevice * device
Definition zip_p.h:62
Zip::ErrorCode createEntry(const QFileInfo &file, const QString &root, Zip::CompressionLevel level)
Definition zip.cpp:541
QString extractRoot(const QString &p)
Definition zip.cpp:1213
ErrorCode
Definition zip.h:49
Here is the call graph for this function:

◆ addDirectoryContents() [1/2]

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}
Here is the call graph for this function:

◆ addDirectoryContents() [2/2]

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}
Here is the call graph for this function:

◆ archiveComment()

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

◆ clearPassword()

void clearPassword ( )

Convenience method, clears the current password.

Definition at line 252 of file zip.cpp.

253{
254 d->password.clear();
255}
QString password
Definition zip_p.h:72

◆ closeArchive()

Zip::ErrorCode closeArchive ( )

Closes the archive and writes any pending data.

Definition at line 465 of file zip.cpp.

466{
468 d->reset();
469 return ec;
470}
void reset()
Definition zip.cpp:1198
Zip::ErrorCode closeArchive()
Definition zip.cpp:1014

Referenced by compress(), and ~Zip().

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

◆ createArchive() [1/2]

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.

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}
ErrorCode createArchive(const QString &file, bool overwrite=true)
Definition zip.cpp:268

Referenced by compress(), and createArchive().

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

◆ createArchive() [2/2]

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
Here is the call graph for this function:

◆ formatError()

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.

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}

Referenced by compress().

Here is the caller graph for this function:

◆ isOpen()

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}

◆ password()

QString password ( ) const

Returns the currently used password.

Definition at line 258 of file zip.cpp.

259{
260 return d->password;
261}

◆ setArchiveComment()

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.

318{
319 if (d->device != 0)
320 d->comment = comment;
321}

Referenced by compress().

Here is the caller graph for this function:

◆ setPassword()

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.

247{
248 d->password = pwd;
249}

Referenced by compress().

Here is the caller graph for this function:

Member Data Documentation

◆ d


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