Deflate

From Crypto++ Wiki
Jump to navigation Jump to search
Deflator
Documentation
#include <cryptopp/zdeflate.h>

Deflate is a lossless compression format standardized in RFC 1951, DEFLATE Compressed Data Format Specification. Crypto++ provides Deflate compression through the Deflator class, and decompression though the Inflator class.

The Deflator compressor takes a pointer to a BufferedTransformation. Because a pointer is taken, the Deflator owns the attached transformation, and therefore will destroy it. See ownership for more details.

The Gzip compression format uses the Deflate algorithm. Also see RFC 1952.

When writing a deflated file to disk, deflate files typically uses the *.zz extension. Also see RFC 1951.

Construction

Deflator (BufferedTransformation *attachment=NULL,
          unsigned int deflateLevel=DEFAULT_DEFLATE_LEVEL,
          unsigned int log2WindowSize=DEFAULT_LOG2_WINDOW_SIZE,
          bool detectUncompressible=true)

Deflator (const NameValuePairs &parameters,
          BufferedTransformation *attachment=NULL)

attachment is a BufferedTransformation, such as another filter or sink. If attachment is NULL, then the Deflator object will internally accumulate the output byte stream.

deflateLevel is the deflation level. The value should be between 0 and 9. 0 provides minimum compression and executes quickly, while 9 provides maximum compression and executes the slowest. zdeflate.h provides some constants for the deflateLevel. MIN_DEFLATE_LEVEL is 0, DEFAULT_DEFLATE_LEVEL is 6, and MAX_DEFLATE_LEVEL is 9.

log2WindowSize controls the table size used for compression. The value should be between 9 and 15, meaning the table will be between 29 and 215. 9 provides the smallest table size, while 15 provides the largest table size. zdeflate.h provides some constants for the log2WindowSize. MIN_LOG2_WINDOW_SIZE is 9, DEFAULT_LOG2_WINDOW_SIZE is 15, and MAX_LOG2_WINDOW_SIZE is 15.

detectUncompressible means the library should try to detect if a file is uncompressible. From zdeflate.h, detectUncompressible makes it faster to process uncompressible files, but if a file has both compressible and uncompressible parts, it may fail to compress some of the compressible parts.

parameters are NameValuePairs used in the alternate constructor. The names recognized are Log2WindowSize, DeflateLevel and DetectUncompressible.

Sample Programs

The following is a small collection of sample programs to demonstrate using the Deflator compressor.

In-memory String

std::string data = "abcdefghijklmnopqrstuvwxyz";
std::string compressed;

Deflator deflator(new StringSink(compressed));
deflator.Put((const byte*) data.data(), data.size());
deflator.MessageEnd();

On-disk File

Deflate files typically use the *.zz extension.

std::string filename("test.txt.zz");
std::string data = "abcdefghijklmnopqrstuvwxyz";

Deflator deflator(new FileSink(filename.c_str(), true));
deflator.Put((const byte*) data.data(), data.size());
deflator.MessageEnd();

String using Pipeline

std::string data = "abcdefghijklmnopqrstuvwxyz";
std::string compressed;
        
StringSource ss(data, true,
    new Deflator(
        new StringSink(compressed)
));

File using Pipeline

std::string filename("test.txt.gz");
std::string data = "abcdefghijklmnopqrstuvwxyz";
        
StringSource ss(data, true,
    new Deflator(
        new FileSink(filename.c_str(), true)
));

String using Put/Get

Deflator deflator;
deflator.Put((const byte*)data.data(), data.size());
deflator.MessageEnd();
        
word64 avail = deflator.MaxRetrievable();
if(avail)
{
    string compressed;
    compressed.resize(avail);
            
    deflator.Get((byte*)&compressed[0], compressed.size());
}

Array using Put/Get

Deflator deflator;
deflator.Put((const byte*)data.data(), data.size());
deflator.MessageEnd();
        
word64 avail = deflator.MaxRetrievable();
if(avail)
{
    vector<byte> compressed;
    compressed.resize(avail);
            
    deflator.Get(&compressed[0], compressed.size());
}

Downloads

No downloads available.