///////////////////////////////////////////////////////////////////////////// // Creation 12/12/2003 // // // TESTUNZIP.C // ----------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // Show how to unizp a file, using the zlib and the unzip.c file // // Usage: testunzip // The files are extracted in the current folder // // Links: // -http://www.winimage.com/zLibDll/unzip.htm // -http://www.zlib.org/ // ///////////////////////////////////////////////////////////////////////////// #include #include #include #include "unzip.h" #ifndef MAX_PATH #define MAX_PATH 1024 #endif int main( int argc, char * argv[] ) { char szZipFile[MAX_PATH] = {0}; unzFile unzf; if( argc < 2 ) { printf( "Usage: %s \n" ); return 1; } strcpy( szZipFile, argv[1] ); // Open zip file unzf = unzOpen( szZipFile ); if( unzf == NULL ) { printf( "Can't open zip file\n" ); return 1; } // First file if( unzGoToFirstFile(unzf) != UNZ_OK ) { printf( "unzGoToFirstFile() error\n" ); return 1; } // Loop do { char szFileName[MAX_PATH]; unz_file_info file_info; char * pBuffer; if( unzGetCurrentFileInfo(unzf, &file_info, szFileName, MAX_PATH, NULL, 0, NULL, 0) == UNZ_OK ) { // Print some infos printf( "File: %s, compressed_size=%d, uncompressed_size=%d\n", szFileName, file_info.compressed_size, file_info.uncompressed_size ); // Unzip this file in current folder pBuffer = (char *)malloc( file_info.uncompressed_size ); if( pBuffer ) { FILE * pFile; // Read the content into memory unzOpenCurrentFile( unzf ); unzReadCurrentFile( unzf, (void*)pBuffer, file_info.uncompressed_size ); unzCloseCurrentFile( unzf ); // Save memory in a file pFile = fopen( szFileName, "wb" ); if( pFile ) { fwrite( (void*) pBuffer, 1, file_info.uncompressed_size, pFile ); fclose( pFile ); } else { printf( "Can't open file '%s'\n", szFileName ); } free( pBuffer ); } else { printf( "Can't allocate %d bytes of memory\n", file_info.uncompressed_size ); } } } while(unzGoToNextFile(unzf) == UNZ_OK); // Close zip file unzClose( unzf ); return 0; }