QZipWriter Class

the QZipWriter class provides a way to create a new zip archive. More...

Header: #include <QZipWriter>

Public Types

enum CompressionPolicy { AlwaysCompress, NeverCompress, AutoCompress }
enum Status { NoError, FileWriteError, FileOpenError, FilePermissionsError, FileError }

Public Functions

QZipWriter(const QString &fileName, QIODevice::OpenMode mode = (QIODevice::WriteOnly | QIODevice::Truncate))
QZipWriter(QIODevice *device)
void addDirectory(const QString &dirName)
void addFile(const QString &fileName, const QByteArray &data)
void addFile(const QString &fileName, QIODevice *device)
void addSymLink(const QString &fileName, const QString &destination)
void close()
CompressionPolicy compressionPolicy() const
QFile::Permissions creationPermissions() const
QIODevice *device() const
bool exists() const
bool isWritable() const
void setCompressionPolicy(CompressionPolicy policy)
void setCreationPermissions(QFile::Permissions permissions)
Status status() const

Detailed Description

\internal\since 4.5

QZipWriter can be used to create a zip archive containing any number of files and directories. The files in the archive will be compressed in a way that is compatible with common zip reader applications.

Member Type Documentation

enum QZipWriter::CompressionPolicy

ConstantValueDescription
QZipWriter::AlwaysCompress0A file that is added is compressed.
QZipWriter::NeverCompress1A file that is added will be stored without changes.
QZipWriter::AutoCompress2A file that is added will be compressed only if that will give a smaller file.

enum QZipWriter::Status

The following status values are possible:

ConstantValueDescription
QZipWriter::NoError0No error occurred.
QZipWriter::FileWriteError1An error occurred when writing to the device.
QZipWriter::FileOpenError2The file could not be opened.
QZipWriter::FilePermissionsError3The file could not be accessed.
QZipWriter::FileError4Another file error occurred.

Member Function Documentation

[explicit] QZipWriter::QZipWriter(const QString &fileName, QIODevice::OpenMode mode = (QIODevice::WriteOnly | QIODevice::Truncate))

Create a new zip archive that operates on the archive filename. The file will be opened with the mode.

See also isValid().

[explicit] QZipWriter::QZipWriter(QIODevice *device)

Create a new zip archive that operates on the archive found in device. You have to open the device previous to calling the constructor and only a device that is readable will be scanned for zip filecontent.

void QZipWriter::addDirectory(const QString &dirName)

Create a new directory in the archive with the specified dirName and the permissions;

void QZipWriter::addFile(const QString &fileName, const QByteArray &data)

Add a file to the archive with data as the file contents. The file will be stored in the archive using the fileName which includes the full path in the archive.

The new file will get the file permissions based on the current creationPermissions and it will be compressed using the zip compression based on the current compression policy.

See also setCreationPermissions() and setCompressionPolicy().

void QZipWriter::addFile(const QString &fileName, QIODevice *device)

Add a file to the archive with device as the source of the contents. The contents returned from QIODevice::readAll() will be used as the filedata. The file will be stored in the archive using the fileName which includes the full path in the archive.

Create a new symbolic link in the archive with the specified dirName and the permissions; A symbolic link contains the destination (relative) path and name.

void QZipWriter::close()

Closes the zip file.

CompressionPolicy QZipWriter::compressionPolicy() const

Returns the currently set compression policy.

See also setCompressionPolicy() and addFile().

QFile::Permissions QZipWriter::creationPermissions() const

Returns the currently set creation permissions.

See also setCreationPermissions() and addFile().

QIODevice *QZipWriter::device() const

Returns device used for writing zip archive.

bool QZipWriter::exists() const

Returns true if the file exists; otherwise returns false.

bool QZipWriter::isWritable() const

Returns true if the user can write to the archive; otherwise returns false.

void QZipWriter::setCompressionPolicy(CompressionPolicy policy)

Sets the policy for compressing newly added files to the new policy.

Note: the default policy is AlwaysCompress

See also compressionPolicy() and addFile().

void QZipWriter::setCreationPermissions(QFile::Permissions permissions)

Sets the permissions that will be used for newly added files.

Note: the default permissions are QFile::ReadOwner | QFile::WriteOwner.

See also creationPermissions() and addFile().

Status QZipWriter::status() const

Returns a status code indicating the first error that was met by QZipWriter, or QZipWriter::NoError if no error occurred.