zoukankan      html  css  js  c++  java
  • C++ Zip压缩解压缩[支持递归压缩]

    我这个是才才用别的第三方函数封装而成;

    简介:

    支持 UNCODE, ANSCII

    支持压缩文件夹

    支持递归压缩

    第三方函数、头文件、测试工程下载地址:http://pan.baidu.com/share/link?shareid=186662&uk=3037189616

    // 文件名: 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;
        }
    }
  • 相关阅读:
    P2167 [SDOI2009]Bill的挑战
    P2153 [SDOI2009]晨跑
    洛咕 P2155 [SDOI2008]沙拉公主的困惑
    洛咕 P2465 [SDOI2008]山贼集团
    洛咕 P2463 [SDOI2008]Sandy的卡片
    洛咕 P4556 [Vani有约会]雨天的尾巴
    CF418D Big Problems for Organizers
    CF28D Don't fear, DravDe is kind
    CF97C Winning Strategy
    P4427 [BJOI2018]求和
  • 原文地址:https://www.cnblogs.com/calm2012/p/2846781.html
Copyright © 2011-2022 走看看