无法将文件添加到.zip文件中

时间:2018-04-24 23:00:04

标签: c++ windows zlib

我尝试使用下面的示例代码(来自How do I use Minizip (on Zlib)?)。

从特定文件夹添加文件时不起作用。 zipfile已创建,但内容中没有任何内容。我不明白我错过了什么。 我想压缩目录"c:\\temp1\\*"中的所有文件。

如果我硬编码文件名,如下面2行所示,那么它们会被添加到zip中。

const char* Filenames[] = {"Readme.txt", "foo.bin"};
unsigned int nCount = sizeof( Filenames )/sizeof( char* );

这是我的代码:

#include "stdafx.h"
#include <vector>
#include <string>
#include <fstream>
#include <windows.h>
#include <comdef.h> 

#include <Shlwapi.h>
#pragma comment (lib, "Shlwapi.lib")

#include "zlib-1.2.11\zlib.h"
#include "zlib-1.2.11\zip.h"

using namespace std;


wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
{
    wchar_t* wString=new wchar_t[4096];
    MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
    return wString;
}

std::wstring CombinePaths(std::wstring const &pattern, LPCWSTR filename) {
    std::wstring tmp(pattern);
    tmp.push_back('\0');
    PathRemoveFileSpec(&tmp[0]);
    std::wstring retVal(MAX_PATH, '\0');
    PathCombine(&retVal[0], tmp.c_str(), filename);
    return retVal.c_str();
}

int zip()
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind;

    zipFile myZip = zipOpen64("MyTest.zip", APPEND_STATUS_CREATE);

    std::wstring myDir(L"C:\\temp1\\*");

    hFind = FindFirstFile(myDir.c_str(), &FindFileData);

    typedef std::vector<std::wstring> listofFiles;
    listofFiles ff;
   // List all the files in the directory
   do
   {
       std::wstring fullPath = CombinePaths(myDir, FindFileData.cFileName);
       ff.push_back(fullPath);
   }
   while (FindNextFile(hFind, &FindFileData) != 0);
   FindClose(hFind);


   bool myreturn = true;

    // add files to the zip file
   for ( unsigned int i = 0; i < ff.size(); i++ )
    {

        ifstream file(ff[i].c_str(), ios::binary | ios::in);
        _bstr_t b(ff[i].c_str());
        const char* filename = b;

        FILE *stream;
        errno_t err = fopen_s(&stream, filename, "r");
        if (file.is_open() )
        {
            file.seekg(0, ios::end);
            size_t size = file.tellg();
            file.seekg(0, ios::beg);

            std::vector<char> buffer(size);
            if (size == 0 || file.read(&buffer[0], size))
            {
                zip_fileinfo zi = {0};
                if (ZIP_OK == zipOpenNewFileInZip(myZip, filename, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION))
                {
                    if (zipWriteInFileInZip(myZip, size == 0 ? "" : &buffer[0], (unsigned int) size))
                        myreturn = false;

                    zipCloseFileInZip(myZip);
                    file.close();
                    continue;

                }
                else
                {
                    file.close();
                    return ZIP_ERRNO;
                }
            }
             file.close();
        }
        if (stream != NULL)
            fclose(stream);
    }
   ff.clear();

   zipCloseFileInZip( myZip );
   if (zipClose(myZip, 0))
       return ZIP_BADZIPFILE;

   return ZIP_OK;
}
void unzip()
{
}
int _tmain(int argc, _TCHAR* argv[])
{
    zip();

    unzip();
    return 0;
}

0 个答案:

没有答案