显示标签为“zlib”的博文。显示所有博文
显示标签为“zlib”的博文。显示所有博文

2008年1月8日星期二

几个关于zlib的小程序

#include
#include "/zlib/include/zlib.h"

// Demonstration of zlib utility functions

unsigned long file_size(char *filename)
{
FILE *pFile = fopen(filename, "rb");
fseek (pFile, 0, SEEK_END);
unsigned long size = ftell(pFile);
fclose (pFile);
return size;

}

int decompress_one_file(char *infilename, char *outfilename)
{
gzFile infile = gzopen(infilename, "rb");
FILE *outfile = fopen(outfilename, "wb");
if (!infile || !outfile) return -1;

char buffer[128];
int num_read = 0;
while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0)
{
fwrite(buffer, 1, num_read, outfile);

}

gzclose(infile);
fclose(outfile);

}

int compress_one_file(char *infilename, char *outfilename)
{
FILE *infile = fopen(infilename, "rb");
gzFile outfile = gzopen(outfilename, "wb");
if (!infile || !outfile) return -1;

char inbuffer[128];
int num_read = 0;
unsigned long total_read = 0, total_wrote = 0;
while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0)
{
total_read += num_read;
gzwrite(outfile, inbuffer, num_read);

}
fclose(infile);
gzclose(outfile);

printf("Read %ld bytes, Wrote %ld bytes,
Compression factor %4.2f%%\n",
total_read, file_size(outfilename),
(1.0-file_size(outfilename)*1.0/total_read)*100.0);

}


int main(int argc, char **argv)
{
compress_one_file(argv[1],argv[2]);
decompress_one_file(argv[2],argv[3]);

}

zlib dll包的使用

将zlib.h,zconf.h,zlib.lib三个文件拷到当前project的目录下,在stdafx.h中加入:
#define ZLIB_DLL
#include "zlib.h"//zconf.h

编译后,在c:\windows\system32中会自动生成一个zlib.dll文件。

注意:zlib version ist 1.1.3

zlib(integrity check)解压文件完整性检查 方案一:(未决)

压缩包文件: apollo.mdb.gz
解压文件: apollo.mdb
目的: 测试解压后的文件是否完整

方案一:
将apollo.mdb压入一个临时.gz文件apollo_tmp.mdb.gz,通过gzread()将apollo.mdb.gz与apollo.mdb进行比较。

代码:
BOOL CPublic::checkZippedFile(LPCTSTR sNameZipped, CString sNameExpand)
{
CString tempFile(ApoWorkDir + "\\apollo_tmp.mdb.gz");
gzFile pFileZipped;
gzFile pFileExpand;
HANDLE hFile;
//LPOFSTRUCT lpReOpenBuff;
int nReadedZipped;
int nReadedExpand;

//voidp pBufZipped;
//voidp pBufExpand;
char pBufZipped[1024];
char pBufExpand[1024];
CString strZip;
CString strExp;
//DWORD nDone;
//BOOL bSuccess;
CString sTmp;

//----compress the to be checked datei


if (!tempFile.IsEmpty())
{
DeleteFile(tempFile);
hFile = CreateFile(tempFile, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// FH 10.12.2004
if (hFile == INVALID_HANDLE_VALUE)
hFile = NULL;
//
if (hFile == NULL)
{
AfxMessageBox("Create ExpandFile "+ tempFile + " failed");
sNameExpand.Empty();
}
}
//fclose(hFile);
//hFile = NULL;

FILE *infile = fopen(sNameExpand, "rb");
gzFile outfile = gzopen(tempFile, "wb");

if (!infile || !outfile)
{
AfxMessageBox("open failed");
return false;
}
char inbuffer[1024];
int num_read = 0;
unsigned long total_read = 0, total_wrote = 0;
while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
total_read += num_read;
gzwrite(outfile, inbuffer, num_read);
}

fclose(infile);
gzclose(outfile);
AfxMessageBox("Create ExpandFile" + tempFile);
//----

/*
* gzopen returns NULL if the file could not be opened or if there was
* insufficient memory to allocate the (de)compression state; errno
* can be checked to distinguish the two cases (if errno is zero, the
* zlib error is Z_MEM_ERROR).
*/
pFileZipped = gzopen(sNameZipped, "rb");
pFileExpand = gzopen(tempFile, "rb");

CString s;
s.Format("%d", pFileExpand);
AfxMessageBox(s);
if (pFileZipped && pFileExpand)
{
AfxMessageBox("begin to check integrity");

int nLoops = 0;

//---- read from source file
/*
* gzread returns the number of uncompressed bytes actually read (0 for
* end of file, -1 for error).
*/
nReadedZipped = gzread(pFileZipped, pBufZipped, sizeof(pBufZipped)); //how large is BUFFER_SIZE:10MB
nReadedExpand = gzread(pFileExpand, pBufExpand, sizeof(pBufExpand));


if (nReadedZipped != -1)
{
while (nReadedZipped != 0)
{
//if ((nLoops % 20) == 0)
// pDLT->pParent->PostMessage(pDLT->nMsgWatchdog, ++nWatchdog, 0);

//if (!sNameExpand.IsEmpty())
// WriteFile(hFile, pBuf, nReaded, &nDone, NULL);


if (nReadedZipped != -1 && nReadedZipped != 0)
{
strZip.Format("%s", pBufZipped);
strExp.Format("%s", pBufExpand);
if (strZip != strExp )
return false;

}

AfxMessageBox(strZip + "\n" + strExp);
//go on reading
nReadedZipped = gzread(pFileZipped, pBufZipped, sizeof(pBufZipped));
nReadedExpand = gzread(pFileExpand, pBufExpand, sizeof(pBufExpand));
if (nReadedZipped == -1 || nReadedZipped == 0)
return false;
AfxMessageBox("break5");
}
}
//----

// if (hFile)
// CloseHandle(hFile);
gzclose(pFileZipped);
gzclose(pFileExpand);
/* if ((nReaded == -1) || (gzclose(pFileZipped) != Z_OK))
{
bSuccess = FALSE;
sTmp.Format("zipped File %s is corrupt", sNameZipped);
// PROTOCOL_ENTRY(0,0,sTmp)

//if (!DeleteFile(sNameZipped))
//PROTOCOL_ENTRY(0,0,"Delete CompressedFile failed")

if ( !sNameExpand.IsEmpty() )
{
if ( !DeleteFile(sNameExpand) )
PROTOCOL_ENTRY(0,0,"Delete ExpandFile failed");
}
return false;
}*/
}
if (!tempFile.IsEmpty())
{
DeleteFile(tempFile);
}
return true;
}