Cockatrice 2025-11-30-Development-2.11.0-beta.38
A cross-platform virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
Zip Class Reference

Zip file compression. More...

#include <zip.h>

Collaboration diagram for Zip:
[legend]

Public Types

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

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, CompressionLevel level=AutoFull)
ErrorCode addDirectory (const QString &path, const QString &root, CompressionLevel level=AutoFull)
ErrorCode addDirectory (const QString &path, const QString &root, CompressionOptions options, CompressionLevel level=AutoFull, int *addedFiles=0)
ErrorCode addFile (const QString &path, CompressionLevel level=AutoFull)
ErrorCode addFile (const QString &path, const QString &root, CompressionLevel level=AutoFull)
ErrorCode addFile (const QString &path, const QString &root, CompressionOptions options, CompressionLevel level=AutoFull)
ErrorCode addFiles (const QStringList &paths, CompressionLevel level=AutoFull)
ErrorCode addFiles (const QStringList &paths, const QString &root, CompressionLevel level=AutoFull)
ErrorCode addFiles (const QStringList &paths, const QString &root, CompressionOptions options, CompressionLevel level=AutoFull, int *addedFiles=0)
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:

/home/user/dir1/file1.1
/home/user/dir1/file1.2
/home/user/dir1/dir1.1/
/home/user/dir1/dir1.2/file1.2.1

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

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

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

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

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

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("/home/user/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:

/home/user/dir1/file1.1
/home/user/dir1/file1.2
/home/user/dir1/dir1.1/
/home/user/dir1/dir1.2/file1.2.1

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

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

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

EXAMPLE 5:
myZipInstance.addDirectory("/home/user/dir1", Zip::IgnoreRoot);

RESULT:
Adds the files in /home/user/dir1 but doesn't create the top level
directory:

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

EXAMPLE 5:
myZipInstance.addDirectory("/home/user/dir1", "data/backup", Zip::IgnoreRoot);

RESULT:
Adds the files in /home/user/dir1 but uses "data/backup" as top level
directory instead of "dir1":

data/backup/file1.1
data/backup/file1.2
data/backup/dir1.1/
data/backup/dir1.2/file1.2.1

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 

◆ CompressionOption

Enumerator
RelativePaths 

Does not preserve absolute paths in the zip file when adding a file or 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

IgnoreRoot 

Works only with addDirectory(). Adds the directory's contents, including subdirectories, but does not add an entry for the root directory itself.

SkipBadFiles 

Used only when compressing a directory or multiple files. If set invalid or unreadable files are simply skipped.

CheckForDuplicates 

Makes sure a file is never added twice to the same zip archive. This check is only necessary in certain usage scenarios and given that it slows down processing you need to enable it explicitly with this flag.

◆ 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 
InternalError 

Constructor & Destructor Documentation

◆ Zip()

Zip::Zip ( )

Creates a new Zip file compressor.

◆ ~Zip()

Zip::~Zip ( )
virtual

Closes any open archive and releases used resources.

Here is the call graph for this function:

Member Function Documentation

◆ addDirectory() [1/3]

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

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

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

◆ addDirectory() [2/3]

Zip::ErrorCode Zip::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.

Here is the call graph for this function:

◆ addDirectory() [3/3]

Zip::ErrorCode Zip::addDirectory ( const QString & path,
const QString & root,
CompressionOptions options,
CompressionLevel level = AutoFull,
int * addedFiles = 0 )

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!).

If addedFiles is not null it is set to the number of successfully added files.

◆ addDirectoryContents() [1/2]

Zip::ErrorCode Zip::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.

The result is that all files found in path (and in subdirectories) are added to the zip file without a directory entry.

Here is the call graph for this function:

◆ addDirectoryContents() [2/2]

Zip::ErrorCode Zip::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.

The result is that all files found in path (and in subdirectories) are added to the zip file without a directory entry (or within a directory structure specified by root).

Here is the call graph for this function:

◆ addFile() [1/3]

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

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

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

◆ addFile() [2/3]

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

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

Here is the call graph for this function:

◆ addFile() [3/3]

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

Adds the file at path to the archive, using root as name for the root folder. If path points to a directory the behaviour is basically the same as addDirectory().

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!).

Here is the call graph for this function:

◆ addFiles() [1/3]

Zip::ErrorCode Zip::addFiles ( const QStringList & paths,
CompressionLevel level = AutoFull )

Convenience method, same as calling Zip::addFiles(const QStringList&,const QString&,CompressionOptions,CompressionLevel) with an empty root parameter and Zip::RelativePaths as compression option.

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

◆ addFiles() [2/3]

Zip::ErrorCode Zip::addFiles ( const QStringList & paths,
const QString & root,
CompressionLevel level = AutoFull )

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

Here is the call graph for this function:

◆ addFiles() [3/3]

Zip::ErrorCode Zip::addFiles ( const QStringList & paths,
const QString & root,
CompressionOptions options,
CompressionLevel level = AutoFull,
int * addedFiles = 0 )

Adds the files or directories in paths to the archive, using root as name for the root folder. This is similar to calling addFile or addDirectory for all the entries in paths, except it is slightly faster.

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!).

If addedFiles is not null it is set to the number of successfully added files.

◆ archiveComment()

QString Zip::archiveComment ( ) const

Returns the current archive comment.

◆ clearPassword()

void Zip::clearPassword ( )

Convenience method, clears the current password.

◆ closeArchive()

Zip::ErrorCode Zip::closeArchive ( )

Closes the archive and writes any pending data.

Here is the caller graph for this function:

◆ createArchive() [1/2]

Zip::ErrorCode Zip::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.

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

◆ createArchive() [2/2]

Zip::ErrorCode Zip::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!

◆ formatError()

QString Zip::formatError ( Zip::ErrorCode c) const

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

◆ isOpen()

bool Zip::isOpen ( ) const

Returns true if there is an open archive.

◆ password()

QString Zip::password ( ) const

Returns the currently used password.

◆ setArchiveComment()

void Zip::setArchiveComment ( const QString & comment)

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

◆ setPassword()

void Zip::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!

Member Data Documentation

◆ d

ZipPrivate* Zip::d
private

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