C++Zip壓縮解壓縮樣本(支援遞迴壓縮)_C 語言

來源:互聯網
上載者:User



第三方函數、標頭檔、測試工程下載地址: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;
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.