Support for compression streams

The ZStream unit implements a TStream descendent () which uses the deflate algorithm to compress everything that is written to it. The compressed data is written to the output stream, which is specified when the compressor class is created.

Likewise, a TStream descendent is implemented which reads data from an input stream () and decompresses it with the inflate algorithm.

Used for exception support. Used for the TStream declaration Support for the deflate algorithm Base exception for exceptions in the ZStream unit Errors which occur in the zstream unit are signaled by raising an EZLibError exception descendent. Errors in the TCompressionStream class are signaled with this exception ECompressionError is the exception class used by the class. Errors in the TDeCompressionStream class are signaled with this exception EDecompressionError is the exception class used by the class. Ancestor for compression and decompression classes

TCustomZlibStream serves as the ancestor class for the and classes.

It introduces support for a progess handler, and stores the input or output stream.

Create a new instance of TCustomZlibStream Create creates a new instance of TCustomZlibStream. It stores a reference to the input/output stream, and initializes the deflate compression mechanism so they can be used by the descendents. Instance of the Input/Output stream Compression level for the deflate algorithm Do not use compression, just copy data. Use fast (but less) compression. Use default compression Use maximum compression Write-only stream class which compresses data written to it. TCompressionStream Create a new instance of the compression stream.

Create creates a new instance of the compression stream. It merely calls the inherited constructor with the destination stream Dest and stores the compression level.

If ASkipHeader is set to True, the method will not write the block header to the stream. This is required for deflated data in a zip file.

Note that the compressed data is only completely written after the compression stream is destroyed.

Destroy
Compression level to use when writing data to the stream Output stream to which compressed data is written. If set, the deflated data block header is not written to stream Flushe data to the output stream and destroys the compression stream. Destroy flushes the output stream: any compressed data not yet written to the output stream are written, and the deflate structures are cleaned up. None. Create Overridden to raise an exception. The Read method of TStream is overridden, and always raises an exception, because TCompressionStream is write-only. An exception is raised. Number of bytes actually read Buffer to read data to Number of bytes to read Write data to the stream Write takes Count bytes from Buffer and comresseses (deflates) them. The compressed result is written to the output stream. If an error occurs, an exception is raised. Read Seek Number of (uncompressed) bytes actually written Buffer to take data from. Number of bytes to take from the buffer. Overrides seek to raise an exception. The Seek method of TStream is overridden, and always raises an exception, because TCompressionStream is write-only, and cannot seek. An exception is raised. Read Write New location after seek Offset to move stream position Origin of seek operation. Running compression rate of compression stream The Compressionrate is updated as more data is written to the stream and represents the ratio of outputted data versus written data. Write Progress handler OnProgress is called whenever output data is written to the output stream. It can be used to update a progress bar or so. The Sender argument to the progress handler is the compression stream instance. Reads and decompresses data from a compressed data stream.

TDecompressionStream performs the inverse operation of . A read operation reads data from an input stream and decompresses (inflates) the data it as it goes along.

The decompression stream reads it's compressed data from a stream with deflated data. This data can be created e.g. with a compression stream.

Creates a new instance of the TDecompressionStream stream

Create creates and initializes a new instance of the TDecompressionStream class. It calls the inherited Create and passes it the Source stream. The source stream is the stream from which the compressed (deflated) data is read.

If ASkipHeader is true, then the gzip data header is skipped, allowing TDecompressionStream to read deflated data in a .zip file. (this data does not have the gzip header record prepended to it).

Note that the source stream is by default not owned by the decompression stream, and is not freed when the decompression stream is destroyed.

Destroy
Stream with compressed data Should the header data be skipped. Destroys the TDecompressionStream instance

Destroy cleans up the inflate structure, and then simply calls the inherited destroy.

By default the source stream is not freed when calling Destroy.

Create
Read data from the compressed stream Read will read data from the compressed stream until the decompressed data size is Count or there is no more compressed data available. The decompressed data is written in Buffer. The function returns the number of bytes written in the buffer. If an error occurs, an exception is raised. Write Number of decompressed bytes that were actually read Buffer in which to store the read data Maximum number of bytes to read. Write data to the stream Write will raise a exception, because the TDecompressionStream class is read-only. An exception is always raised. Read Number of bytes written Buffer containing the data to be written Number of bytes to write Move stream position to a certain location in the stream.

Seek overrides the standard Seek implementation. There are a few differences between the implementation of Seek in Free Pascal compared to Delphi:

  • In Free Pascal, you can perform any seek. In case of a forward seek, the Free Pascal implementation will read some bytes until the desired position is reached, in case of a backward seek it will seek the source stream backwards to the position it had at the creation time of the TDecompressionStream and then again read some bytes until the desired position has been reached.
  • In Free Pascal, a seek with soFromBeginning will reset the source stream to the position it had when the TDecompressionStream was created. In Delphi, the source stream is reset to position 0. This means that at creation time the source stream must always be at the start of the zstream, you cannot use TDecompressionStream.Seek to reset the source stream to the begin of the file.
An exception is raised if the stream does not allow the requested seek operation. Read
New position in the stream Offset to apply to the position in the stream Origin from where Offset should be counted. Progress handler OnProgress is called whenever input data is read from the source stream. It can be used to update a progress bar or so. The Sender argument to the progress handler is the decompression stream instance. Open mode for gzip file. Open file for reading Open file for writing Stream to read/write from a gzip file. TGZFileStream can be used to read data from a gzip file, or to write data to a gzip file. Create a new instance of TGZFileStream

Create creates a new instance of the TGZFileStream class. It opens FileName for reading or writing, depending on the FileMode parameter. It is not possible to open the file read-write. If the file is opened for reading, it must exist.

If the file is opened for reading, the method can be used for reading the data in uncompressed form.

If the file is opened for writing, any data written using the method will be stored in the file in compressed (deflated) form.

If the file is not found, an exception is raised. Destroy TGZOpenMode
Name of the gzip file Open mode for the file. Removes TGZFileStream instance Destroy closes the file and releases the TGZFileStream instance from memory. Create Read data from the compressed file

Read overrides the Read method of TStream to read the data from the compressed file. The Buffer parameter indicates where the read data should be stored. The Count parameter specifies the number of bytes (uncompressed) that should be read from the compressed file. Note that it is not possible to read from the stream if it was opened in write mode.

The function returns the number of uncompressed bytes actually read.

If Buffer points to an invalid location, or does not have enough room for Count bytes, an exception will be raised. Create Write Seek
Number of bytes (uncompressed) actually read Memory buffer to store the data in Number of (uncompressed) bytes to read. Write data to be compressed

Write writes Count bytes from Buffer to the compressed file. The data is compressed as it is written, so ideally, less than Count bytes end up in the compressed file. Note that it is not possible to write to the stream if it was opened in read mode.

The function returns the number of (uncompressed) bytes that were actually written.

In case of an error, an exception is raised. Create Read Seek
Number of uncompressed bytes written Memory location containing the data to be written. Number of bytes to write. Set the position in the compressed stream. Seek sets the position to Offset bytes, starting from Origin. Not all combinations are possible, see for a list of possibilities. In case an impossible combination is asked, an exception is raised. New position in the stream Number of bytes to position on Where to start positioning Source or target stream Clear up instance Destroy cleans up the internal memory buffer and calls the inherited destroy. Compression level for compression stream Flush remaining data to the target stream flush writes any remaining data in the memory buffers to the target stream, and clears the memory buffer. Get the current compression rate get_compressionrate returns the percentage of the number of written compressed bytes relative to the number of written bytes. If no bytes were written, an exception is raised. Percentage of compression Get the current compression rate get_compressionrate returns the percentage of the number of read compressed bytes relative to the total number of read bytes. If no bytes were written, an exception is raised. Percentage of compression Exception class used in Tgzfilestream class Egzfileerror is the exception class used to report errors by the class.