⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winzipdlg.cpp

📁 winzip压缩解压器
💻 CPP
字号:
// winzipDlg.cpp : implementation file
//

#include "stdafx.h"
#include "winzip.h"
#include "winzipDlg.h"
#include "InfoZip\InfoZip.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWinzipDlg dialog

CWinzipDlg::CWinzipDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWinzipDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWinzipDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWinzipDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWinzipDlg)
	DDX_Control(pDX, IDC_LISTFILES, m_lstFiles);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWinzipDlg, CDialog)
	//{{AFX_MSG_MAP(CWinzipDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADDFILES, OnAddFiles)
	ON_BN_CLICKED(IDC_DELETEFILES, OnDeleteFiles)
	ON_BN_CLICKED(IDC_SELECTARCHIVE, OnSelectArchive)
	ON_BN_CLICKED(IDC_COMPRESS, OnCompress)
	ON_BN_CLICKED(IDC_EXTRACT, OnExtract)
	ON_BN_CLICKED(IDC_SELECTARCHIVE2, OnSelectarchive2)
	ON_BN_CLICKED(IDC_SELECTARCHIVE3, OnSelectarchive3)
	ON_LBN_SELCHANGE(IDC_LISTFILES, OnSelchangeListfiles)
	ON_EN_CHANGE(IDC_ARCHIVE, OnChangeArchive)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWinzipDlg message handlers

BOOL CWinzipDlg::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
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWinzipDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CWinzipDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWinzipDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CWinzipDlg::OnAddFiles() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg (TRUE, NULL, NULL, OFN_ALLOWMULTISELECT,
		"All files (*.*)|*.*||", this);

	if (dlg.DoModal() == IDOK)
	{
		POSITION pos = dlg.GetStartPosition();
		while (pos)
		{
			CString sFile = dlg.GetNextPathName(pos);
			m_lstFiles.AddString(sFile);
		}
	}
}

void CWinzipDlg::OnDeleteFiles() 
{
	// TODO: Add your control notification handler code here
	int iSelCount = m_lstFiles.GetSelCount();
	int *piSelFiles = new int[iSelCount];
	m_lstFiles.GetSelItems(iSelCount, piSelFiles);

	for (int i=iSelCount-1; i>=0; i--)
	{
		int iSelectedFile = piSelFiles[i];
		m_lstFiles.DeleteString(iSelectedFile);
	}

	delete[] piSelFiles;
}

void CWinzipDlg::OnSelectArchive() 
{
	// TODO: Add your control notification handler code here
	/**
	CFileDialog dlg (FALSE, NULL, NULL, 0,
		"All files (*.*)|*.*||", this);

	if (dlg.DoModal() == IDOK)
	{
		CString sFile = dlg.GetPathName();
		GetDlgItem(IDC_ARCHIVE)->SetWindowText(sFile);
	}**/
	/***用选取文件夹的方法*****/
	char pszDisplayName[_MAX_PATH]; 
    BROWSEINFO BrowseInfo; 
    LPITEMIDLIST pidlBrowse; 
    LPMALLOC pMalloc; 
    if( !SUCCEEDED(SHGetMalloc(&pMalloc)) ) 
        return; 
    BrowseInfo.hwndOwner = m_hWnd; 
    BrowseInfo.pidlRoot = NULL; 
    BrowseInfo.pszDisplayName = pszDisplayName; 
    BrowseInfo.lpszTitle = _T("选择保存位置..."); 
    BrowseInfo.ulFlags = 0x40|BIF_RETURNONLYFSDIRS; //BIF_DONTGOBELOWDOMAIN; 
    BrowseInfo.lpfn = NULL; 
    BrowseInfo.lParam = 0; 
    BrowseInfo.iImage = 0; 
    pidlBrowse = SHBrowseForFolder(&BrowseInfo); 
    if( pidlBrowse!=NULL ) 
    { 
        if( SHGetPathFromIDList(pidlBrowse,pszDisplayName) ) 
        {
			int i;
	        i=strlen(pszDisplayName);
			if(pszDisplayName[i-1]!='\\')
			{
				pszDisplayName[i]='\\';
                pszDisplayName[i+1]=0;
			}
            CString m_path = pszDisplayName; 
            GetDlgItem(IDC_ARCHIVE)->SetWindowText(pszDisplayName);
         } 
         pMalloc->Free(pidlBrowse); 
     } 
     pMalloc->Release(); 
//	 GetDlgItem(IDC_ARCHIVE)->SetWindowText(pszDisplayName);
}

void CWinzipDlg::OnCompress() 
{
	BeginWaitCursor();
	char pszArchive[MAX_PATH+1];
	GetDlgItem(IDC_ARCHIVE)->GetWindowText(pszArchive, MAX_PATH+1);
	
	int iFileCount = m_lstFiles.GetCount();

	CFileFind finder;								//判断文件是否存在的类 

	int length;

	char **pFiles = (char **) new  int [iFileCount];
	
	if(iFileCount==0)									
	{
		AfxMessageBox("没有选择要压缩的文件!", MB_OK);
		return;
	}
	
	if(pszArchive[0]=='\0')								
	{
		AfxMessageBox("没有指定压缩后的文件名!", MB_OK);
		return;
	}
	
	length=strlen(pszArchive);							//判断后缀名是否正确
	if((pszArchive[length-4]!='.') || (pszArchive[length-3]!='z' && pszArchive[length-3]!='Z') || (pszArchive[length-2]!='i'&&pszArchive[length-2]!='I') || (pszArchive[length-1]!='p'&&pszArchive[length-1]!='P'))
	{
		AfxMessageBox("文件命名不正确,请重命名为*.zip格式!", MB_OK);
		return;
	}

	BOOL bWorking = finder.FindFile(pszArchive);		//判断文件是否已存在
	if(bWorking!=0)
	{
		AfxMessageBox("压缩后的文件已存在,请重命名!", MB_OK);
		return;
	}
	
	for (int i=0; i<iFileCount; i++)
	{
		pFiles[i] = new char[MAX_PATH+1];
		m_lstFiles.GetText(i, pFiles[i]);
	}

	CInfoZip InfoZip;
	if (!InfoZip.InitializeZip())
	{
		EndWaitCursor();
		AfxMessageBox("Must be initialized", MB_OK);
		return;
	}

	if (!InfoZip.AddFiles(pszArchive, pFiles, iFileCount))
	{
		EndWaitCursor();
		AfxMessageBox("Files not added", MB_OK);
		return;
	}

	if (!InfoZip.Finalize())
	{
		EndWaitCursor();
		AfxMessageBox("Cannot finalize", MB_OK);
		return;
	}
	AfxMessageBox("完成压缩!", MB_OK);
	EndWaitCursor();
}

void CWinzipDlg::OnExtract() 
{
	// TODO: Add your control notification handler code here
	BeginWaitCursor();
	char pszArchive[MAX_PATH+1];
	ZeroMemory(pszArchive, MAX_PATH+1);
	GetDlgItem(IDC_ARCHIVE2)->GetWindowText(pszArchive, MAX_PATH+1);

	char pszTarget[MAX_PATH+1];
	ZeroMemory(pszTarget, MAX_PATH+1);
	GetDlgItem(IDC_TARGET)->GetWindowText(pszTarget, MAX_PATH+1);
	
	if(pszArchive[0]=='\0')				
	{
		AfxMessageBox("没有选取要解压的zip文件!", MB_OK);
		return;
	}
	if( pszTarget[0]=='\0')				
	{
		AfxMessageBox("没有指定要解压到哪个文件夹!", MB_OK);
		return;
	}
	CInfoZip InfoZip;

	if (!InfoZip.InitializeUnzip())
	{
		EndWaitCursor();
		AfxMessageBox("Must be initialized", MB_OK);
		return;
	}

	if (!InfoZip.ExtractFiles(pszArchive, pszTarget))
	{
		EndWaitCursor();
		AfxMessageBox("Files not added", MB_OK);
		return;
	}

	if (!InfoZip.FinalizeUnzip())
	{
		EndWaitCursor();
		AfxMessageBox("Cannot finalize", MB_OK);
		return;
	}
	EndWaitCursor();
	AfxMessageBox("完成解压!", MB_OK);
}

void CWinzipDlg::OnSelectarchive2() 
{
	// TODO: Add your control notification handler code here
		CFileDialog dlg (TRUE, NULL, NULL, 0,
		"Zip files (*.zip)|*.zip||", this);

	if (dlg.DoModal() == IDOK)
	{
		CString sFile = dlg.GetPathName();
		GetDlgItem(IDC_ARCHIVE2)->SetWindowText(sFile);
	}
}

void CWinzipDlg::OnSelectarchive3() 
{
	// TODO: Add your control notification handler code here
	/**
	CFileDialog dlg (FALSE, NULL, NULL, 0,
		"All files (*.*)|*.*||", this);

	if (dlg.DoModal() == IDOK)
	{
		CString sFile = dlg.GetPathName();
		GetDlgItem(IDC_TARGET)->SetWindowText(sFile);
	}**/
	/***用浏览文件夹方法****/
	char pszDisplayName[_MAX_PATH]; 
    BROWSEINFO BrowseInfo; 
    LPITEMIDLIST pidlBrowse; 
    LPMALLOC pMalloc; 
    if( !SUCCEEDED(SHGetMalloc(&pMalloc)) ) 
        return; 
    BrowseInfo.hwndOwner = m_hWnd; 
    BrowseInfo.pidlRoot = NULL; 
    BrowseInfo.pszDisplayName = pszDisplayName; 
    BrowseInfo.lpszTitle = _T("选择保存位置..."); 
    BrowseInfo.ulFlags = 0x40|BIF_RETURNONLYFSDIRS; //BIF_DONTGOBELOWDOMAIN; 
    BrowseInfo.lpfn = NULL; 
    BrowseInfo.lParam = 0; 
    BrowseInfo.iImage = 0; 
    pidlBrowse = SHBrowseForFolder(&BrowseInfo); 
    if( pidlBrowse!=NULL ) 
    { 
        if( SHGetPathFromIDList(pidlBrowse,pszDisplayName) ) 
        {
			int i;
	        i=strlen(pszDisplayName);
			if(pszDisplayName[i-1]!='\\')
			{
				pszDisplayName[i]='\\';
                pszDisplayName[i+1]=0;
			}
            CString m_path = pszDisplayName; 
            GetDlgItem(IDC_TARGET)->SetWindowText(pszDisplayName);
         } 
         pMalloc->Free(pidlBrowse); 
     } 
     pMalloc->Release(); 
//	 GetDlgItem(IDC_TARGET)->SetWindowText(pszDisplayName);
}

void CWinzipDlg::OnSelchangeListfiles() 
{
	// TODO: Add your control notification handler code here
	
}

void CWinzipDlg::OnChangeArchive() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CWinzipDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -