The C++Zip Zip example of of supports recursive compression

  • 2020-04-02 02:01:02
  • OfStack

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131124114132.jpg? 20131024114652 ">

Third party function, header files, test, project download address: http://pan.baidu.com/s/1gSfKo


//File name: zipfunction.h
#pragma once
#include "zip.h"
#include "unzip.h"
namespace ZipUtils
{
    // ------------------------------------------------------------------------------------------------------------------------
    // Summary:
    //& have spent & have spent Unzip the zip file to the specified path and return the unzip file path and file name.
    // Parameters:
    //   lpszZipFullName   -  To extract the  zip Zip package folder path and zip Name of the compression ;  Such as "D://00//1.zip" . 
    //& have spent & have spent SzFilePathArr  & have spent & have spent & have spent - the file name of the saved unzipped file; Such as "1. JPG". & have spent & have spent & have spent
    //   lpszUnZipPath     -  Unzip the file   Full path to the location ;  Such as   " D://01 " 
    //& have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent When this parameter is omitted, it is unzipped to the folder where the exe program resides by default.
    // Returns:
    //& have spent & have spent Unzip returns ZR_OK on success, unzip returns an error code on failure.
    // ------------------------------------------------------------------------------------------------------------------------
    ZRESULT ExtractZipToDir(LPCTSTR lpszZipFullName, CStringArray& szFilePathArr, LPCTSTR lpszUnZipPath = NULL);
    // ------------------------------------------------------------------------------------------------------------------------
    // Summary:
    //& have spent & have spent Compress the file under the specified path and save the zip package to the specified path.
    // Parameters:
    //   lpszSrcPath        -  The path where the file is to be compressed ;  Such as "D://00" . 
    //& have spent & have spent LpszDestPath  & have spent & have spent & have spent & have spent & have spent - the path to store the compressed package after the compression is completed.
    //& have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent When this parameter is omitted, the default path is the path to the file where the exe program resides.
    //& have spent & have spent LpszZipName  & have spent & have spent & have spent & have spent & have spent & have spent - name of compression after completion of compression; Such as "MySkin.zip".
    // Returns:
    //& have spent & have spent ZR_OK is returned on success of compression, and an error code is returned on failure of compression.
    // ------------------------------------------------------------------------------------------------------------------------
    ZRESULT CompressDirToZip(LPCTSTR lpszSrcPath, LPCTSTR lpszZipName, LPCTSTR lpszDestPath = NULL);
}


#include "stdafx.h"
#include "ZipFunction.h"
#include <io.h>
namespace ZipUtils
{
    //The global variable
    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())
        {
            //If the file path does not exist create first, exist without any modification
            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 
                //Determine if the file exists
                if (_access(szFilePathArr[zi], 0))    
                {
                    //When the file doesn't exist
                    return zResult;
                }
#else
                if (_access((char *)(LPTSTR)(LPCTSTR)szFilePathArr[zi], 0))    
                {
                    //When the file doesn't exist
                    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)
        {
            //This side is only executed once
            if (!strDestPath.IsEmpty())
            {
                //If the unzip path folder does not exist create first, the existence does not make any changes
                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 it's a file directory
            if(fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if (strFileName.IsEmpty())
                {
                    ZipAddFolder(hz, fileData.cFileName);
                }
                else
                {
                    ZipAddFolder(hz, strFileName+_T("\")+fileData.cFileName);
                }

                strFileName = fileData.cFileName;
                //Recursive calls to subfolders exist
                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;
    }
}


Related articles: