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

the QZipReader class provides a way to inspect the contents of a zip archive and extract individual files from it. More...

#include <qzipreader_p.h>

Classes

class  FileInfo
 Represents one entry in the zip table of contents. More...
 

Public Types

enum  Status {
  NoError, FileReadError, FileOpenError, FilePermissionsError,
  FileError
}
 The following status values are possible: More...
 

Public Functions

void close ()
 Close the zip file. More...
 
int count () const
 Return the number of items in the zip archive. More...
 
QIODevicedevice () const
 Returns device used for reading zip archive. More...
 
FileInfo entryInfoAt (int index) const
 Returns a FileInfo of an entry in the zipfile. More...
 
bool exists () const
 Returns true if the file exists; otherwise returns false. More...
 
bool extractAll (const QString &destinationDir) const
 Extracts the full contents of the zip file into destinationDir on the local filesystem. More...
 
QByteArray fileData (const QString &fileName) const
 Fetch the file contents from the zip archive and return the uncompressed bytes. More...
 
QList< FileInfofileInfoList () const
 Returns the list of files the archive contains. More...
 
bool isReadable () const
 Returns true if the user can read the file; otherwise returns false. More...
 
 QZipReader (const QString &fileName, QIODevice::OpenMode mode=QIODevice::ReadOnly)
 Create a new zip archive that operates on the fileName. More...
 
 QZipReader (QIODevice *device)
 Create a new zip archive that operates on the archive found in device. More...
 
Status status () const
 Returns a status code indicating the first error that was met by QZipReader, or QZipReader::NoError if no error occurred. More...
 
 ~QZipReader ()
 Desctructor. More...
 

Properties

QZipReaderPrivated
 

Detailed Description

the QZipReader class provides a way to inspect the contents of a zip archive and extract individual files from it.

Warning
This function is not part of the public interface.
Since
4.5

QZipReader can be used to read a zip archive either from a file or from any device. An in-memory QBuffer for instance. The reader can be used to read which files are in the archive using fileInfoList() and entryInfoAt() but also to extract individual files using fileData() or even to extract all files in the archive using extractAll()

Definition at line 66 of file qzipreader_p.h.

Enumerations

◆ Status

The following status values are possible:

  • NoError No error occurred.
  • FileReadError An error occurred when reading from the file.
  • FileOpenError The file could not be opened.
  • FilePermissionsError The file could not be accessed.
  • FileError Another file error occurred.
Enumerator
NoError 
FileReadError 
FileOpenError 
FilePermissionsError 
FileError 

Definition at line 104 of file qzipreader_p.h.

Constructors and Destructors

◆ QZipReader() [1/2]

QZipReader::QZipReader ( const QString archive,
QIODevice::OpenMode  mode = QIODevice::ReadOnly 
)

Create a new zip archive that operates on the fileName.

The file will be opened with the mode.

Definition at line 758 of file qzip.cpp.

759 {
760  QScopedPointer<QFile> f(new QFile(archive));
761  f->open(mode);
763  if (f->error() == QFile::NoError)
764  status = NoError;
765  else {
766  if (f->error() == QFile::ReadError)
767  status = FileReadError;
768  else if (f->error() == QFile::OpenError)
769  status = FileOpenError;
770  else if (f->error() == QFile::PermissionsError)
771  status = FilePermissionsError;
772  else
773  status = FileError;
774  }
775 
776  d = new QZipReaderPrivate(f.data(), /*ownDevice=*/true);
777  f.take();
778  d->status = status;
779 }
Status status() const
Returns a status code indicating the first error that was met by QZipReader, or QZipReader::NoError i...
Definition: qzip.cpp:1013
Status
The following status values are possible:
Definition: qzipreader_p.h:104
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
QZipReader::Status status
Definition: qzip.cpp:440
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

◆ QZipReader() [2/2]

QZipReader::QZipReader ( 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 786 of file qzip.cpp.

787  : d(new QZipReaderPrivate(device, /*ownDevice=*/false))
788 {
789  Q_ASSERT(device);
790 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

◆ ~QZipReader()

QZipReader::~QZipReader ( )

Desctructor.

Definition at line 795 of file qzip.cpp.

796 {
797  close();
798  delete d;
799 }
void close()
Close the zip file.
Definition: qzip.cpp:1021
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

Functions

◆ close()

void QZipReader::close ( )

Close the zip file.

Definition at line 1021 of file qzip.cpp.

Referenced by ~QZipReader().

1022 {
1023  d->device->close();
1024 }
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
QIODevice * device
Definition: qzip.cpp:408
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

◆ count()

int QZipReader::count ( ) const

Return the number of items in the zip archive.

Definition at line 847 of file qzip.cpp.

848 {
849  d->scanFiles();
850  return d->fileHeaders.count();
851 }
void scanFiles()
Definition: qzip.cpp:479
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QList< FileHeader > fileHeaders
Definition: qzip.cpp:411
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

◆ device()

QIODevice * QZipReader::device ( ) const

Returns device used for reading zip archive.

Definition at line 804 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry(), QZipReaderPrivate::scanFiles(), and QZipPrivate::~QZipPrivate().

805 {
806  return d->device;
807 }
QIODevice * device
Definition: qzip.cpp:408
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

◆ entryInfoAt()

QZipReader::FileInfo QZipReader::entryInfoAt ( int  index) const

Returns a FileInfo of an entry in the zipfile.

The index is the index into the directory listing of the zipfile. Returns an invalid FileInfo if index is out of boundaries.

See also
fileInfoList()

Definition at line 860 of file qzip.cpp.

861 {
862  d->scanFiles();
864  if (index >= 0 && index < d->fileHeaders.count())
865  d->fillFileInfo(index, fi);
866  return fi;
867 }
void fillFileInfo(int index, QZipReader::FileInfo &fileInfo) const
Definition: qzip.cpp:416
void scanFiles()
Definition: qzip.cpp:479
Represents one entry in the zip table of contents.
Definition: qzipreader_p.h:79
quint16 index
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

◆ exists()

bool QZipReader::exists ( ) const

Returns true if the file exists; otherwise returns false.

Definition at line 820 of file qzip.cpp.

821 {
822  QFile *f = qobject_cast<QFile*> (d->device);
823  if (f == 0)
824  return true;
825  return f->exists();
826 }
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
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
QIODevice * device
Definition: qzip.cpp:408
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

◆ extractAll()

bool QZipReader::extractAll ( const QString destinationDir) const

Extracts the full contents of the zip file into destinationDir on the local filesystem.

In case writing or linking a file fails, the extraction will be aborted.

Definition at line 944 of file qzip.cpp.

945 {
946  QDir baseDir(destinationDir);
947 
948  // create directories first
949  QList<FileInfo> allFiles = fileInfoList();
950  foreach (FileInfo fi, allFiles) {
951  const QString absPath = destinationDir + QDir::separator() + fi.filePath;
952  if (fi.isDir) {
953  if (!baseDir.mkpath(fi.filePath))
954  return false;
955  if (!QFile::setPermissions(absPath, fi.permissions))
956  return false;
957  }
958  }
959 
960  // set up symlinks
961  foreach (FileInfo fi, allFiles) {
962  const QString absPath = destinationDir + QDir::separator() + fi.filePath;
963  if (fi.isSymLink) {
964  QString destination = QFile::decodeName(fileData(fi.filePath));
965  if (destination.isEmpty())
966  return false;
967  QFileInfo linkFi(absPath);
968  if (!QFile::exists(linkFi.absolutePath()))
969  QDir::root().mkpath(linkFi.absolutePath());
970  if (!QFile::link(destination, absPath))
971  return false;
972  /* cannot change permission of links
973  if (!QFile::setPermissions(absPath, fi.permissions))
974  return false;
975  */
976  }
977  }
978 
979  foreach (FileInfo fi, allFiles) {
980  const QString absPath = destinationDir + QDir::separator() + fi.filePath;
981  if (fi.isFile) {
982  QFile f(absPath);
983  if (!f.open(QIODevice::WriteOnly))
984  return false;
985  f.write(fileData(fi.filePath));
986  f.setPermissions(fi.permissions);
987  f.close();
988  }
989  }
990 
991  return true;
992 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
bool mkpath(const QString &dirPath) const
Creates the directory path dirPath.
Definition: qdir.cpp:1477
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831
QByteArray fileData(const QString &fileName) const
Fetch the file contents from the zip archive and return the uncompressed bytes.
Definition: qzip.cpp:872
static QString decodeName(const QByteArray &localFileName)
This does the reverse of QFile::encodeName() using localFileName.
Definition: qfile.cpp:552
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QList< FileInfo > fileInfoList() const
Returns the list of files the archive contains.
Definition: qzip.cpp:831
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
bool setPermissions(Permissions permissionSpec)
Sets the permissions for the file to the permissions specified.
Definition: qfile.cpp:1605
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static QDir root()
Returns the root directory.
Definition: qdir.h:214
bool link(const QString &newName)
Creates a link named linkName that points to the file currently specified by fileName().
Definition: qfile.cpp:877
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ fileData()

QByteArray QZipReader::fileData ( const QString fileName) const

Fetch the file contents from the zip archive and return the uncompressed bytes.

Definition at line 872 of file qzip.cpp.

Referenced by extractAll().

873 {
874  d->scanFiles();
875  int i;
876  for (i = 0; i < d->fileHeaders.size(); ++i) {
877  if (QString::fromLocal8Bit(d->fileHeaders.at(i).file_name) == fileName)
878  break;
879  }
880  if (i == d->fileHeaders.size())
881  return QByteArray();
882 
883  FileHeader header = d->fileHeaders.at(i);
884 
885  int compressed_size = readUInt(header.h.compressed_size);
886  int uncompressed_size = readUInt(header.h.uncompressed_size);
887  int start = readUInt(header.h.offset_local_header);
888  //qDebug("uncompressing file %d: local header at %d", i, start);
889 
890  d->device->seek(start);
891  LocalFileHeader lh;
892  d->device->read((char *)&lh, sizeof(LocalFileHeader));
893  uint skip = readUShort(lh.file_name_length) + readUShort(lh.extra_field_length);
894  d->device->seek(d->device->pos() + skip);
895 
896  int compression_method = readUShort(lh.compression_method);
897  //qDebug("file=%s: compressed_size=%d, uncompressed_size=%d", fileName.toLocal8Bit().data(), compressed_size, uncompressed_size);
898 
899  //qDebug("file at %lld", d->device->pos());
900  QByteArray compressed = d->device->read(compressed_size);
901  if (compression_method == 0) {
902  // no compression
903  compressed.truncate(uncompressed_size);
904  return compressed;
905  } else if (compression_method == 8) {
906  // Deflate
907  //qDebug("compressed=%d", compressed.size());
908  compressed.truncate(compressed_size);
909  QByteArray baunzip;
910  ulong len = qMax(uncompressed_size, 1);
911  int res;
912  do {
913  baunzip.resize(len);
914  res = inflate((uchar*)baunzip.data(), &len,
915  (uchar*)compressed.constData(), compressed_size);
916 
917  switch (res) {
918  case Z_OK:
919  if ((int)len != baunzip.size())
920  baunzip.resize(len);
921  break;
922  case Z_MEM_ERROR:
923  qWarning("QZip: Z_MEM_ERROR: Not enough memory");
924  break;
925  case Z_BUF_ERROR:
926  len *= 2;
927  break;
928  case Z_DATA_ERROR:
929  qWarning("QZip: Z_DATA_ERROR: Input data is corrupted");
930  break;
931  }
932  } while (res == Z_BUF_ERROR);
933  return baunzip;
934  }
935  qWarning() << "QZip: Unknown compression method";
936  return QByteArray();
937 }
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
void truncate(int pos)
Truncates the byte array at index position pos.
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
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
void scanFiles()
Definition: qzip.cpp:479
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
static uint readUInt(const uchar *data)
Definition: qzip.cpp:95
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
unsigned char uchar
Definition: qglobal.h:994
static ushort readUShort(const uchar *data)
Definition: qzip.cpp:100
uchar offset_local_header[4]
Definition: qzip.cpp:336
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
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
uchar uncompressed_size[4]
Definition: qzip.cpp:329
unsigned long ulong
Definition: qglobal.h:997
static int inflate(Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen)
Definition: qzip.cpp:189
QList< FileHeader > fileHeaders
Definition: qzip.cpp:411
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QByteArray file_name
Definition: qzip.cpp:355
uchar compressed_size[4]
Definition: qzip.cpp:328
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QIODevice * device
Definition: qzip.cpp:408
QZipReaderPrivate * d
Definition: qzipreader_p.h:117
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

◆ fileInfoList()

QList< QZipReader::FileInfo > QZipReader::fileInfoList ( ) const

Returns the list of files the archive contains.

Definition at line 831 of file qzip.cpp.

Referenced by extractAll().

832 {
833  d->scanFiles();
835  for (int i = 0; i < d->fileHeaders.size(); ++i) {
837  d->fillFileInfo(i, fi);
838  files.append(fi);
839  }
840  return files;
841 
842 }
void fillFileInfo(int index, QZipReader::FileInfo &fileInfo) const
Definition: qzip.cpp:416
void scanFiles()
Definition: qzip.cpp:479
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
Represents one entry in the zip table of contents.
Definition: qzipreader_p.h:79
QList< FileHeader > fileHeaders
Definition: qzip.cpp:411
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QZipReaderPrivate * d
Definition: qzipreader_p.h:117
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ isReadable()

bool QZipReader::isReadable ( ) const

Returns true if the user can read the file; otherwise returns false.

Definition at line 812 of file qzip.cpp.

813 {
814  return d->device->isReadable();
815 }
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
Definition: qiodevice.cpp:544
QIODevice * device
Definition: qzip.cpp:408
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

◆ status()

QZipReader::Status QZipReader::status ( ) const

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

Definition at line 1013 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry(), QZipReader(), QZipWriter::QZipWriter(), and QZipReaderPrivate::scanFiles().

1014 {
1015  return d->status;
1016 }
QZipReader::Status status
Definition: qzip.cpp:440
QZipReaderPrivate * d
Definition: qzipreader_p.h:117

Properties

◆ d

QZipReaderPrivate* QZipReader::d
private

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