📄 xziptestdlg.cpp
字号:
cp = (TCHAR *) lpszSrcFile;
else
cp++;
HZIP hz = CreateZip((void *)lpszZipArchive, 0, ZIP_FILENAME);
if (hz)
{
ZRESULT zr = ZipAdd(hz, cp, (void *)lpszSrcFile, 0, ZIP_FILENAME);
CloseZip(hz);
// did add work?
if (zr == ZR_OK)
{
TRACE(_T("added '%s' to zip file '%s'\n"),
lpszSrcFile, lpszZipArchive);
bResult = TRUE;
}
else
{
TRACE(_T("WARNING: failed to add zip source file '%s'\n"),
lpszSrcFile);
bResult = FALSE;
}
}
else
{
TRACE(_T("ERROR: failed to create zip file '%s'\n"),
lpszZipArchive);
bResult = FALSE;
}
return bResult;
}
///////////////////////////////////////////////////////////////////////////////
// VerifyZip
void CXZipTestDlg::VerifyZip(HZIP hz, LPCTSTR lpszFile)
{
#ifdef _UNICODE
ZIPENTRYW ze;
#else
ZIPENTRY ze;
#endif
memset(&ze, 0, sizeof(ze));
int index = -1;
ZRESULT zr = 0;
zr = FindZipItem(hz, lpszFile, TRUE, &index, &ze);
m_List.Printf(CXListBox::Black, CXListBox::White, 0,
_T(" === Checking contents of zip entry %s ==="), lpszFile);
if (zr == ZR_OK)
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" FindZipItem returned OK"));
else
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" FindZipItem failed"));
if (_tcscmp(lpszFile, ze.name) == 0)
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" FindZipItem found name ==> OK"));
else
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" FindZipItem failed to find name"));
TCHAR targetname[MAX_PATH];
_tcscpy(targetname, _T("_unzip"));
_tcscat(targetname, lpszFile);
// delete target file if it exists
::DeleteFile(targetname);
zr = UnzipItem(hz, index, targetname, 0, ZIP_FILENAME);
if (zr == ZR_OK)
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" UnzipItem returned OK"));
else
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" UnzipItem failed"));
if (_taccess(targetname, 04) == 0)
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" Target file created OK"));
else
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" UnzipItem failed to create target file"));
BOOL bResult = FALSE;
BOOL bRet = Compare(lpszFile, targetname, &bResult);
if (bRet && bResult)
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" Target file matches original file ==> OK"));
else
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" Target file does not match original file"));
// the target file is not deleted - you can inspect it after running test
//::DeleteFile(targetname);
}
#define COMPARE_BUF_SIZE (64*1024)
///////////////////////////////////////////////////////////////////////////////
// Compare
BOOL CXZipTestDlg::Compare(LPCTSTR lpszFile1, LPCTSTR lpszFile2, BOOL *pbResult)
{
_ASSERTE(pbResult);
if (!pbResult)
return FALSE;
*pbResult = FALSE;
BOOL bCompare = FALSE; // TRUE = files identical
// FALSE = files not identical
BOOL bOp = TRUE; // TRUE = no API failures, compare completed
// FALSE = file or memory API failed
_ASSERTE(lpszFile1);
_ASSERTE(lpszFile1[0] != _T('\0'));
if (!lpszFile1 || lpszFile1[0] == _T('\0'))
return FALSE;
_ASSERTE(lpszFile2);
_ASSERTE(lpszFile2[0] != _T('\0'));
if (!lpszFile2 || lpszFile2[0] == _T('\0'))
return FALSE;
HANDLE hFile1 = NULL, hFile2 = NULL;
TRACE(_T("opening '%s'"), lpszFile1);
// open file 1
hFile1 = ::CreateFile(lpszFile1,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile1 == INVALID_HANDLE_VALUE)
{
TRACE(_T("ERROR: %s failed\n"), _T("CreateFile"));
return FALSE;
}
TRACE(_T("opening '%s'"), lpszFile2);
// open file 2
hFile2 = ::CreateFile(lpszFile2,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile2 == INVALID_HANDLE_VALUE)
{
TRACE(_T("ERROR: %s failed\n"), _T("CreateFile"));
::CloseHandle(hFile1);
return FALSE;
}
DWORD dwFileSize1 = ::GetFileSize(hFile1, NULL);
DWORD dwFileSize2 = ::GetFileSize(hFile2, NULL);
if ((dwFileSize1 != INVALID_FILE_SIZE) && (dwFileSize2 != INVALID_FILE_SIZE))
{
// continue if file sizes match
if (dwFileSize1 == dwFileSize2)
{
BYTE * pBuf1 = new BYTE [COMPARE_BUF_SIZE];
_ASSERTE(pBuf1);
BYTE * pBuf2 = new BYTE [COMPARE_BUF_SIZE];
_ASSERTE(pBuf2);
if (pBuf1 && pBuf2)
{
while (dwFileSize1)
{
DWORD dwBytesRead1 = 0;
BOOL bRet1 = ::ReadFile(hFile1,
(LPVOID) pBuf1,
COMPARE_BUF_SIZE,
&dwBytesRead1,
NULL);
if (!bRet1)
{
TRACE(_T("ERROR: %s failed\n"), _T("ReadFile"));
bOp = FALSE;
break;
}
DWORD dwBytesRead2 = 0;
BOOL bRet2 = ::ReadFile(hFile2,
(LPVOID) pBuf2,
COMPARE_BUF_SIZE,
&dwBytesRead2,
NULL);
if (!bRet2)
{
TRACE(_T("ERROR: %s failed\n"), _T("ReadFile"));
bOp = FALSE;
break;
}
if (dwBytesRead1 != dwBytesRead2)
{
TRACE(_T("Compare failed ==> file Read sizes different\n"));
break;
}
if (dwBytesRead1 == 0)
{
// Read ok, but nothing read
TRACE(_T("Read %s ==> EOF reached.\n"), lpszFile1);
bCompare = TRUE;
break;
}
if (dwBytesRead2 == 0)
{
// Read ok, but nothing read
TRACE(_T("Read %s ==> EOF reached.\n"), lpszFile2);
bCompare = TRUE;
break;
}
// do contents match?
int nCmp = memcmp(pBuf1, pBuf2, dwBytesRead1);
if (nCmp != 0)
{
TRACE(_T("Compare failed ==> file contents different\n"));
break;
}
dwFileSize1 -= dwBytesRead1;
} // while (dwFileSize1)
if (dwFileSize1 == 0)
bCompare = TRUE;
if (pBuf1)
delete [] pBuf1;
if (pBuf2)
delete [] pBuf2;
}
else
{
// memory allocation failed
TRACE(_T("ERROR: memory allocation failure\n"));
bOp = FALSE;
}
}
else
{
TRACE(_T("Compare failed ==> file sizes different\n"));
}
}
else
{
// GetFileSize failed
TRACE(_T("ERROR: %s failed\n"), _T("GetFileSize"));
bOp = FALSE;
}
if (IsValidFileHandle(hFile1))
::CloseHandle(hFile1);
if (IsValidFileHandle(hFile2))
::CloseHandle(hFile2);
*pbResult = bCompare;
return bOp;
}
///////////////////////////////////////////////////////////////////////////////
// CreateTextFile
void CXZipTestDlg::CreateTextFile(LPCTSTR lpszFile, int nLines)
{
m_List.Printf(CXListBox::Black, CXListBox::White, 0,
_T(" Creating text file '%s' with %d lines"), lpszFile, nLines);
// open existing or create new
HANDLE hFile = NULL;
hFile = ::CreateFile(lpszFile,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return;
TCHAR s[100];
BOOL bRet = FALSE;
for (int i = 0; i < nLines; i++)
{
_stprintf(s, TEXT_LINE, i+1);
DWORD dwBytesWritten = 0;
int n = _tcslen(s)*sizeof(TCHAR);
bRet = ::WriteFile(hFile,
s,
n,
&dwBytesWritten,
NULL);
if (!bRet)
{
TRACE(_T("ERROR: %s failed\n"), _T("WriteFile"));
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" ERROR: file write failed"));
return;
}
}
DWORD dwEOF = ::GetFileSize(hFile, NULL);
DWORD dwSize = nLines * _tcslen(TEXT_LINE1) * sizeof(TCHAR);
if (dwEOF == dwSize)
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" File size matches size written ==> file write OK"));
else
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" File size does not match size written ==> file write failed"));
::CloseHandle(hFile);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -