2008年1月8日星期二

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;
}

没有评论: