mahmood
mahmood

Reputation: 24705

writing to gz using fstream

How can I write the output to a compressed file (gz, bz2, ...) using fstream? It seems that Boost library can do that, but I am looking for a non Boost solution. I saw example only for reading from a compressed file.

Upvotes: 2

Views: 2411

Answers (2)

Matt Kline
Matt Kline

Reputation: 10477

To write compressed data to a file, you would run your uncompressed data through a compression library such as zlib (for DEFLATE, the compression algorithm used with .zip and .gz files) or xz utils (for LZMA, the compression algorithm used with 7zip and .xz files), then write the result as usual using ofstream or fwrite.

Upvotes: 3

Brian Cain
Brian Cain

Reputation: 14619

The two major pieces to implement are the encoding/compression and framing/encapsulation/file format.

From wikipedia, the DEFLATE algorithm:

Stream format

A Deflate stream consists of a series of blocks. Each block is preceded by a 3-bit header: 1 bit: Last-block-in-stream marker: 1: this is the last block in the stream. 0: there are more blocks to process after this one. 2 bits: Encoding method used for this block type: 00: a stored/raw/literal section, between 0 and 65,535 bytes in length. 01: a static Huffman compressed block, using a pre-agreed Huffman tree. 10: a compressed block complete with the Huffman table supplied. 11: reserved, don't use. Most blocks will end up being encoded using method 10, the dynamic Huffman encoding, which produces an optimised Huffman tree customised for each block of data individually. Instructions to generate the necessary Huffman tree immediately follow the block header. Compression is achieved through two steps The matching and replacement of duplicate strings with pointers. Replacing symbols with new, weighted symbols based on frequency of use.

From wikipedia, the gzip file format:

"gzip" is often also used to refer to the gzip file format, which is: a 10-byte header, containing a magic number, a version number and a timestamp optional extra headers, such as the original file name, a body, containing a DEFLATE-compressed payload an 8-byte footer, containing a CRC-32 checksum and the length of the original uncompressed data

Upvotes: 2

Related Questions