C/C++Zlib 解压文件

版权声明:转载请声明出处,谢谢~ https://blog.csdn.net/what951006/article/details/81905330

还是记下来吧,用了这么N多次了,博客方便点
.h头文件

#include <string>
using namespace std;

class ZlibClass
{
public:
    static bool UnCompress(const string &pSzSrcFile, const string & pSzPSW, const string &pSzOutDir);
};

.cpp文件

#include "ZlibClass.h"

#include <direct.h> //_mkdir函数的头文件
#include <io.h>     //_access函数的头文件
#include <windows.h>

#include "zlib\unzip.h"
#include "zlib\zlib.h"
#include "zlib\zip.h"

void FormatDirectorys(LPSTR lpszDirectorys)
{
    if (lpszDirectorys != NULL)
    {
        int i = 0;

        while (lpszDirectorys[i] != ('\0'))
        {
            if (lpszDirectorys[i] == ('/'))
            {
                lpszDirectorys[i] = ('\\');
            }

            i++;
        }
    }
}

void CreateDir(const char *dir)
{
    int m = 0, n;
    string str1, str2;
    str1 = dir;
    str2 = str1.substr(0, 2);
    str1 = str1.substr(3, str1.size());
    while (m >= 0)
    {
        m = str1.find('\\');

        str2 += '\\' + str1.substr(0, m);
        n = _access(str2.c_str(), 0); //判断该目录是否存在
        if (n == -1)
        {
            _mkdir(str2.c_str());     //创建目录
        }

        str1 = str1.substr(m + 1, str1.size());
    }
}


bool ZlibClass::UnCompress(const string &pSzSrcFile, const string & pSzPSW, const string &pSzOutDir)
{
    char szSrcFile[MAX_PATH] = { 0 };
    char szOutDir[MAX_PATH] = { 0 };
    strcpy(szSrcFile, pSzSrcFile.c_str());
    strcpy(szOutDir, pSzOutDir.c_str());



    char szDestFile[MAX_PATH] = ("");
    char szFile[50] = "0";


    if (pSzSrcFile.empty() && pSzOutDir.empty())
        return false;

    FormatDirectorys(szSrcFile);  // 格式化字符串
    FormatDirectorys(szOutDir);  // 格式化字符串

    unzFile uf = NULL;
    unz_global_info gi;
    unz_file_info FileInfo;
    uf = unzOpen(szSrcFile);
    int result = unzGetGlobalInfo(uf, &gi);
#define BUFF_SZIE 16384
    char szBuf[BUFF_SZIE] = "0";

    for (int i = 0; i < gi.number_entry; ++i)
    {
        HANDLE h = NULL;
        if (result != UNZ_OK)
            return false;

        if (unzGetCurrentFileInfo(uf, &FileInfo, szFile, 50, NULL, 0, NULL, 0) != UNZ_OK)
            return false;
        if (!(FileInfo.external_fa & FILE_ATTRIBUTE_DIRECTORY)) //文件,否则为目录
        {
            // 创建文件
            FormatDirectorys(szFile);
            sprintf(szDestFile, ("%s\\%s"), szOutDir, szFile);
            bool bCreateFile = true;
            if (!pSzPSW.empty())
            {
                result = unzOpenCurrentFilePassword(uf, pSzPSW.c_str()); /* 有密码 */
            }
            else
            {
                result = unzOpenCurrentFile(uf); /* 无密码 */
                                                 // 感觉有的时候,即使压缩包有密码,但是解压时,不输入密码,也能解压成功?Wht?是因为密码太简单的缘故么?还是因为zlib破解了?
            }
            while (true)
            {
                int size = unzReadCurrentFile(uf, szBuf, BUFF_SZIE); //读取内容
                if (size >= 0)
                {
                    if (bCreateFile)
                        h = CreateFileA(szDestFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
                    bCreateFile = false;
                }

                if (size < 0)
                {
                    if (h)
                    {
                        CloseHandle(h);
                    }
                    unzCloseCurrentFile(uf);  //关闭当前文件
                    return false;
                }
                if (size == 0)
                {
                    break;
                }
                DWORD writ;
                BOOL bRes = WriteFile(h, szBuf, size, &writ, NULL);
                if (!bRes)
                {
                    break;
                }
            }
            CloseHandle(h);
            unzCloseCurrentFile(uf);  //关闭当前文件
        }
        else
        {
            //创建文件夹
            char szDestDir[MAX_PATH] = ("");
            FormatDirectorys(szFile);

            sprintf(szDestDir, ("%s\\%s"), szOutDir, szFile);
            CreateDir(szDestDir);
        }
        unzGoToNextFile(uf);
    }
    return true;
}

把zlib的码放进来就okay

猜你喜欢

转载自blog.csdn.net/what951006/article/details/81905330