UnZip.h/Zip.h C++压缩解压类

最近刚好用到,个人觉得还是比较好用的:
以下来自网络示例:
Example 1 - create a Zip file from existing files

// We place the file "simple.bmp" inside, but inside
// the zipfile it will actually be called "znsimple.bmp".
// Similarly the textfile.

HZIP hz = CreateZip("simple1.zip",0);
ZipAdd(hz,"znsimple.bmp",  "simple.bmp");
ZipAdd(hz,"znsimple.txt",  "simple.txt");
CloseZip(hz);

Example 2 - unzip a Zip file using the names it has inside it

HZIP hz = OpenZip("\\simple1.zip",0);
ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
// -1 gives overall information about the zipfile
for (int zi=0; zi<numitems; zi++)
{ ZIPENTRY ze; GetZipItem(hz,zi,&ze); // fetch individual details
  UnzipItem(hz, zi, ze.name);         // e.g. the item's name.
}
CloseZip(hz);

Example 3- unzip from resource directly into memory

This technique is useful for small games, where you want to keep all resources bundled up inside the executable, but restricting the size.

Suppose we used a .rc with 1 RCDATA “file.zip” to embed the Zip file as a resource.

HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
HANDLE hglob = LoadResource(hInstance,hrsrc);
void *zipbuf = LockResource(hglob);
unsigned int ziplen = SizeofResource(hInstance,hrsrc);
hz = OpenZip(zipbuf, ziplen, 0);
ZIPENTRY ze; int i; FindZipItem(hz,"sample.jpg",true,&i,&ze);
// that lets us search for an item by filename.
// Now we unzip it to a membuffer.
char *ibuf = new char[ze.unc_size];
UnzipItem(hz,i, ibuf, ze.unc_size);
...
delete[] ibuf;
CloseZip(hz);
// note: no need to free resources obtained through Find/Load/LockResource

Example 4 - unzip chunk by chunk to a membuffer

Normally when you call UnzipItem(…), it gives the return-code ZR_OK. But if you gave it too small a buffer so that it couldn’t fit it all in, then it returns ZR_MORE.

char buf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0;
while (zr==ZR_MORE)
{ zr = UnzipItem(hz,i, buf,1024);
  unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
  ... maybe write the buffer to a disk file here
  totsize+=bufsize;
}

下载地址:Zip Utils

### 回答1: MFC(Microsoft Foundation Class)是一种用于开发Windows应用程序的框架,其中包含了丰富的库和基础设施,用于简化Windows开发过程。在MFC中,可以使用ZipArchive解压缩文件。 压缩文件unzip.cpp,指的是实现解压缩文件功能的源代码文件。 在unzip.cpp中,我们可以使用MFC提供的ZipArchive来进行解压缩操作。首先,我们需要包含相应的头文件: #include <afx.h> #include <afxwin.h> #include <afxext.h> 然后,我们需要创建一个ZipArchive对象,并指定待解压缩的文件路径: CZipArchive zip; CString filePath = "待解压缩文件的路径"; zip.Open(filePath); 接下来,我们可以使用zip.GetFileCount()方法获取压缩文件中的文件数量,并通过循环遍历每个文件: int fileCount = zip.GetFileCount(); for (int i = 0; i < fileCount; i++) { CZipFile *zipFile = zip.GetFileInfo(i); // 获取文件信息 CString fileName = zipFile->GetFileName(); // 获取文件名 CString targetPath = "目标解压路径" + fileName; // 设置解压后的文件路径 zipFile->Open(); // 打开文件 FILE *fp = fopen(targetPath, "wb"); // 创建目标文件 char buffer[1024]; UINT bytesRead; while ((bytesRead = zipFile->Read(buffer, 1024)) > 0) { fwrite(buffer, bytesRead, 1, fp); // 逐块写入目标文件 } fclose(fp); // 关闭文件 zipFile->Close(); // 关闭ZIP文件 } 最后,记得关闭ZipArchive对象: zip.Close(); 以上就是使用MFC解压缩文件的简要过程。通过这段代码,我们可以实现对压缩文件的解压缩操作。当然,具体的实现细节还需要根据实际需求进行调整和完善。 ### 回答2: MFC压缩文件的解压缩主要通过使用CFile和CArchive来实现。以下是一个使用MFC解压缩文件的示例代码(unzip.cpp): #include "stdafx.h" #include "unzip.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 函数头声明 void UnzipFile(const CString& strZipFile, const CString& strDestFolder); // 执行解压缩的函数 void UnzipFile(const CString& strZipFile, const CString& strDestFolder) { // 创建解压缩文件的CFile对象 CFile file; file.Open(strZipFile, CFile::modeRead); // 创建CArchive对象来实现解压缩 CArchive ar(&file, CArchive::load); // 获取目标文件夹的路径并确保其存在 CString strFolder = strDestFolder + "\\"; if (!PathFileExists(strFolder)) { CreateDirectory(strFolder, NULL); } // 循环解压缩文件 while (!ar.IsBufferEmpty()) { // 在目标文件夹中创建一个新文件 CString strFileName; ar >> strFileName; strFileName = strFolder + strFileName; CFile newFile; newFile.Open(strFileName, CFile::modeCreate | CFile::modeWrite); // 将数据从归档对象写入新文件 UINT nLength = ar.GetFile()->GetLength(); BYTE* pBuf = new BYTE[nLength]; ar.Read(pBuf, nLength); newFile.Write(pBuf, nLength); // 写入完成后关闭新文件 newFile.Close(); } // 关闭解压缩归档对象 ar.Close(); file.Close(); } 使用上述代码,可以将压缩文件解压缩到指定的目标文件夹中。解压缩功能通过使用CFile和CArchive实现,循环将解压缩的文件从归档对象中读取并写入到目标文件夹中的新文件中。解压缩前需要确保目标文件夹存在,并在解压缩完成后关闭文件和归档对象。 注意:上述代码仅供参考,实际使用时,可能需要根据具体的需求进行修改和适配。 ### 回答3: MFC是Microsoft Foundation Classes的缩写,它是微软公司提供的一套面向对象的C++库,用于开发Windows应用程序。unzip.cpp是用于解压缩文件的代码文件。 在MFC中,要实现文件的解压缩功能,可以利用CFile和CFileException来操作文件。 首先,需要声明一个CFile对象,并打开要解压缩的文件。可以使用CFile::Open()函数打开文件,并在打开文件时检查是否正常打开。 然后,可以利用CFile的Read()函数读取文件中的内容,并且将读取的内容写入到目标文件中。要解压缩文件,需要读取压缩文件中的每个字节,并根据压缩格式的规则进行解压缩。 在解压缩过程中,可以利用缓冲区来提高读写的效率。可以使用BYTE型的数组作为缓冲区,使用Read()函数从文件中读取数据,然后使用Write()函数将数据写入到目标文件中。 解压缩完成后,应该关闭文件。可以使用CFile的Close()函数来关闭文件。 以上就是使用MFC的CFile实现文件解压缩的大致步骤。当然,具体还需要根据解压缩文件的格式和要求进行相应的编码和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值