Perform a gzip compression operation on the supplied chain.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | Gzip | (hzChain&,hzChain&,) |
Declared in file: hzCodec.h
Defined in file : hzCodec.cpp
Function Logic:
Function body:
hzEcode Gzip (hzChain& gzipped)hzChain& orig,
{
// Category: Codec
//
// Perform a gzip compression operation on the supplied chain.
//
// Arguments: 1) gzipped The hzChain reference that will contain the result. Any pre-existing contents of this are cleared at the outset.
// 2) orig The original hzChain. This is not altered by this operation.
//
// Returns: E_NODATA If the original is empty
// E_NOINIT If there is an error in conversion
// E_OK If the operation is successful
_hzfunc(__func__) ;
chIter zi ; // Chain iterator for input
z_stream defstream; // The zlib struct
char* tmp ; // A string to convert chain to contiguous memory
char* buf ; // Buffer to construct gzipped content
int32_t err ; // Return code from init
gzipped.Clear() ;
if (!orig.Size())
return E_NODATA ;
// Deflate original data
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
// setup tmp as the input and buf as the compressed output
tmp = new char[orig.Size()+1];
buf = new char[orig.Size()] ;
// for (n = 0, zi = orig ; !zi.eof() ; n++, zi++)
// tmp[n] = *zi ;
// tmp[n] = 0 ;
zi = orig ;
zi.Write(tmp, orig.Size()) ;
tmp[orig.Size()] = 0;
defstream.avail_in = (uInt) orig.Size() ;
defstream.next_in = (Bytef*) tmp;
defstream.avail_out = (uInt) orig.Size() ;
defstream.next_out = (Bytef*) buf;
// Initialize the zlib deflation (i.e. compression) internals with deflateInit2().
//
// The parameters are as follows:
//
// z_streamp strm - Pointer to a zstream struct
//
// int level - Compression level. Must be Z_DEFAULT_COMPRESSION, or between 0 and 9: 1 gives best speed, 9 gives best compression, 0 gives no compression.
//
// int method - Compression method. Only method supported is "Z_DEFLATED".
//
// int windowBits - Base two logarithm of the maximum window size (the size of the history buffer). It should be in the range 8-15. Add 16 to windowBits to write a simple
// gzip header and trailer around the compressed data instead of a zlib wrapper. The gzip header will have no file name, no extra data, no comment, no mod
// time (set to zero), no header crc, and the operating system will be set to 255 (unknown).
//
// int memLevel - Amount of memory allocated for internal compression state. 1 uses minimum memory but is slow and reduces compression ratio; 9 uses maximum memory for
// optimal speed. Default value is 8.
//
// int strategy - Used to tune the compression algorithm. Use the value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a filter (or predictor), or
// Z_HUFFMAN_ONLY to force Huffman encoding only (no string match)
err = deflateInit2(&defstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31,8,Z_DEFAULT_STRATEGY);
if (err != Z_OK)
return hzerr(E_NOINIT, "Could not run deflate process") ;
// the actual compression work.
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);
// for (n = 0 ; n < defstream.total_out ; n++)
// gzipped.AddByte(buf[n]) ;
gzipped.Append(buf, defstream.total_out) ;
delete tmp ;
delete buf ;
if (!gzipped.Size())
return hzerr(E_NODATA, "Input %d bytes but no output", orig.Size()) ;
return E_OK ;
}