零使用zlib。这个链接帮助我做到了。想分享这个,以便它可以帮助某人。就我而言,我直接使用该缓冲区而不是写入文件。
http://www.gamedev.net/reference/articles/article2279.asp
#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int main(int argc, char* argv[])
{
char c;
if ( argc != 2 )
{
cout << "Usage program.exe zipfilename" << endl;
return 0;
}
FILE * FileIn;
FILE * FileOut;
unsigned long FileInSize;
void *RawDataBuff;
//input and output files
FileIn = fopen(argv[1], "rb");
FileOut = fopen("FileOut.zip", "w");
//get the file size of the input file
fseek(FileIn, 0, SEEK_END);
FileInSize = ftell(FileIn);
//buffers for the raw and compressed data</span>
RawDataBuff = malloc(FileInSize);
void *CompDataBuff = NULL;
//zlib states that the source buffer must be at least 0.1 times larger than the source buffer plus 12 bytes
//to cope with the overhead of zlib data streams
uLongf CompBuffSize = (uLongf)(FileInSize + (FileInSize * 0.1) + 12);
CompDataBuff = malloc((size_t)(CompBuffSize));
//read in the contents of the file into the source buffer
fseek(FileIn, 0, SEEK_SET);
fread(RawDataBuff, FileInSize, 1, FileIn);
//now compress the data
uLongf DestBuffSize;
int returnValue;
returnValue = compress((Bytef*)CompDataBuff, (uLongf*)&DestBuffSize,
(const Bytef*)RawDataBuff, (uLongf)FileInSize);
cout << "Return value " << returnValue;
//write the compressed data to disk
fwrite(CompDataBuff, DestBuffSize, 1, FileOut);
fclose(FileIn);
fclose(FileOut);
errno_t err;
// Open for read (will fail if file "input.gz" does not exist)
if ((FileIn = fopen("FileOut.zip", "rb")) == NULL) {
fprintf(stderr, "error: Unable to open file" "\n");
exit(EXIT_FAILURE);
}
else
printf( "Successfully opened the file\n" );
cout << "Input file name " << argv[1] << "\n";
// Open for write (will fail if file "test.txt" does not exist)
if( (err = fopen_s( &FileOut, "test.txt", "wb" )) !=0 )
{
printf( "The file 'test.txt' was not opened\n" );
system ("pause");
exit (1);
}
else
printf( "The file 'test.txt' was opened\n" );
//get the file size of the input file
fseek(FileIn, 0, SEEK_END);
FileInSize = ftell(FileIn);
//buffers for the raw and uncompressed data
RawDataBuff = malloc(FileInSize);
char *UnCompDataBuff = NULL;
//RawDataBuff = (char*) malloc (sizeof(char)*FileInSize);
if (RawDataBuff == NULL)
{
fputs ("Memory error",stderr);
exit (2);
}
//read in the contents of the file into the source buffer
fseek(FileIn, 0, SEEK_SET);
fread(RawDataBuff, FileInSize, 1, FileIn);
//allocate a buffer big enough to hold the uncompressed data, we can cheat here
//because we know the file size of the original
uLongf UnCompSize = 482000; //TODO : Revisit this
int retValue;
UnCompDataBuff = (char*) malloc (sizeof(char)*UnCompSize);
if (UnCompDataBuff == NULL)
{
fputs ("Memory error",stderr);
exit (2);
}
//all data we require is ready so compress it into the source buffer, the exact
//size will be stored in UnCompSize
retValue = uncompress((Bytef*)UnCompDataBuff, &UnCompSize, (const Bytef*)RawDataBuff, FileInSize);
cout << "Return value of decompression " << retValue << "\n";
//write the decompressed data to disk
fwrite(UnCompDataBuff, UnCompSize, 1, FileOut);
free(RawDataBuff);
free(UnCompDataBuff);
fclose(FileIn);
fclose(FileOut);
system("pause");
exit (0);
}