您的位置:首页 > 其它

note : create zip and add file to it

2014-03-27 02:15 330 查看
在csdn上看了一段将整个目录压缩成一个zip文件的代码片段.

我的应用是建立一个zip, 向里面压入几个文件.

所以我整理了一下,那个csdner的代码,使之符合我的应用.

调用端代码:

LONG WINAPI ExceptionProc(struct _EXCEPTION_POINTERS* ExceptionInfo)
{
    zipFile hZip = NULL;
    std::wstring strDumpZipFilePathName = L"";
    std::wstring strDumpBinFilePathName = L"";
    std::wstring strDumpTxtFilePathName = L"";

    ns_base::GenerateCrashFilePathName(
        strDumpZipFilePathName,
        strDumpBinFilePathName,
        strDumpTxtFilePathName);

    RecordExceptionInfo(
        ExceptionInfo, 
        L"ExceptionProc",
        strDumpBinFilePathName.c_str(),
        strDumpTxtFilePathName.c_str());

    if (ns_base::CreateZipFile(strDumpZipFilePathName.c_str(), hZip, TRUE))
    {
        ns_base::AddFileToZip(hZip, strDumpBinFilePathName.c_str());
        ns_base::DeleteFilePro(strDumpBinFilePathName.c_str());

        ns_base::AddFileToZip(hZip, strDumpTxtFilePathName.c_str());
        ns_base::DeleteFilePro(strDumpTxtFilePathName.c_str());

        ns_base::CloseZipFile(hZip, L"create by ExceptionProc");

        /// use this zip file...(e.g. upload crash info to developer)
    }

    return 0;
}


zip操作功能函数实现

BOOL CreateZipFile(const WCHAR * pcNewZipFilePathName, zipFile & hZip, BOOL bCreateNewZip)
    {
        USES_CONVERSION;
        hZip = NULL;

        if (NULL == pcNewZipFilePathName)
            return FALSE;

        hZip = zipOpen(W2CA(pcNewZipFilePathName), bCreateNewZip ? APPEND_STATUS_CREATE : APPEND_STATUS_ADDINZIP);
        return (NULL != hZip);
    }

    BOOL AddFileToZip(zipFile & hZip, const WCHAR * pcFileAddToZip)
    {
        USES_CONVERSION;
        FILE* srcfp = NULL;

        std::wstring strDriver = L"";
        std::wstring strDir = L"";
        std::wstring strFileName = L"";
        std::wstring strFileNamePostfix = L"";

        zip_fileinfo zi;
        ZeroMemory(&zi, sizeof(zip_fileinfo));

        char szFileNameInZip[MAX_PATH];
        ZeroMemory(szFileNameInZip, sizeof(szFileNameInZip));
        strcat(szFileNameInZip, "/");

        if (NULL == pcFileAddToZip)
            return FALSE;

        ns_base::SplitFilePathName(
            pcFileAddToZip,
            strDriver,
            strDir,
            strFileName,
            strFileNamePostfix);

        strcat(szFileNameInZip, W2A(strFileName.c_str()));
        strcat(szFileNameInZip, W2A(strFileNamePostfix.c_str()));
        if (ZIP_OK != zipOpenNewFileInZip(hZip, szFileNameInZip, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION))
            return FALSE;

        if (NULL != pcFileAddToZip)
        {
            srcfp = fopen(W2A(pcFileAddToZip), "rb");
            if (NULL == srcfp)
                return FALSE;

            char buf[64 * 1024];
            int numBytes = 0;
            while (!feof(srcfp))
            {
                numBytes = fread(buf, 1, sizeof(buf), srcfp);
                zipWriteInFileInZip(hZip, buf, numBytes);
                if(ferror(srcfp))
                    break;
            }

            fclose(srcfp);
        }

        zipCloseFileInZip(hZip);
        return TRUE;
    }

    BOOL CloseZipFile(zipFile & hZip, const WCHAR * pcComment)
    {
        USES_CONVERSION;

        if (NULL == hZip)
            return FALSE;

        zipClose(hZip, (NULL == pcComment) ? "" : W2CA(pcComment));
        return TRUE;
    }


这应用,是建立一个新zip文件, 一次性向里面压入文件.

如果是新建一个zip文件,然后关闭,再打开后,向里面压入文件。发现新建的zip里必须要有一个文件(e.g. readme.txt), 才能成功压入新的文件.

还没有去验证,是否新建一个文件后,建立一个"/"文件后, 是否可以在关闭再打开后,顺利压入新文件.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐