0

我有这个要求要解决。用户输入一个包含文本文件的加密 zip 文件(仅 zip 文件被加密,而不是其中的内容)。该函数应使用提供的密码或密钥解密 zip 文件,然后将文件作为字符数组解压缩到内存并返回指向字符的指针。

我浏览了所有提供的建议,包括使用 Minizip、microzip、zlib。但我仍然不清楚什么最适合我的要求。

到目前为止,我已经实现了使用密码解密 zip 文件并将 zip 文件转换为字符串。我打算将此字符串用作 zip 解压缩器的输入并将其提取到内存中。但是,我不确定我的方法是否正确。如果有更好的方法,请提供您的建议以及您对在我的 C++ 应用程序中使用的库的建议。

https://code.google.com/p/microzip/source/browse/src/microzip/Unzipper.cpp?r=c18cac3b6126cfd1a08b3e4543801b21d80da08c

http://www.winimage.com/zLibDll/minizip.html

http://www.example-code.com/vcpp/zip.asp

http://zlib.net/

非常感谢

请提供您的建议。

4

2 回答 2

2

零使用zlib。这个链接帮助我做到了。想分享这个,以便它可以帮助某人。就我而言,我直接使用该缓冲区而不是写入文件。 http://www.gamedev.net/reference/articles/article2279.asp

#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>

using namespace std;

    int main(int argc, char* argv[])
    {

        char c;
        if ( argc != 2 )
        {
         cout << "Usage program.exe zipfilename" << endl;
         return 0;
        } 

        FILE * FileIn;
        FILE * FileOut;
        unsigned long FileInSize;
        void *RawDataBuff;


        //input and output files
        FileIn = fopen(argv[1], "rb");
        FileOut = fopen("FileOut.zip", "w");

        //get the file size of the input file
        fseek(FileIn, 0, SEEK_END);
        FileInSize = ftell(FileIn);

        //buffers for the raw and compressed data</span>
        RawDataBuff = malloc(FileInSize);
        void *CompDataBuff = NULL;

        //zlib states that the source buffer must be at least 0.1 times larger than the source buffer plus 12 bytes
        //to cope with the overhead of zlib data streams
        uLongf CompBuffSize = (uLongf)(FileInSize + (FileInSize * 0.1) + 12);
        CompDataBuff = malloc((size_t)(CompBuffSize));

        //read in the contents of the file into the source buffer
        fseek(FileIn, 0, SEEK_SET);
        fread(RawDataBuff, FileInSize, 1, FileIn);

        //now compress the data
        uLongf DestBuffSize;
        int returnValue;
        returnValue = compress((Bytef*)CompDataBuff, (uLongf*)&DestBuffSize,
            (const Bytef*)RawDataBuff, (uLongf)FileInSize);

        cout << "Return value " << returnValue;
        //write the compressed data to disk
        fwrite(CompDataBuff, DestBuffSize, 1, FileOut);
        fclose(FileIn);
        fclose(FileOut);

         errno_t err;

       // Open for read (will fail if file "input.gz" does not exist)
       if ((FileIn = fopen("FileOut.zip", "rb")) == NULL) {
            fprintf(stderr, "error: Unable to open file" "\n");
            exit(EXIT_FAILURE);
       }
       else
          printf( "Successfully opened the file\n" );


       cout << "Input file name " << argv[1] << "\n";


       // Open for write (will fail if file "test.txt" does not exist)
       if( (err  = fopen_s( &FileOut, "test.txt", "wb" )) !=0 )
       {
           printf( "The file 'test.txt' was not opened\n" );
           system ("pause");
           exit (1);
       }
       else
          printf( "The file 'test.txt' was opened\n" );

       //get the file size of the input file
       fseek(FileIn, 0, SEEK_END);
       FileInSize = ftell(FileIn);

       //buffers for the raw and uncompressed data
       RawDataBuff = malloc(FileInSize);
       char *UnCompDataBuff = NULL;

       //RawDataBuff = (char*) malloc (sizeof(char)*FileInSize);
       if (RawDataBuff == NULL)
       {
           fputs ("Memory error",stderr);
           exit (2);
       }
       //read in the contents of the file into the source buffer
       fseek(FileIn, 0, SEEK_SET);
       fread(RawDataBuff, FileInSize, 1, FileIn);

       //allocate a buffer big enough to hold the uncompressed data, we can cheat here
       //because we know the file size of the original

       uLongf UnCompSize = 482000; //TODO : Revisit this
       int retValue;
       UnCompDataBuff = (char*) malloc (sizeof(char)*UnCompSize);
       if (UnCompDataBuff == NULL)
       {
           fputs ("Memory error",stderr);
           exit (2);
       }

       //all data we require is ready so compress it into the source buffer, the exact
       //size will be stored in UnCompSize
       retValue = uncompress((Bytef*)UnCompDataBuff, &UnCompSize, (const Bytef*)RawDataBuff, FileInSize);

       cout << "Return value of decompression " << retValue << "\n";
       //write the decompressed data to disk
       fwrite(UnCompDataBuff, UnCompSize, 1, FileOut);

       free(RawDataBuff);
       free(UnCompDataBuff);
       fclose(FileIn);
       fclose(FileOut);

       system("pause");
       exit (0);
    }
于 2013-07-10T08:35:02.380 回答
0

大多数(如果不是全部)最流行的 ZIP 工具也支持命令行使用。所以,如果我是你,我会system从你的 C++ 程序中运行一个命令,使用这些流行的 ZIP 工具之一解压缩和解密文件。文本文件解压缩和解密后,您可以将其从磁盘加载到内存中,以便从那里进一步处理它。一个简单但有效的解决方案。

于 2013-07-04T19:17:37.220 回答