手机
当前位置:查字典教程网 >编程开发 >C语言 >C++Zip压缩解压缩示例(支持递归压缩)
C++Zip压缩解压缩示例(支持递归压缩)
摘要:第三方函数、头文件、测试工程下载地址:http://pan.baidu.com/s/1gSfKo复制代码代码如下://文件名:ZipFunc...

C++Zip压缩解压缩示例(支持递归压缩)1

第三方函数、头文件、测试工程下载地址:http://pan.baidu.com/s/1gSfKo

复制代码 代码如下:

// 文件名: ZipFunction.h

#pragma once

#include "zip.h"

#include "unzip.h"

namespace ZipUtils

{

// ------------------------------------------------------------------------------------------------------------------------

// Summary:

// 解压zip文件到指定路径, 并返回解压文件路径和文件名。

// Parameters:

// lpszZipFullName - 待解压 zip压缩包所在文件夹路径和zip压缩名称; 如"D://00//1.zip"。

// szFilePathArr - 保存的解压后文件的文件名;如"1.jpg"。

// lpszUnZipPath - 解压出来的文件 所存放位置的完整路径; 如 “D://01”

// 此参数省略时,默认解压到exe程序所在文件夹下。

// Returns:

// 解压成功返回ZR_OK,解压失败返回错误码。

// ------------------------------------------------------------------------------------------------------------------------

ZRESULT ExtractZipToDir(LPCTSTR lpszZipFullName, CStringArray& szFilePathArr, LPCTSTR lpszUnZipPath = NULL);

// ------------------------------------------------------------------------------------------------------------------------

// Summary:

// 压缩指定路径下的文件,并保存压缩包到指定路径。

// Parameters:

// lpszSrcPath - 待压缩文件所在的路径; 如"D://00"。

// lpszDestPath - 压缩完成后,存放压缩包的路径。

// 此参数省略时,默认存放路径为exe程序所在文件的路径。

// lpszZipName - 压缩完成后,压缩的名称;如“MySkin.zip”。

// Returns:

// 压缩成功返回ZR_OK,压缩失败返回错误码。

// ------------------------------------------------------------------------------------------------------------------------

ZRESULT CompressDirToZip(LPCTSTR lpszSrcPath, LPCTSTR lpszZipName, LPCTSTR lpszDestPath = NULL);

}

复制代码 代码如下:

#include "stdafx.h"

#include "ZipFunction.h"

#include <io.h>

namespace ZipUtils

{

// 全局变量

int g_nCount = 0;

ZRESULT ExtractZipToDir(LPCTSTR lpszZipFullName, CStringArray& szFilePathArr, LPCTSTR lpszUnZipPath)

{

TCHAR buffer[MAX_PATH] = {0};

CString strUnZipPath = lpszUnZipPath;

DWORD zResult = ZR_OK;

if (!strUnZipPath.IsEmpty())

{

// 如果文件路径不存在先创建,存在不做任何修改

SHCreateDirectoryEx(NULL, lpszUnZipPath, NULL);

}

else

{

GetCurrentDirectory(MAX_PATH, (LPTSTR)&buffer);

strUnZipPath = buffer;

SHCreateDirectoryEx(NULL, strUnZipPath, NULL);

}

HZIP hz = OpenZip(lpszZipFullName, 0);

ZIPENTRY ze;

GetZipItem(hz, -1, &ze);

int numitems = ze.index;

for (int zi = 0; zi < numitems; zi++)

{

ZIPENTRY ze;

GetZipItem(hz,zi,&ze);

zResult = UnzipItem(hz, zi, (CString)strUnZipPath+_T("")+ze.name);

szFilePathArr.Add(ze.name);

if (zResult != ZR_OK)

{

#ifndef _UNICODE

// 判断文件是否存在

if (_access(szFilePathArr[zi], 0))

{

// 文件不存在的时候

return zResult;

}

#else

if (_access((char *)(LPTSTR)(LPCTSTR)szFilePathArr[zi], 0))

{

// 文件不存在的时候

return zResult;

}

#endif

}

}

CloseZip(hz);

return zResult;

}

ZRESULT DirToZip(LPCTSTR lpszSrcPath, LPCTSTR lpszZipName, HZIP& hz, LPCTSTR lpszDestPath)

{

static CString strFileName;

g_nCount++;

DWORD zResult = ZR_OK;

TCHAR buffer[MAX_PATH] = {0};

CString strDestPath = lpszDestPath;

if (g_nCount == 1)

{

// 这里边的只执行一次

if (!strDestPath.IsEmpty())

{

// 如果解压路径文件夹不存在 先创建,存在 不做任何修改

SHCreateDirectoryEx(NULL, lpszDestPath, NULL);

}

else

{

GetCurrentDirectory(MAX_PATH, (LPTSTR)&buffer);

strDestPath = buffer;

SHCreateDirectoryEx(NULL, strDestPath, NULL);

}

hz = CreateZip((CString)strDestPath+_T("")+(CString)lpszZipName, 0);

}

HANDLE file;

WIN32_FIND_DATA fileData;

file = FindFirstFile((CString)lpszSrcPath+_T("*.*"), &fileData);

FindNextFile(file, &fileData);

while (FindNextFile(file, &fileData))

{

// 如果是一个文件目录

if(fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

{

if (strFileName.IsEmpty())

{

ZipAddFolder(hz, fileData.cFileName);

}

else

{

ZipAddFolder(hz, strFileName+_T("")+fileData.cFileName);

}

strFileName = fileData.cFileName;

// 存在子文件夹 递归调用

DirToZip((CString)lpszSrcPath+_T("")+ fileData.cFileName, lpszZipName, hz, lpszDestPath);

strFileName.Empty();

}

else

{

CString strTempPath;

strTempPath.Format(_T("%s%s"), (CString)lpszSrcPath, fileData.cFileName);

if (strFileName.IsEmpty())

{

ZipAdd(hz, fileData.cFileName, strTempPath);

}

else

{

ZipAdd(hz, strFileName+_T("")+fileData.cFileName, strTempPath);

}

if (zResult != ZR_OK)

{

return zResult;

}

}

}

return zResult;

}

ZRESULT CompressDirToZip(LPCTSTR lpszSrcPath, LPCTSTR lpszZipName, LPCTSTR lpszDestPath)

{

HZIP hz;

DWORD zResult = ZR_OK;

zResult = DirToZip(lpszSrcPath, lpszZipName,hz, lpszDestPath);

if(zResult == ZR_OK)

{

CloseZip(hz);

}

g_nCount = 0;

return zResult;

}

}

【C++Zip压缩解压缩示例(支持递归压缩)】相关文章:

基于select、poll、epoll的区别详解

c++中string类成员函数c_str()的用法

深入C语言内存区域分配(进程的各个段)详解

C++中this指针的用法及介绍

深入理解二叉树的非递归遍历

浅析C++中memset,memcpy,strcpy的区别

浅析C语言中printf(),sprintf(),scanf(),sscanf()的用法和区别

基于errno返回值的对应错误码的详细介绍

C++中const的实现机制深入分析

C/C++实现矩阵的转置(示例代码)

精品推荐
分类导航