Compressing and Decompressing File - Zip Format

The Zip Compression Library, EZLib provides stream and file compression and decompression functionality for the Symbian platforms. The files can be compressed to two formats namely zip and gzip. Compression and Decompression are performed iteratively, till the completion of required task.

  1. Pass the input and output files as parameters to construct an instance of CEZFileBufferManager. This returns a pointer to CEZFileBufferManager (input buffer). CEZFileBufferManager provides functions that accept and converts the data into buffers for MEZBufferManager class to manage it internally.

  2. Pass the input buffer and other compression parameters to the object of the CEZCompressor class.

  3. Use CEZCompressor::DeflateL() to compress the file.

The following code demonstrates a compression procedure.

/*
 * Compresses the file into a zip file using the EZlib component. 
 * It is assumed that the input and output file names are contained in a 
 * provided ini file.
 */


void CEZlibEZipTests::DoEZlibDeflateL()
    {
RFs iFs;
 iFS.connect();
    


    //Open input file
_LIT(KInputFileLocation, "c:\\private\\E80000B7\\zip\\input\\input.doc");
TFileName inputFile(KInputFileLocation);
    RFile input;
    err = input.Open(iFs, inputFile, EFileStream | EFileRead | EFileShareExclusive);
    if(err != KErrNone)
        {
        INFO_PRINTF1(KOpenFileError);
        User::Leave(err);
        }   
    
    

    //open output file

_LIT(KOutputFileLocation, "c:\\private\\E80000B7\\zip\\input\\output.zip");
TFileName outputFile(KOutputFileLocation);
    RFile output;
    err = output.Replace(iFs, outputFile, EFileStream | EFileWrite | EFileShareExclusive);
    if(err != KErrNone)
        {
        INFO_PRINTF1(KCreateFileError);
        User::Leave(err);
        }   
        


    CEZFileBufferManager *bufferManager = CEZFileBufferManager::NewLC(input, output);   
    CEZCompressor *compressor = CEZCompressor::NewLC(*bufferManager, aLevel, aWindowBits, aMemLevel, aStrategy);

    while(compressor->DeflateL())
        {       
        }   
     output.Close(); 
input.Close();
 iFS.Close();
    

    
    }

The decompression of a .zip file can be achieved through the following the steps:

  1. Pass the input and output files as parameters to construct an instance of CEZFileBufferManager. This returns a pointer to CEZFileBufferManager ( buffer).

  2. Pass the buffer to the object of the CEZDecompressor class.

  3. CEZDecompressor::InflateL() is called repeatedly until the decompression is complete.

The following code demonstrates the decompression procedure.

/* Decompresses the file contained in a Zip file using the EZlib component. 
 * It is assumed that the input and output file names are contained in a 
 * provided ini file.
 */

void CEZlibEZipTests::DoEZlibInflateL()
    {
RFs iFs;
 iFS.connect();

//Open input file
_LIT(KInputFileLocation, "c:\\private\\E80000B7\\zip\\input\\output.zip");
TFileName inputFile(KInputFileLocation);
    RFile input;
    err = input.Open(iRfs, inputFile, EFileStream | EFileRead | EFileShareExclusive);
    if(err != KErrNone)
        {
        INFO_PRINTF1(KOpenFileError);
        User::Leave(err);
        }    
    
    

    //open output file

_LIT(KOutputFile, "c:\\private\\E80000B7\\zip\\input\\input.doc");
TFileName outputFile(KOutputFile);
    RFile output;
    err = output.Replace(iFs, outputFile, EFileStream | EFileWrite | EFileShareExclusive);
    if(err != KErrNone)
        {
        INFO_PRINTF1(KCreateFileError);
        User::Leave(err);
        }    
        


CEZFileBufferManager *bufferManager = CEZFileBufferManager::NewLC(input, Output);    
CEZDecompressor *decompressor = CEZDecompressor::NewLC(*bufferManager, aWindowBits);


    while(decompressor->InflateL())
        {        
        }    
output.Close();
        input.Close();
    ifs.Close();

    }