Qt 4.8
Public Types | Public Functions | Properties | List of all members
QZipWriter Class Reference

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

#include <qzipwriter_p.h>

Public Types

enum  CompressionPolicy { AlwaysCompress, NeverCompress, AutoCompress }
 
enum  Status {
  NoError, FileWriteError, FileOpenError, FilePermissionsError,
  FileError
}
 The following status values are possible: More...
 

Public Functions

void addDirectory (const QString &dirName)
 Create a new directory in the archive with the specified dirName and the permissions;. More...
 
void addFile (const QString &fileName, const QByteArray &data)
 Add a file to the archive with data as the file contents. More...
 
void addFile (const QString &fileName, QIODevice *device)
 Add a file to the archive with device as the source of the contents. More...
 
void addSymLink (const QString &fileName, const QString &destination)
 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. More...
 
void close ()
 Closes the zip file. More...
 
CompressionPolicy compressionPolicy () const
 Returns the currently set compression policy. More...
 
QFile::Permissions creationPermissions () const
 Returns the currently set creation permissions. More...
 
QIODevicedevice () const
 Returns device used for writing zip archive. More...
 
bool exists () const
 Returns true if the file exists; otherwise returns false. More...
 
bool isWritable () const
 Returns true if the user can write to the archive; otherwise returns false. More...
 
 QZipWriter (const QString &fileName, QIODevice::OpenMode mode=(QIODevice::WriteOnly|QIODevice::Truncate))
 Create a new zip archive that operates on the archive filename. More...
 
 QZipWriter (QIODevice *device)
 Create a new zip archive that operates on the archive found in device. More...
 
void setCompressionPolicy (CompressionPolicy policy)
 Sets the policy for compressing newly added files to the new policy. More...
 
void setCreationPermissions (QFile::Permissions permissions)
 Sets the permissions that will be used for newly added files. More...
 
Status status () const
 Returns a status code indicating the first error that was met by QZipWriter, or QZipWriter::NoError if no error occurred. More...
 
 ~QZipWriter ()
 

Properties

QZipWriterPrivated
 

Detailed Description

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

Warning
This function is not part of the public interface.
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.

Definition at line 64 of file qzipwriter_p.h.

Enumerations

◆ CompressionPolicy

  • AlwaysCompress A file that is added is compressed.
  • NeverCompress A file that is added will be stored without changes.
  • AutoCompress A file that is added will be compressed only if that will give a smaller file.
Enumerator
AlwaysCompress 
NeverCompress 
AutoCompress 

Definition at line 87 of file qzipwriter_p.h.

◆ Status

The following status values are possible:

  • NoError No error occurred.
  • FileWriteError An error occurred when writing to the device.
  • FileOpenError The file could not be opened.
  • FilePermissionsError The file could not be accessed.
  • FileError Another file error occurred.
Enumerator
NoError 
FileWriteError 
FileOpenError 
FilePermissionsError 
FileError 

Definition at line 77 of file qzipwriter_p.h.

Constructors and Destructors

◆ QZipWriter() [1/2]

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

Definition at line 1049 of file qzip.cpp.

1050 {
1051  QScopedPointer<QFile> f(new QFile(fileName));
1052  f->open(mode);
1054  if (f->error() == QFile::NoError)
1055  status = QZipWriter::NoError;
1056  else {
1057  if (f->error() == QFile::WriteError)
1058  status = QZipWriter::FileWriteError;
1059  else if (f->error() == QFile::OpenError)
1060  status = QZipWriter::FileOpenError;
1061  else if (f->error() == QFile::PermissionsError)
1063  else
1064  status = QZipWriter::FileError;
1065  }
1066 
1067  d = new QZipWriterPrivate(f.data(), /*ownDevice=*/true);
1068  f.take();
1069  d->status = status;
1070 }
Status status() const
Returns a status code indicating the first error that was met by QZipWriter, or QZipWriter::NoError i...
Definition: qzip.cpp:1135
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QZipWriter::Status status
Definition: qzip.cpp:454
Status
The following status values are possible:
Definition: qzipwriter_p.h:77
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65

◆ QZipWriter() [2/2]

QZipWriter::QZipWriter ( QIODevice device)
explicit

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.

Definition at line 1077 of file qzip.cpp.

1078  : d(new QZipWriterPrivate(device, /*ownDevice=*/false))
1079 {
1080  Q_ASSERT(device);
1081 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109

◆ ~QZipWriter()

QZipWriter::~QZipWriter ( )

Definition at line 1083 of file qzip.cpp.

1084 {
1085  close();
1086  delete d;
1087 }
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
void close()
Closes the zip file.
Definition: qzip.cpp:1262

Functions

◆ addDirectory()

void QZipWriter::addDirectory ( const QString dirName)

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

Definition at line 1240 of file qzip.cpp.

1241 {
1243  // separator is mandatory
1244  if (!name.endsWith(QLatin1Char('/')))
1245  name.append(QLatin1Char('/'));
1247 }
void addEntry(EntryType type, const QString &fileName, const QByteArray &contents)
Definition: qzip.cpp:568
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
const char * name
static QString fromNativeSeparators(const QString &pathName)
Returns pathName using &#39;/&#39; as file separator.
Definition: qdir.cpp:848
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ addFile() [1/2]

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

Definition at line 1207 of file qzip.cpp.

1208 {
1210 }
void addEntry(EntryType type, const QString &fileName, const QByteArray &contents)
Definition: qzip.cpp:568
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
static QString fromNativeSeparators(const QString &pathName)
Returns pathName using &#39;/&#39; as file separator.
Definition: qdir.cpp:848

◆ addFile() [2/2]

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.

Definition at line 1219 of file qzip.cpp.

1220 {
1221  Q_ASSERT(device);
1222  QIODevice::OpenMode mode = device->openMode();
1223  bool opened = false;
1224  if ((mode & QIODevice::ReadOnly) == 0) {
1225  opened = true;
1226  if (! device->open(QIODevice::ReadOnly)) {
1227  d->status = FileOpenError;
1228  return;
1229  }
1230  }
1232  if (opened)
1233  device->close();
1234 }
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
void addEntry(EntryType type, const QString &fileName, const QByteArray &contents)
Definition: qzip.cpp:568
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QZipWriter::Status status
Definition: qzip.cpp:454
OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
Definition: qiodevice.cpp:465
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
static QString fromNativeSeparators(const QString &pathName)
Returns pathName using &#39;/&#39; as file separator.
Definition: qdir.cpp:848

◆ addSymLink()

void QZipWriter::addSymLink ( const QString fileName,
const QString destination 
)

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.

Definition at line 1254 of file qzip.cpp.

1255 {
1257 }
void addEntry(EntryType type, const QString &fileName, const QByteArray &contents)
Definition: qzip.cpp:568
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
static QString fromNativeSeparators(const QString &pathName)
Returns pathName using &#39;/&#39; as file separator.
Definition: qdir.cpp:848

◆ close()

void QZipWriter::close ( )

Closes the zip file.

Definition at line 1262 of file qzip.cpp.

Referenced by ~QZipWriter().

1263 {
1264  if (!(d->device->openMode() & QIODevice::WriteOnly)) {
1265  d->device->close();
1266  return;
1267  }
1268 
1269  //qDebug("QZip::close writing directory, %d entries", d->fileHeaders.size());
1271  // write new directory
1272  for (int i = 0; i < d->fileHeaders.size(); ++i) {
1273  const FileHeader &header = d->fileHeaders.at(i);
1274  d->device->write((const char *)&header.h, sizeof(CentralFileHeader));
1275  d->device->write(header.file_name);
1276  d->device->write(header.extra_field);
1277  d->device->write(header.file_comment);
1278  }
1279  int dir_size = d->device->pos() - d->start_of_directory;
1280  // write end of directory
1281  EndOfDirectory eod;
1282  memset(&eod, 0, sizeof(EndOfDirectory));
1283  writeUInt(eod.signature, 0x06054b50);
1284  //uchar this_disk[2];
1285  //uchar start_of_directory_disk[2];
1288  writeUInt(eod.directory_size, dir_size);
1291 
1292  d->device->write((const char *)&eod, sizeof(EndOfDirectory));
1293  d->device->write(d->comment);
1294  d->device->close();
1295 }
uchar dir_start_offset[4]
Definition: qzip.cpp:348
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
QByteArray extra_field
Definition: qzip.cpp:356
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
Definition: qiodevice.cpp:624
uchar signature[4]
Definition: qzip.cpp:342
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
CentralFileHeader h
Definition: qzip.cpp:354
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
uchar directory_size[4]
Definition: qzip.cpp:347
QList< FileHeader > fileHeaders
Definition: qzip.cpp:411
int length() const
Same as size().
Definition: qbytearray.h:356
QByteArray file_name
Definition: qzip.cpp:355
OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
Definition: qiodevice.cpp:465
static void writeUShort(uchar *data, ushort i)
Definition: qzip.cpp:113
uchar comment_length[2]
Definition: qzip.cpp:349
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QByteArray file_comment
Definition: qzip.cpp:357
QIODevice * device
Definition: qzip.cpp:408
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342
QByteArray comment
Definition: qzip.cpp:412
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
Definition: qiodevice.cpp:659
uint start_of_directory
Definition: qzip.cpp:413
uchar num_dir_entries_this_disk[2]
Definition: qzip.cpp:345
static void writeUInt(uchar *data, uint i)
Definition: qzip.cpp:105
uchar num_dir_entries[2]
Definition: qzip.cpp:346

◆ compressionPolicy()

QZipWriter::CompressionPolicy QZipWriter::compressionPolicy ( ) const

Returns the currently set compression policy.

See also
setCompressionPolicy()
addFile()

Definition at line 1166 of file qzip.cpp.

1167 {
1168  return d->compressionPolicy;
1169 }
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QZipWriter::CompressionPolicy compressionPolicy
Definition: qzip.cpp:456

◆ creationPermissions()

QFile::Permissions QZipWriter::creationPermissions ( ) const

Returns the currently set creation permissions.

See also
setCreationPermissions()
addFile()

Definition at line 1190 of file qzip.cpp.

1191 {
1192  return d->permissions;
1193 }
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QFile::Permissions permissions
Definition: qzip.cpp:455

◆ device()

QIODevice * QZipWriter::device ( ) const

Returns device used for writing zip archive.

Definition at line 1092 of file qzip.cpp.

1093 {
1094  return d->device;
1095 }
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QIODevice * device
Definition: qzip.cpp:408

◆ exists()

bool QZipWriter::exists ( ) const

Returns true if the file exists; otherwise returns false.

Definition at line 1108 of file qzip.cpp.

1109 {
1110  QFile *f = qobject_cast<QFile*> (d->device);
1111  if (f == 0)
1112  return true;
1113  return f->exists();
1114 }
T qobject_cast(QObject *object)
Definition: qobject.h:375
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
QIODevice * device
Definition: qzip.cpp:408

◆ isWritable()

bool QZipWriter::isWritable ( ) const

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

Definition at line 1100 of file qzip.cpp.

1101 {
1102  return d->device->isWritable();
1103 }
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
Definition: qiodevice.cpp:558
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QIODevice * device
Definition: qzip.cpp:408

◆ setCompressionPolicy()

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

Definition at line 1156 of file qzip.cpp.

1157 {
1158  d->compressionPolicy = policy;
1159 }
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QZipWriter::CompressionPolicy compressionPolicy
Definition: qzip.cpp:456

◆ setCreationPermissions()

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

Definition at line 1179 of file qzip.cpp.

1180 {
1181  d->permissions = permissions;
1182 }
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QFile::Permissions permissions
Definition: qzip.cpp:455

◆ status()

QZipWriter::Status QZipWriter::status ( ) const

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

Definition at line 1135 of file qzip.cpp.

1136 {
1137  return d->status;
1138 }
QZipWriterPrivate * d
Definition: qzipwriter_p.h:109
QZipWriter::Status status
Definition: qzip.cpp:454

Properties

◆ d

QZipWriterPrivate* QZipWriter::d
private

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