Zip Class Reference

Zip file compression. More...

#include <zip.h>

Collaboration diagram for Zip:

Collaboration graph
[legend]
List of all members.

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

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.

enum CompressionLevel

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.

enum 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.


Constructor & Destructor Documentation

Zip (  ) 

Creates a new Zip file compressor.

Definition at line 218 of file zip.cpp.

~Zip (  )  [virtual]

Closes any open archive and releases used resources.

Definition at line 226 of file zip.cpp.


Member Function Documentation

bool isOpen (  )  const

Returns true if there is an open archive.

Definition at line 235 of file zip.cpp.

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

Here is the caller graph for this function:

void clearPassword (  ) 

Convenience method, clears the current password.

Definition at line 252 of file zip.cpp.

QString password (  )  const

Returns the currently used password.

Definition at line 258 of file zip.cpp.

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

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.

QString archiveComment (  )  const

Returns the current archive comment.

Definition at line 308 of file zip.cpp.

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

Here is the caller 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.

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.

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

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.

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.

Zip::ErrorCode closeArchive (  ) 

Closes the archive and writes any pending data.

Definition at line 465 of file zip.cpp.

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

Here is the caller 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().

Here is the caller graph for this function:


Member Data Documentation

ZipPrivate* d [private]

Definition at line 108 of file zip.h.

Referenced by addDirectory(), archiveComment(), clearPassword(), closeArchive(), createArchive(), isOpen(), password(), setArchiveComment(), setPassword(), Zip(), and ~Zip().


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