Qt 4.8
Public Types | Public Functions | Public Variables | List of all members
QZipWriterPrivate Class Reference
Inheritance diagram for QZipWriterPrivate:
QZipPrivate

Public Types

enum  EntryType { Directory, File, Symlink }
 

Public Functions

void addEntry (EntryType type, const QString &fileName, const QByteArray &contents)
 
 QZipWriterPrivate (QIODevice *device, bool ownDev)
 
- Public Functions inherited from QZipPrivate
void fillFileInfo (int index, QZipReader::FileInfo &fileInfo) const
 
 QZipPrivate (QIODevice *device, bool ownDev)
 
 ~QZipPrivate ()
 

Public Variables

QZipWriter::CompressionPolicy compressionPolicy
 
QFile::Permissions permissions
 
QZipWriter::Status status
 
- Public Variables inherited from QZipPrivate
QByteArray comment
 
QIODevicedevice
 
bool dirtyFileTree
 
QList< FileHeaderfileHeaders
 
bool ownDevice
 
uint start_of_directory
 

Detailed Description

Definition at line 443 of file qzip.cpp.

Enumerations

◆ EntryType

Enumerator
Directory 
File 
Symlink 

Definition at line 458 of file qzip.cpp.

Constructors and Destructors

◆ QZipWriterPrivate()

QZipWriterPrivate::QZipWriterPrivate ( QIODevice device,
bool  ownDev 
)
inline

Definition at line 446 of file qzip.cpp.

447  : QZipPrivate(device, ownDev),
451  {
452  }
QZipWriter::Status status
Definition: qzip.cpp:454
QFile::Permissions permissions
Definition: qzip.cpp:455
QZipPrivate(QIODevice *device, bool ownDev)
Definition: qzip.cpp:395
QZipWriter::CompressionPolicy compressionPolicy
Definition: qzip.cpp:456

Functions

◆ addEntry()

void QZipWriterPrivate::addEntry ( EntryType  type,
const QString fileName,
const QByteArray contents 
)

Definition at line 568 of file qzip.cpp.

Referenced by QZipWriter::addDirectory(), QZipWriter::addFile(), and QZipWriter::addSymLink().

569 {
570 #ifndef NDEBUG
571  static const char *entryTypes[] = {
572  "directory",
573  "file ",
574  "symlink " };
575  ZDEBUG() << "adding" << entryTypes[type] <<":" << fileName.toUtf8().data() << (type == 2 ? QByteArray(" -> " + contents).constData() : "");
576 #endif
577 
578  if (! (device->isOpen() || device->open(QIODevice::WriteOnly))) {
580  return;
581  }
583 
584  // don't compress small files
587  if (contents.length() < 64)
588  compression = QZipWriter::NeverCompress;
589  else
590  compression = QZipWriter::AlwaysCompress;
591  }
592 
593  FileHeader header;
594  memset(&header.h, 0, sizeof(CentralFileHeader));
595  writeUInt(header.h.signature, 0x02014b50);
596 
597  writeUShort(header.h.version_needed, 0x14);
598  writeUInt(header.h.uncompressed_size, contents.length());
600  QByteArray data = contents;
601  if (compression == QZipWriter::AlwaysCompress) {
602  writeUShort(header.h.compression_method, 8);
603 
604  ulong len = contents.length();
605  // shamelessly copied form zlib
606  len += (len >> 12) + (len >> 14) + 11;
607  int res;
608  do {
609  data.resize(len);
610  res = deflate((uchar*)data.data(), &len, (const uchar*)contents.constData(), contents.length());
611 
612  switch (res) {
613  case Z_OK:
614  data.resize(len);
615  break;
616  case Z_MEM_ERROR:
617  qWarning("QZip: Z_MEM_ERROR: Not enough memory to compress file, skipping");
618  data.resize(0);
619  break;
620  case Z_BUF_ERROR:
621  len *= 2;
622  break;
623  }
624  } while (res == Z_BUF_ERROR);
625  }
626 // TODO add a check if data.length() > contents.length(). Then try to store the original and revert the compression method to be uncompressed
627  writeUInt(header.h.compressed_size, data.length());
628  uint crc_32 = ::crc32(0, 0, 0);
629  crc_32 = ::crc32(crc_32, (const uchar *)contents.constData(), contents.length());
630  writeUInt(header.h.crc_32, crc_32);
631 
632  header.file_name = fileName.toLocal8Bit();
633  if (header.file_name.size() > 0xffff) {
634  qWarning("QZip: Filename too long, chopping it to 65535 characters");
635  header.file_name = header.file_name.left(0xffff);
636  }
637  writeUShort(header.h.file_name_length, header.file_name.length());
638  //h.extra_field_length[2];
639 
640  writeUShort(header.h.version_made, 3 << 8);
641  //uchar internal_file_attributes[2];
642  //uchar external_file_attributes[4];
644  switch (type) {
645  case File: mode |= S_IFREG; break;
646  case Directory: mode |= S_IFDIR; break;
647  case Symlink: mode |= S_IFLNK; break;
648  }
649  writeUInt(header.h.external_file_attributes, mode << 16);
651 
652 
653  fileHeaders.append(header);
654 
655  LocalFileHeader h = header.h.toLocalHeader();
656  device->write((const char *)&h, sizeof(LocalFileHeader));
657  device->write(header.file_name);
658  device->write(data);
660  dirtyFileTree = true;
661 }
int type
Definition: qmetatype.cpp:239
#define S_IFLNK
Definition: qzip.cpp:68
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
static int deflate(Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen)
Definition: qzip.cpp:224
uchar crc_32[4]
Definition: qzip.cpp:327
uchar file_name_length[2]
Definition: qzip.cpp:330
uchar version_needed[2]
Definition: qzip.cpp:323
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
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 external_file_attributes[4]
Definition: qzip.cpp:335
#define S_IFREG
Definition: qzip.cpp:58
uchar last_mod_file[4]
Definition: qzip.cpp:326
uchar compression_method[2]
Definition: qzip.cpp:325
#define ZDEBUG
Definition: qzip.cpp:90
unsigned char uchar
Definition: qglobal.h:994
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
uchar version_made[2]
Definition: qzip.cpp:322
uchar offset_local_header[4]
Definition: qzip.cpp:336
QZipWriter::Status status
Definition: qzip.cpp:454
CentralFileHeader h
Definition: qzip.cpp:354
QFile::Permissions permissions
Definition: qzip.cpp:455
uchar signature[4]
Definition: qzip.cpp:321
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
uchar uncompressed_size[4]
Definition: qzip.cpp:329
QByteArray left(int len) const
Returns a byte array that contains the leftmost len bytes of this byte array.
unsigned long ulong
Definition: qglobal.h:997
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
QList< FileHeader > fileHeaders
Definition: qzip.cpp:411
int length() const
Same as size().
Definition: qbytearray.h:356
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
static void writeMSDosDate(uchar *dest, const QDateTime &dt)
Definition: qzip.cpp:133
QByteArray file_name
Definition: qzip.cpp:355
QZipWriter::CompressionPolicy compressionPolicy
Definition: qzip.cpp:456
static void writeUShort(uchar *data, ushort i)
Definition: qzip.cpp:113
uchar compressed_size[4]
Definition: qzip.cpp:328
void resize(int size)
Sets the size of the byte array to size bytes.
unsigned int quint32
Definition: qglobal.h:938
static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
Definition: qdatetime.cpp:3138
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
LocalFileHeader toLocalHeader() const
Definition: qzip.cpp:463
QIODevice * device
Definition: qzip.cpp:408
bool dirtyFileTree
Definition: qzip.cpp:410
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342
#define S_IFDIR
Definition: qzip.cpp:60
static quint32 permissionsToMode(QFile::Permissions perms)
Definition: qzip.cpp:159
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
static void writeUInt(uchar *data, uint i)
Definition: qzip.cpp:105

Properties

◆ compressionPolicy

QZipWriter::CompressionPolicy QZipWriterPrivate::compressionPolicy

Definition at line 456 of file qzip.cpp.

Referenced by QZipWriter::compressionPolicy(), and QZipWriter::setCompressionPolicy().

◆ permissions

QFile::Permissions QZipWriterPrivate::permissions

◆ status

QZipWriter::Status QZipWriterPrivate::status

Definition at line 454 of file qzip.cpp.

Referenced by QZipWriter::addFile(), and QZipWriter::status().


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