zlib-0.3: Compression and decompression in the gzip and zlib formatsContentsIndex
Portabilityportable (H98 + FFI)

Compression and decompression of data streams in the gzip format.

The format is described in detail in RFC #1952: http://www.ietf.org/rfc/rfc1952.txt

See also the zlib home page: http://zlib.net/

compress :: ByteString -> ByteString
compressWith :: CompressionLevel -> ByteString -> ByteString
data CompressionLevel
= DefaultCompression
| NoCompression
| BestSpeed
| BestCompression
| CompressionLevel Int
decompress :: ByteString -> ByteString

This module provides pure functions for compressing and decompressing streams of data represented by lazy ByteStrings. This makes it easy to use either in memory or with disk or network IO.

For example a simple gzip compression program is just:

 import qualified Data.ByteString.Lazy as ByteString
 import qualified Codec.Compression.GZip as GZip

 main = ByteString.interact GZip.compress

Or you could lazily read in and decompress a .gz file using:

 content <- fmap GZip.decompress (readFile file)
compress :: ByteString -> ByteString

Compress a stream of data into the gzip format.

This uses the default compression level which favours a higher compression ratio over compression speed. Use compressWith to adjust the compression level.

compressWith :: CompressionLevel -> ByteString -> ByteString

Like compress but with an extra parameter to specify the compression level.

There are a number of additional compression parameters which are rarely necessary to change but if you need to you can do so using compressFull.

data CompressionLevel
Control amount of compression. This is a trade-off between the amount of compression and the time and memory required to do the compression.
DefaultCompressionThe default compression level is 6 (that is, biased towards high compression at expense of speed).
NoCompressionNo compression, just a block copy.
BestSpeedThe fastest compression method (less compression)
BestCompressionThe slowest compression method (best compression).
CompressionLevel IntA specific compression level between 1 and 9.
show/hide Instances
decompress :: ByteString -> ByteString

Decompress a stream of data in the gzip format.

There are a number of errors that can occur. In each case an exception will be thrown. The possible error conditions are:

  • if the stream does not start with a valid gzip header
  • if the compressed stream is corrupted
  • if the compressed stream ends permaturely

Note that the decompression is performed lazily. Errors in the data stream may not be detected until the end of the stream is demanded (since it is only at the end that the final checksum can be checked). If this is important to you, you must make sure to consume the whole decompressed stream before doing any IO action that depends on it.

Produced by Haddock version 0.8