zlib解压时, 文件夹的判断
发布日期:2021-06-30 22:06:09 浏览次数:2 分类:技术文章

本文共 3434 字,大约阅读时间需要 11 分钟。

unz_file_info fileInfo;

(0 == fileInfo.uncompressed_size)时, 为文件夹. 如果zip包中真有size=0的文件, 那这样判断也不行.

(0 != fileInfo.uncompressed_size)时, 为文件.

用 fileInfo.external_fa 判断不行, 不同的zip文件这个值都不同.

BOOL UnZipFileFromZipRes(const WCHAR * pcZipFilePathName, const WCHAR * pcUnZipDirPathName)    {           int  nUnResult = 1;        std::string strZipFilePathName = ns_base::W2Aex(pcZipFilePathName).c_str();        std::string strUnZipDirPathName = ns_base::W2Aex(pcUnZipDirPathName).c_str();        char szFilePath[MAX_PATH] = {NULL};        unz_file_info fileInfo;        unzFile hUnFile;        string strFullFile = "";        string::size_type pos = 0;        LPBYTE pBuffer = NULL;        unsigned int count = 0;        unsigned int nSize = 1;        FILE* fp = NULL;        hUnFile = unzOpen(strZipFilePathName.c_str());        if (hUnFile == NULL)        {            return FALSE;        }        nUnResult = unzGoToFirstFile(hUnFile);        if ( nUnResult == UNZ_OK )        {            do             {                if ( nUnResult != UNZ_OK)                    break;                nUnResult = unzGetCurrentFileInfo(hUnFile, &fileInfo, szFilePath, sizeof(szFilePath), NULL,0,NULL,0);                if ( nUnResult != UNZ_OK)                    break;                nUnResult = unzOpenCurrentFile(hUnFile);                if ( nUnResult != UNZ_OK)                    break;                strFullFile = strUnZipDirPathName;                strFullFile += szFilePath;                pos = 0;                while ((pos = strFullFile.find_first_of("/", pos)) != string::npos)                {                    strFullFile.replace(pos,1, "\\");                    pos++;                }                if (0 == fileInfo.uncompressed_size)                {                    // 文件大小为0, 有可能是目录. 除非真的是0size的文件                    ns_base::SetDirPermissionToEveryOne(ns_base::A2Wex(strFullFile.c_str()).c_str());                }                else                {                    /// 解压缩不覆盖同名文件, 由调用者删除需要被覆盖的源文件                    if (ns_base::IsFileExist(ns_base::A2Wex(strFullFile.c_str()).c_str()))                    {                        unzCloseCurrentFile(hUnFile);                        continue;                    }                    pBuffer = new BYTE[fileInfo.uncompressed_size];                    if (NULL == pBuffer)                    {                        unzCloseCurrentFile(hUnFile);                        nUnResult = -1;                        break;                    }                    count = 0;                    nSize = 1;                    while (nSize > 0)                    {                        nSize = unzReadCurrentFile(hUnFile, &pBuffer[count], fileInfo.uncompressed_size - count);                        count += nSize;                    }                    fp = fopen(strFullFile.c_str(), "wb");                    if (NULL != fp)                    {                        fwrite(pBuffer, fileInfo.uncompressed_size, 1, fp);                        fclose(fp);                    }                    else                    {                        nUnResult = -1; ///< 不能break, 后面还要释放资源                    }                    delete[] pBuffer;                    pBuffer = NULL;                }                unzCloseCurrentFile(hUnFile);            } while ( unzGoToNextFile(hUnFile) == UNZ_OK );        }        unzClose(hUnFile);        return (UNZ_OK == nUnResult);    }

转载地址:https://lostspeed.blog.csdn.net/article/details/46653221 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:EF AES Library 的封装类
下一篇:interface语法糖

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月15日 15时31分13秒