Scribus
Open source desktop publishing at your fingertips
QtIOCompressor Class Reference

The QtIOCompressor class is a QIODevice that compresses data streams. More...

Inheritance diagram for QtIOCompressor:
Collaboration diagram for QtIOCompressor:

Public Types

enum  StreamFormat { ZlibFormat, GzipFormat, RawZipFormat }
 

Public Member Functions

 QtIOCompressor (QIODevice *device, int compressionLevel=6, int bufferSize=65500)
 
 ~QtIOCompressor ()
 
void setStreamFormat (StreamFormat format)
 
StreamFormat streamFormat () const
 
bool isSequential () const
 
bool open (OpenMode mode)
 
void close ()
 
void flush ()
 
qint64 bytesAvailable () const
 

Static Public Member Functions

static bool isGzipSupported ()
 

Protected Member Functions

qint64 readData (char *data, qint64 maxSize)
 
qint64 writeData (const char *data, qint64 maxSize)
 

Detailed Description

The QtIOCompressor class is a QIODevice that compresses data streams.

A QtIOCompressor object is constructed with a pointer to an underlying QIODevice. Data written to the QtIOCompressor object will be compressed before it is written to the underlying QIODevice. Similary, if you read from the QtIOCompressor object, the data will be read from the underlying device and then decompressed.

QtIOCompressor is a sequential device, which means that it does not support seeks or random access. Internally, QtIOCompressor uses the zlib library to compress and uncompress data.

Usage examples: Writing compressed data to a file:

QFile file("foo");
QtIOCompressor compressor(&file);
compressor.open(QIODevice::WriteOnly);
compressor.write(QByteArray() << "The quick brown fox");
compressor.close();

Reading compressed data from a file:

QFile file("foo");
QtIOCompressor compressor(&file);
compressor.open(QIODevice::ReadOnly);
const QByteArray text = compressor.readAll();
compressor.close();

QtIOCompressor can also read and write compressed data in different compressed formats, ref. StreamFormat. Use setStreamFormat() before open() to select format.

Member Enumeration Documentation

This enum specifies which stream format to use.

ZlibFormat: This is the default and has the smallest overhead.

GzipFormat: This format is compatible with the gzip file format, but has more overhead than ZlibFormat. Note: requires zlib version 1.2.x or higher at runtime.

RawZipFormat: This is compatible with the most common compression method of the data blocks contained in ZIP archives. Note: ZIP file headers are not read or generated, so setting this format, by itself, does not let QtIOCompressor read or write ZIP files. Ref. the ziplist example program.

See also
setStreamFormat()

Constructor & Destructor Documentation

QtIOCompressor::QtIOCompressor ( QIODevice *  device,
int  compressionLevel = 6,
int  bufferSize = 65500 
)

Constructs a QtIOCompressor using the given device as the underlying device.

The allowed value range for compressionLevel is 0 to 9, where 0 means no compression and 9 means maximum compression. The default value is 6.

bufferSize specifies the size of the internal buffer used when reading from and writing to the underlying device. The default value is 65KB. Using a larger value allows for faster compression and deompression at the expense of memory usage.

QtIOCompressor::~QtIOCompressor ( )

Destroys the QtIOCompressor, closing it if neccesary.

Member Function Documentation

qint64 QtIOCompressor::bytesAvailable ( ) const

Returns 1 if there might be data available for reading, or 0 if there is no data available.

There is unfortunately no way of knowing how much data there is available when dealing with compressed streams.

Also, since the remaining compressed data might be a part of the meta-data that ends the compressed stream (and therefore will yield no uncompressed data), you cannot assume that a read after getting a 1 from this function will return data.

void QtIOCompressor::close ( )

Closes the QtIOCompressor, and also the underlying device if it was opened by QtIOCompressor.

See also
open()
void QtIOCompressor::flush ( )

Flushes the internal buffer.

Each time you call flush, all data written to the QtIOCompressor is compressed and written to the underlying device. Calling this function can reduce the compression ratio. The underlying device is not flushed.

Calling this function when QtIOCompressor is in ReadOnly mode has no effect.

bool QtIOCompressor::isGzipSupported ( )
static

Returns true if the zlib library in use supports the gzip format, false otherwise.

bool QtIOCompressor::isSequential ( ) const
bool QtIOCompressor::open ( OpenMode  mode)

Opens the QtIOCompressor in mode. Only ReadOnly and WriteOnly is supported. This functon will return false if you try to open in other modes.

If the underlying device is not opened, this function will open it in a suitable mode. If this happens the device will also be closed when close() is called.

If the underlying device is already opened, its openmode must be compatable with mode.

Returns true on success, false on error.

See also
close()
void QtIOCompressor::setStreamFormat ( StreamFormat  format)

Sets the format on the compressed stream to format.

See also
QtIOCompressor::StreamFormat
QtIOCompressor::StreamFormat QtIOCompressor::streamFormat ( ) const

Returns the format set on the compressed stream.

See also
QtIOCompressor::StreamFormat

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