国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++Zip壓縮解壓縮示例(支持遞歸壓縮)

2020-01-26 15:41:34
字體:
來源:轉載
供稿:網友



第三方函數、頭文件、測試工程下載地址: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("http://")+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("http://")+(CString)lpszZipName, 0);
        }

        HANDLE file;
        WIN32_FIND_DATA fileData;
        file = FindFirstFile((CString)lpszSrcPath+_T("http://*.*"), &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("http://")+fileData.cFileName);
                }

                strFileName = fileData.cFileName;
                // 存在子文件夾 遞歸調用
                DirToZip((CString)lpszSrcPath+_T("http://")+ 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("http://")+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;
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泽库县| 锡林郭勒盟| 涿鹿县| 界首市| 绥滨县| 南陵县| 洱源县| 台中市| 磴口县| 黄浦区| 安福县| 东宁县| 突泉县| 岳池县| 建宁县| 赤峰市| 山西省| 水富县| 巴东县| 天长市| 巴中市| 漠河县| 隆尧县| 无为县| 汤原县| 张家港市| 韩城市| 子长县| 西峡县| 平湖市| 昌图县| 通榆县| 芮城县| 顺平县| 宝兴县| 双柏县| 贵阳市| 尚义县| 那坡县| 桂林市| 诸城市|