home *** CD-ROM | disk | FTP | other *** search
- /* Xceed Streaming Compression Library - Compress Sample Application
- * Copyright (c) 2001 Xceed Software Inc.
- *
- * [Compress.cpp]
- *
- * This console application shows how to compress a file, using
- * different compression data formats. It specifically demonstrates:
- * - The WriteFile and ProcessFile methods.
- * - The CompressionFormat property.
- *
- * This file is part of the Xceed Streaming Compression Library sample
- * applications. The source code in this file is only intended as
- * a supplement to Xceed Streaming Compression Library's documentation,
- * and is provided "as is", without warranty of any kind, either
- * expressed or implied.
- */
-
- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- #include <limits.h>
- #include "Compress.h"
-
- //
- // Mapping between command-line and property values
- //
-
- static SCompressionFormat g_pxCompressionFormats[] =
- {
- { "=Std", cfStandard },
- { "=BZip2", cfBZip2 },
- { "=GZip", cfGZip },
- { "=Zip3", cfZip3 },
- { "=ZLib", cfZLib },
- { "=BWT", cfBWT },
- { "=Deflate", cfDeflate },
- { "=Store", cfStore },
- };
-
-
- //--------------------------------------------------------------------------
- // Entry point of the application
- //--------------------------------------------------------------------------
- int main(int argc, char* argv[])
- {
- //Initializes the COM library on the current thread following the STA model
- CoInitialize( NULL );
-
- try
- {
- // Create an instance of the XceedStreamingCompression coclass, and
- // use the "I" interface which manipulates byte arrays (instead of
- // Variants for the "D" interfaces).
- IXceedStreamingCompressionPtr piComp;
- piComp.CreateInstance( CLSID_XceedStreamingCompression );
-
- // Two BSTR variables that will contain the Input and Output file names.
- _bstr_t bstrInputFileName;
- _bstr_t bstrOutputFileName;
-
- // Extract the command line parameters and initialize the Compression
- // instance according to the user specification. After this call
- // the Compression instance piComp is ready to compress.
- if( !ExtractParameters( argc, argv,
- piComp,
- bstrInputFileName,
- bstrOutputFileName ) )
- {
- ShowHelp();
- return 1;
- }
-
- if( bstrInputFileName.length() == 0 )
- {
- // An input file name was not provided by the user.
- // Get the text to compress from the console
-
- printf( "Compressing the console input. Press Ctrl-Z and Enter when done.\n\n" );
-
- // Make sure to overwrite the file. We first call the WriteFile method without
- // any source data, and with the bAppend parameter set to false.
- DWORD dwBytesWritten = 0;
- piComp->WriteFile( NULL, 0, cfpCompress, FALSE, bstrOutputFileName, FALSE, &dwBytesWritten );
-
- char pcBuffer[ BUFFER_SIZE ];
-
- while( !feof( stdin ) )
- {
- int nRead = fread( pcBuffer, sizeof( char ), BUFFER_SIZE, stdin );
-
- if( nRead )
- {
- // Compress the characters read so far in the output file.
- // We keep calling the WriteFile method with the bEndOfData parameter
- // set to FALSE. This way, all the data we keep feeding the compressor
- // is considered a unique stream of data, and it compresses more.
- piComp->WriteFile( ( BYTE* )pcBuffer, nRead, cfpCompress, FALSE,
- bstrOutputFileName, TRUE, &dwBytesWritten );
- }
- }
-
- // Since we always called with bEndOfData to FALSE, the compressor may be
- // holding some more data, so we must make sure to flush the remaining data.
- piComp->WriteFile( NULL, 0, cfpCompress, TRUE, bstrOutputFileName, TRUE, &dwBytesWritten );
-
- printf( "Successfully compressed the console input to file %s\n", ( const char* )bstrOutputFileName );
- }
- else
- {
- // An input file name was provided by the user.
- // Get the text to compress from this file
-
- DWORD dwBytesRead = 0;
- DWORD dwBytesWritten = 0;
-
- // Compress by reading a file and sending the output to another file.
- // We simply call the ProcessFile method once, providing:
- // - The source filename, without any offset or size, since we want to
- // compress it all.
- // - The type of processing we want to perform, in this case cfpCompress.
- // - This is our last (and first!) call to the method, so bEndOfData is
- // FALSE.
- // - The destination filename, with the bAppend parameter set to FALSE,
- // since we want to overwrite any existing file.
- // - The address of two DWORD which will contain the number of bytes
- // read and written on return.
- piComp->ProcessFile( bstrInputFileName, 0, 0, cfpCompress, TRUE,
- bstrOutputFileName, FALSE,
- &dwBytesRead, &dwBytesWritten );
-
- printf( "Successfully compressed file %s [%d] to file %s [%d]\n",
- ( const char* )bstrInputFileName, dwBytesRead,
- ( const char* )bstrOutputFileName, dwBytesWritten );
- }
- }
- catch( const _com_error& err )
- {
- // When using the "#import" directive, the compiler generates wrapper classes
- // around all interface types. These wrapper classes throw exceptions when
- // a method call returns an HRESULT which is a failure code.
- printf( "Error %08x: %s\n", err.Error(), ( const char* )err.Description() );
- }
- catch( ... )
- {
- // Catch any other exception.
- printf( "An unknown error occured.\n" );
- }
-
- // Close the COM library for the current thread
- CoUninitialize();
-
- return 0;
- }
-
- //--------------------------------------------------------------------------
- // Display usage information
- //--------------------------------------------------------------------------
- void ShowHelp()
- {
- // "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
- printf( "Usage: Compress [options] [input_file] output_file\n\n"
- " input_file: the file to compress\n"
- " output_file: the destination file\n\n"
- " options: /m=[Std | BZip2 | GZip | Zip3 | ZLib | BWT | Deflate | Store]\n"
- " This is the compression format. The default is 'Std'\n"
- " /h or /? : Show this help\n" );
- }
-
- //--------------------------------------------------------------------------
- // Extract commands from the command line parameters
- //
- // In this function, we call the piComp interface and let exceptions
- // be caught by the caller.
- // This function returns false if an error occured parsing the command
- // line parameters or if the user requested help.
- //--------------------------------------------------------------------------
- bool ExtractParameters( int argc, char* argv[],
- IXceedStreamingCompressionPtr piComp,
- _bstr_t& bstrInputFileName,
- _bstr_t& bstrOutputFileName )
- {
- // We set pxFormat to the first compression format of the global
- // compression format array in case the user don't specify any.
- SCompressionFormat* pxFormat = g_pxCompressionFormats;
- bool bFound;
-
- int i = 0;
-
- // We parse each command line parameter
- while( ++i < argc )
- {
- if( argv[ i ][ 0 ] == '/' )
- {
- // The parameter starts with a /
- // Meaning it's an option parameter
- switch( argv[ i ][ 1 ] )
- {
- case 'm':
- case 'M':
- // The user wants to set the compression method
- bFound = false;
-
- // We go through all the compression methods in the
- // correspondence table, stopping when we find a
- // match.
- for( pxFormat = g_pxCompressionFormats;
- pxFormat->pszCommandLine != NULL;
- pxFormat++ )
- {
- if( lstrcmpi( argv[ i ] + 2, pxFormat->pszCommandLine ) == 0 )
- {
- bFound = true;
- break;
- }
- }
-
- if( !bFound )
- {
- printf( "Invalid compression format '%s'\n\n", argv[ i ] );
- return false;
- }
-
- break;
-
- default:
- printf( "Unknown command '%s'\n\n", argv[ i ] );
- // Continue
- case 'h':
- case 'H':
- case '?':
- return false;
- }
- }
- }
-
- if( argc < 2 || argv[ argc - 1 ][ 0 ] == '/' )
- {
- printf( "You did not specify an output filename\n\n" );
- return false;
- }
- else if( argc < 3 || argv[ argc - 2 ][ 0 ] == '/' )
- {
- // Only an output file was specified
- bstrOutputFileName = argv[ argc - 1 ];
- }
- else
- {
- // Both input and output file were specified
- bstrInputFileName = argv[ argc - 2 ];
- bstrOutputFileName = argv[ argc - 1 ];
- }
-
- // Set the compression format property of the piComp.
- // - We create the corresponding format instance.
- // - We optionally set its properties (none in this sample).
- // - We assign it to the CompressionFormat property of the main
- // compression object. Since this property is of type
- // "IXceedCompressData*", we use the wrapper class to perform
- // a QueryInterface on our format instance.
-
- switch( pxFormat->eFormat )
- {
- case cfBZip2 :
- {
- IXceedBZip2CompressionFormatPtr piFormat;
-
- piFormat.CreateInstance( CLSID_XceedBZip2CompressionFormat );
- piComp->CompressionFormat = IXceedCompressDataPtr( piFormat );
- }
- break;
-
- case cfGZip :
- {
- IXceedGZipCompressionFormatPtr piFormat;
-
- piFormat.CreateInstance( CLSID_XceedGZipCompressionFormat );
- piComp->CompressionFormat = IXceedCompressDataPtr( piFormat );
- }
- break;
-
- case cfStandard :
- {
- IXceedStandardCompressionFormatPtr piFormat;
-
- piFormat.CreateInstance( CLSID_XceedStandardCompressionFormat );
- piComp->CompressionFormat = IXceedCompressDataPtr( piFormat );
- }
- break;
-
- case cfZip3 :
- {
- IXceedZip3CompressionFormatPtr piFormat;
-
- piFormat.CreateInstance( CLSID_XceedZip3CompressionFormat );
- piComp->CompressionFormat = IXceedCompressDataPtr( piFormat );
- }
-
- case cfZLib :
- {
- IXceedZLibCompressionFormatPtr piFormat;
-
- piFormat.CreateInstance( CLSID_XceedZLibCompressionFormat );
- piComp->CompressionFormat = IXceedCompressDataPtr( piFormat );
- }
- break;
-
- // The next three items are not compression formats. They are compression
- // methods. They still can be assigned to the CompressionFormat property
- // of the XceedStreamingCompression object. In this case, the resulting
- // compressed streams will have no formating (no header, footer, checksum, ...)
- case cfBWT :
- {
- IXceedBWTCompressionMethodPtr piMethod;
-
- piMethod.CreateInstance( CLSID_XceedBWTCompressionMethod );
- piComp->CompressionFormat = IXceedCompressDataPtr( piMethod );
- }
- break;
-
- case cfDeflate :
- {
- IXceedDeflateCompressionMethodPtr piMethod;
-
- piMethod.CreateInstance( CLSID_XceedDeflateCompressionMethod );
- piComp->CompressionFormat = IXceedCompressDataPtr( piMethod );
- }
- break;
-
- case cfStore :
- {
- // Using Store as the compression format will produce an output
- // compressed stream identical to the source to compress!
- IXceedStoreCompressionMethodPtr piMethod;
-
- piMethod.CreateInstance( CLSID_XceedStoreCompressionMethod );
- piComp->CompressionFormat = IXceedCompressDataPtr( piMethod );
- }
- break;
- }
-
- return true;
- }
-
-
- //
- // END_OF_FILE
- //
-