📄 xziptestdlg.cpp
字号:
// XZipTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "XZipTest.h"
#include "XZipTestDlg.h"
#include "about.h"
#include <io.h>
#include "XZip.h"
#include "XUnzip.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TEXT_LINE _T("This is line %5d\r\n")
#define TEXT_LINE1 _T("This is line 1\r\n")
#define IsValidFileHandle(x) ((x) && ((x) != INVALID_HANDLE_VALUE))
///////////////////////////////////////////////////////////////////////////////
// CXZipTestDlg dialog
BEGIN_MESSAGE_MAP(CXZipTestDlg, CDialog)
//{{AFX_MSG_MAP(CXZipTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_TEST, OnTest)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////////
// ctor
CXZipTestDlg::CXZipTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CXZipTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CXZipTestDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
///////////////////////////////////////////////////////////////////////////////
// DoDataExchange
void CXZipTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CXZipTestDlg)
DDX_Control(pDX, IDC_LIST, m_List);
//}}AFX_DATA_MAP
}
///////////////////////////////////////////////////////////////////////////////
// CXZipTestDlg message handlers
///////////////////////////////////////////////////////////////////////////////
// OnInitDialog
BOOL CXZipTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_List.SetContextMenuId(IDR_XLISTBOX);
m_List.AddLine(CXListBox::Blue, CXListBox::White, _T("XZipTest v1.1"));
m_List.AddLine(CXListBox::Blue, CXListBox::White, _T(""));
return TRUE; // return TRUE unless you set the focus to a control
}
///////////////////////////////////////////////////////////////////////////////
// OnSysCommand
void CXZipTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
///////////////////////////////////////////////////////////////////////////////
// OnPaint
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CXZipTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
///////////////////////////////////////////////////////////////////////////////
// OnQueryDragIcon
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CXZipTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
///////////////////////////////////////////////////////////////////////////////
// OnTest
void CXZipTestDlg::OnTest()
{
#ifdef _UNICODE
ZIPENTRYW ze;
#else
ZIPENTRY ze;
#endif
memset(&ze, 0, sizeof(ze));
TCHAR * pszArchive1 = _T("_TestZipTmp1.zip");
TCHAR * pszArchive2 = _T("_TestZipTmp2.zip");
TCHAR * pszName1 = _T("_TestZipTmp1.txt");
TCHAR * pszName2 = _T("_TestZipTmp2.txt");
TCHAR * pszName3 = _T("_TestZipTmp3.txt");
// delete zip files
::DeleteFile(pszArchive1);
::DeleteFile(pszArchive2);
// create .txt files
CreateTextFile(pszName1, 10000);
CreateTextFile(pszName2, 100);
CreateTextFile(pszName3, 10);
///////////////////////////////////////////////////////////////////////////
// single-file zip
m_List.AddLine(CXListBox::Blue, CXListBox::White, _T(""));
m_List.Printf(CXListBox::Navy, CXListBox::White, 0,
_T(" === Testing single-file zip ==="));
BOOL bRet = Zip(pszArchive1, pszName1);
if (bRet)
{
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" Zip archive created OK"));
m_List.Printf(CXListBox::Black, CXListBox::White, 0,
_T(" === Testing unzip ==="));
HZIP hz = OpenZip(pszArchive1, 0, ZIP_FILENAME);
if (hz)
{
ZRESULT zr = GetZipItem(hz, -1, &ze);
if (zr == ZR_OK)
{
int numitems = ze.index;
if (numitems == 1)
{
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" Zip archive contains 1 file ==> OK"));
VerifyZip(hz, pszName1);
CloseZip(hz);
}
else
{
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" Zip contents bad"));
}
}
else
{
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" GetZipItem failed"));
}
}
else
{
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" Failed to open zip file '%s'"), pszArchive1);
}
}
else
{
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" Failed to create zip"));
}
///////////////////////////////////////////////////////////////////////////
// multi-file zip
m_List.AddLine(CXListBox::Blue, CXListBox::White, _T(""));
m_List.Printf(CXListBox::Navy, CXListBox::White, 0,
_T(" === Testing multi-file zip ==="));
HZIP hz = CreateZip(pszArchive2, 0, ZIP_FILENAME);
if (hz)
{
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" Zip archive created OK"));
if ((ZipAdd(hz, pszName1, pszName1, 0, ZIP_FILENAME) == ZR_OK) &&
(ZipAdd(hz, pszName2, pszName2, 0, ZIP_FILENAME) == ZR_OK) &&
(ZipAdd(hz, pszName3, pszName3, 0, ZIP_FILENAME) == ZR_OK))
{
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" Files added to zip archive OK"));
CloseZip(hz);
hz = OpenZip(pszArchive2, 0, ZIP_FILENAME);
if (hz)
{
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" Zip archive opened OK"));
GetZipItem(hz, -1, &ze);
int numitems = ze.index;
if (numitems == 3)
{
m_List.Printf(CXListBox::Green, CXListBox::White, 0,
_T(" Zip archive contains correct number of entries"));
for (int i = 0; i < numitems; i++)
{
GetZipItem(hz, i, &ze);
m_List.Printf(CXListBox::Black, CXListBox::White, 0,
_T(" %d: %s"), i, ze.name);
}
VerifyZip(hz, pszName1);
VerifyZip(hz, pszName2);
VerifyZip(hz, pszName3);
}
else
{
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" Number of entries in zip archive is incorrect"));
}
}
else
{
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" Failed to open zip archive"));
}
}
else
{
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" Failed to add file to zip archive"));
}
CloseZip(hz);
}
else
{
m_List.Printf(CXListBox::Red, CXListBox::White, 0,
_T(" Failed to create zip archive"));
}
CloseZip(hz);
// the files are not deleted - you can inspect them after running test
//::DeleteFile(pszArchive1);
//::DeleteFile(pszArchive2);
//::DeleteFile(pszName1);
//::DeleteFile(pszName2);
//::DeleteFile(pszName3);
m_List.AddLine(CXListBox::Blue, CXListBox::White, _T(""));
}
///////////////////////////////////////////////////////////////////////////////
// Zip
BOOL CXZipTestDlg::Zip(LPCTSTR lpszZipArchive, LPCTSTR lpszSrcFile)
{
BOOL bResult = TRUE;
_ASSERTE(lpszZipArchive);
_ASSERTE(lpszZipArchive[0] != _T('\0'));
if (!lpszZipArchive || lpszZipArchive[0] == _T('\0'))
return FALSE;
_ASSERTE(lpszSrcFile);
_ASSERTE(lpszSrcFile[0] != _T('\0'));
if (!lpszSrcFile || lpszSrcFile[0] == _T('\0'))
return FALSE;
// does zip source file exist?
if (_taccess(lpszSrcFile, 04) != 0)
{
TRACE(_T("WARNING: zip source file '%s' cannot be found, operation aborted\n"),
lpszSrcFile);
return FALSE;
}
// use only the file name for zip file entry
TCHAR * cp = _tcsrchr(lpszSrcFile, _T('\\'));
if (cp == NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -