C++zip Compression Decompression Example (support for recursive compression) _c language

Source: Internet
Author: User



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

Copy Code code as follows:

FileName: ZipFunction.h
#pragma once
#include "Zip.h"
#include "Unzip.h"

Namespace Ziputils
{
// ------------------------------------------------------------------------------------------------------------- -----------
Summary:
Extract the zip file to the specified path and return the extracted file path and file name.
Parameters:
Lpszzipfullname-the folder path and zip compression name of the ZIP compression package to be uncompressed; such as "D://00//1.zip".
Szfilepatharr-The filename of the saved uncompressed file, such as "1.jpg".
Lpszunzippath-The full path of the location where the extracted files are stored; such as "d://01"
When this parameter is omitted, the default is extracted into the folder where the EXE program resides.
Returns:
Decompression successfully returned ZR_OK, decompression failed to return the error code.
// ------------------------------------------------------------------------------------------------------------- -----------
Zresult Extractziptodir (LPCTSTR lpszzipfullname, cstringarray& szfilepatharr, lpctstr lpszUnZipPath = NULL);

// ------------------------------------------------------------------------------------------------------------- -----------
Summary:
Compresses the file under the specified path and saves the compressed package to the specified path.
Parameters:
Lpszsrcpath-The path where the file is to be compressed; such as "d://00".
Lpszdestpath-After the compression is complete, store the path of the compressed package.
When this parameter is omitted, the default deposit path is the path of the file where the EXE program resides.
Lpszzipname-The compressed name, such as "Myskin.zip", when the compression is complete.
Returns:
Compression returned ZR_OK successfully, compression failed to return error code.
// ------------------------------------------------------------------------------------------------------------- -----------
Zresult Compressdirtozip (LPCTSTR lpszsrcpath, LPCTSTR lpszzipname, lpctstr lpszdestpath = NULL);
}

Copy Code code as follows:

#include "stdafx.h"
#include "ZipFunction.h"
#include <io.h>

Namespace Ziputils
{
Global variables
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 making any changes
            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
To determine whether a 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 decompression path folder does not exist first created, 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 is 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;
There are child folder recursive calls
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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.