///////////////////////////////////////////////////////////////////////////// // Creation 23/12/2003 // // // BZ2TEST.C // --------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // simple bz2 usage // ///////////////////////////////////////////////////////////////////////////// #include #include "bzlib.h" int bz2CompressData( const char * pszFileSource, const char * pszFileDest ) { //Here's how you'd write data to a compressed file: FILE* f, *fSource; BZFILE* b; int nBuf; char *buf; int bzerror; // int nWritten; int abandon = 1; unsigned int nbytes_in; unsigned int nbytes_out; // Open source and write all data in buf fSource = fopen( pszFileSource, "rb" ); if( ! fSource ) { return -1; } fseek( fSource, 0, SEEK_END ); nBuf = ftell(fSource ); fseek( fSource, 0, SEEK_SET ); buf = (char * )malloc( nBuf ); if( ! buf ) { fclose( fSource ); return -1; } fread( buf, 1, nBuf, fSource ); fclose( fSource ); // bz2 file f = fopen ( pszFileDest, "wb" ); if (!f) { /* handle error */ free( buf ); return -1; } b = BZ2_bzWriteOpen ( &bzerror, f, 9, 0, 30); if (bzerror != BZ_OK) { abandon = 1; BZ2_bzWriteClose ( &bzerror, b, abandon, &nbytes_in, &nbytes_out ); /* handle error */ free( buf ); return -1; } //while ( /* condition */ ) { /* get data to write into buf, and set nBuf appropriately */ /*nWritten = */ BZ2_bzWrite ( &bzerror, b, buf, nBuf ); if (bzerror == BZ_IO_ERROR) { abandon = 1; BZ2_bzWriteClose ( &bzerror, b, abandon, &nbytes_in, &nbytes_out ); /* handle error */ free( buf ); return -1; } } abandon = 0; // BZ2_bzclose( b ); BZ2_bzWriteClose ( &bzerror, b, abandon, NULL, NULL ); // &nbytes_in, &nbytes_out ); if (bzerror == BZ_IO_ERROR) { /* handle error */ free( buf ); return -1; } free( buf ); return 0; } int bz2DecompressData( const char * pszFileSource, const char * pszFileDest ) { #if 1 // And to read from a compressed file: FILE* f, *fDest; BZFILE* b; int nBuf; char buf[8096]; int bzerror; int nWritten; fDest = fopen ( pszFileDest, "wb" ); if( ! fDest ) { return -1; } f = fopen ( pszFileSource, "rb" ); if (!f) { /* handle error */ fclose( fDest ); return -1; } b = BZ2_bzReadOpen ( &bzerror, f, 0, 4, NULL, 0 ); if (bzerror != BZ_OK) { BZ2_bzReadClose ( &bzerror, b ); /* handle error */ fclose( fDest ); return -1; } bzerror = BZ_OK; while (bzerror == BZ_OK //&& /* arbitrary other conditions */ ) { nBuf = BZ2_bzRead ( &bzerror, b, buf, sizeof(buf) ); if (bzerror == BZ_OK) { /* do something with buf[0 .. nBuf-1] */ fwrite( buf, 1, nBuf, fDest ); } } if (bzerror != BZ_STREAM_END) { BZ2_bzReadClose ( &bzerror, b ); /* handle error */ fclose( fDest ); return -1; } else { BZ2_bzReadClose ( &bzerror, b ); } fclose( fDest ); #endif return 0; } int main( int argc, char * argv[] ) { int fCompress; if( argc != 4 ) { printf( "Usage: %s \n", argv[0] ); return 1; } fCompress = argv[1][0] == 'c'; if( fCompress ) { bz2CompressData( argv[2], argv[3] ); } else { bz2DecompressData( argv[2], argv[3] ); } return 0; }