解压缩功能的实现--使用XZip与XUnzip

本文档介绍了如何在MFC程序中使用XZip库进行解压缩操作。首先,通过添加XZip到项目并引用头文件来集成库。接着,详细解释了CreateZip、ZipAdd、OpenZip等关键函数的用法,如创建压缩文件、添加文件到压缩文件、打开压缩文件等。还提到了如何通过管道、文件、内存等方式操作压缩文件,并提供了简易界面的实现示例。

使用的XZip版本1.3  源程序可在Codeproject上获得,本教程实例代码下载

  • 建立基于对话框的MFC程序(XZip是不依赖MFC)
  • 添加XZip到项目,并添加头文件引用,至此可以使用XZip。

          

在编译的时候,若有报错如下:

对XUnzip.cpp与XZip不适用预编译头

重新编译,无报错。


几个常用函数简介:

  • CreateZip()--创建压缩文件。
<pre class="cpp" name="code">HZIP CreateZip(void *z,
 unsigned int len,
 DWORD flags);
 

// Purpose:     Create a zip archive file//// Parameters:  z      - archive file name if flags is ZIP_FILENAME;  for other//                       uses see below//              len    - for memory (ZIP_MEMORY) should be the buffer size;//                       for other uses, should be 0//              flags  - indicates usage, see below;  for files, this will be//                       ZIP_FILENAME//// Returns:     HZIP   - non-zero if zip archive created ok, otherwise 0

调用CreateZip来创建一个压缩文件,在创建压缩文件的时候,根据flags来判断文件存储位置

管道中:CreateZip(hpip_write,0,ZIP_HANDLE);

文件中(根据文件句柄):CreateZip(hfile, 0,ZIP_HANDLE);

文件中(文件路径):CreateZip("c:\\test.zip", 0,ZIP_FILENAME);

内存中:CreateZip(buf, len,ZIP_MEMORY);

页面文件:CreateZip(0, len,ZIP_MEMORY);

  • ZipAdd()--在压缩文件中添加文件
ZRESULT ZipAdd(HZIP hz,
 const TCHAR *dstzn,
 void *src,
 unsigned int len,
 DWORD flags);

// ZipAdd()
//
// Purpose:     Add a file to a zip archive
//
// Parameters:  hz      - handle to an open zip archive
//              dstzn   - name used inside the zip archive to identify the file
//              src     - for a file (ZIP_FILENAME) this specifies the filename
//                        to be added to the archive;  for other uses, see below
//              len     - for memory (ZIP_MEMORY) this specifies the buffer
//                        length;  for other uses, this should be 0
//              flags   - indicates usage, see below;  for files, this will be
//                        ZIP_FILENAME
//
// Returns:     ZRESULT - ZR_OK if success, otherwise some other value

每一个需要添加进hz的文件都需要调用ZipAdd();

dstzn存在hz中的文件的名字。被加到hz中的文件可以从管道中来:ZipAdd(hz,"file.dat", hpipe_read,0,ZIP_HANDLE);

从文件(文件句柄)中来: ZipAdd(hz,"file.dat", hfile,0,ZIP_HANDLE);

从文件(路径)中来:ZipAdd(hz,"file.dat", "c:\\docs\\origfile.dat",0,ZIP_FILENAME);

从内存中来: ZipAdd(hz,"subdir\\file.dat", buf,len,ZIP_MEMORY);

或者添加文件夹:ZipAdd(hz,"subdir",   0,0,ZIP_FOLDER);

  • 打开zip文件--OpenZip()
HZIP OpenZip(void *z, unsigned int len, DWORD flags);

// OpenZip()
//
// Purpose:     Open an existing zip archive file
//
// Parameters:  z      - archive file name if flags is ZIP_FILENAME;  for other
//                       uses see below
//              len    - for memory (ZIP_MEMORY) should be the buffer size;
//                       for other uses, should be 0
//              flags  - indicates usage, see below;  for files, this will be
//                       ZIP_FILENAME
//
// Returns:     HZIP   - non-zero if zip archive opened ok, otherwise 0
// OpenZip - opens a zip file and returns a handle with which you can
// subsequently examine its contents. You can open a zip file from:
// from a pipe:             OpenZip(hpipe_read,0, ZIP_HANDLE);
// from a file (by handle): OpenZip(hfile,0,      ZIP_HANDLE);
// from a file (by name):   OpenZip("c:\\test.zip",0, ZIP_FILENAME);
// from a memory block:     OpenZip(bufstart, buflen, ZIP_MEMORY);
// If the file is opened through a pipe, then items may only be
// accessed in increasing order, and an item may only be unzipped once,
// although GetZipItem can be called immediately before and after unzipping
// it. If it's opened i n any other way, then full random access is possible.
// Note: pipe input is not yet implemented.

  • GetZipItem() - Get information about an item in an open zip archive.
//////////////////////////////////////////////////////////////////////////////
//
// GetZipItem()
//
// Purpose:     Get information about an item in an open zip archive
//
// Parameters:  hz      - handle of open zip archive
//              index   - index number (0 based) of item in zip
//              ze      - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
//                        (if Unicode)
//
// Returns:     ZRESULT - ZR_OK if success, otherwise some other value
//
  • FindZipItem() - Find item by name and return information about it.
//////////////////////////////////////////////////////////////////////////////
//
// FindZipItem()
//
// Purpose:     Find item by name and return information about it
//
// Parameters:  hz      - handle of open zip archive
//              name    - name of file to look for inside zip archive
//              ic      - TRUE = case insensitive
//              index   - pointer to index number returned, or -1
//              ze      - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
//                        (if Unicode)
//
// Returns:     ZRESULT - ZR_OK if success, otherwise some other value
//
  • UnzipItem() - Find item by index and unzip it.
//////////////////////////////////////////////////////////////////////////////
//
// UnzipItem()
//
// Purpose:     Find item by index and unzip it
//
// Parameters:  hz      - handle of open zip archive
//              index   - index number of file to unzip
//              dst     - target file name of unzipped file
//              len     - for memory (ZIP_MEMORY. length of buffer;
//                        otherwise 0
//              flags   - indicates usage, see below;  for files, this will be
//                        ZIP_FILENAME
//
// Returns:     ZRESULT - ZR_OK if success, otherwise some other value
//


  • 关闭zip文件--CloseZip();

ZRESULT CloseZip(HZIP hz);


在实例中,演示压缩指定文件夹里的所有文件。

简易界面:


添加文件代码:

void CXzippppDlg::OnBnClickedBtnAddpath()
{
	// TODO: 在此添加控件通知处理程序代码
	TCHAR szPath[MAX_PATH];     //存放选择的目录路径 
	m_edit_show=_T("");
    ZeroMemory(szPath, sizeof(szPath));   
    BROWSEINFO bi;   
    bi.hwndOwner = m_hWnd;   
    bi.pidlRoot = NULL;   
    bi.pszDisplayName = szPath;   
    bi.lpszTitle = L"选择需要添加的文件目录";   
    bi.ulFlags = 0;   
    bi.lpfn = NULL;   
    bi.lParam = 0;   
    bi.iImage = 0;   
    //弹出选择目录对话框
    LPITEMIDLIST lp = SHBrowseForFolder(&bi);   
    if(lp && SHGetPathFromIDList(lp, szPath))   
    {
        m_edit_show.Format(L"已选择目录 %s",  szPath);
		m_strPath=szPath;
    }
    else   
        AfxMessageBox(L"无效的目录,请重新选择"); 
	UpdateData(FALSE);
}
压缩文件代码:

oid CXzippppDlg::OnBnClickedBtnZip()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_strPath==_T(""))
	{
		 AfxMessageBox(L"请选择有效的路径");
		 return;
	}
	//1.创建zip
		HZIP hz=CreateZip(L"C:\\test.zip",0,ZIP_FILENAME);//
	//2.添加文件
		if (hz)
		{
			//ZipAdd(hz,m_strPath,0,0,ZIP_FOLDER);
			//遍历路径下所有文件和文件夹。
			ZipAllFile(m_strPath,hz);
		}
		CloseZip(hz);
}

void CXzippppDlg::ZipAllFile(CString strDir,HZIP hz)

{//可以在线程中实现,不然压缩大文件时,主程序要等待
     if(strDir == _T(""))  
         return; 
     else
     {
         if(strDir.Right(1) != _T("\\"))
              strDir += L"\\"; 
         strDir =strDir+_T("*.*"); 
     }
     CFileFind finder; 
     CString strPath; 
     BOOL bWorking = finder.FindFile(strDir); 
     while(bWorking)
     { 
         bWorking = finder.FindNextFile(); 
         strPath = finder.GetFilePath(); 
         if(finder.IsDirectory() && !finder.IsDots()) 
		 {
			// CString pathname=strPath.Mid(strPath.ReverseFind('\\')+1);
			 //ZipAdd(hz,pathname,0,0,ZIP_FOLDER);
              ZipAllFile(strPath,hz); //递归调用
		 }
         else if(!finder.IsDirectory() && !finder.IsDots()) 
         {
              //strPah就是所要获取的文件路径
			 CString filename=strPath.Mid(strPath.ReverseFind('\\')+1);
			 ZipAdd(hz, filename, strPath.GetBuffer(), 0, ZIP_FILENAME);
         }
     } 

}
压缩功能做好后,还有解压功能暂时没做。






评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值