C++ : Need help on decrypting a zip file and extracting the contents to memory

标签 c++ memory unzip

我有这个要求需要解决。用户输入一个加密的 zip 文件(仅加密 zip 文件,而不加密其中的内容),其中包含文本文件。 该函数应使用提供的密码或 key 解密 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/

非常感谢

请提出您的建议。

最佳答案

使用 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);
    }

关于C++ : Need help on decrypting a zip file and extracting the contents to memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17476815/

相关文章:

c++ - 为什么下面使用 auto_ptr 的异常安全代码不同?

C++ 动态数组仅 getter 与重载的 [] 运算符一起使用

ios - 位图 CGContext 绘图不断增加内存使用量

c++ - 在一个 C++ 程序中有不同的新运算符 : How to? 坏主意?

excel - 如何在vba中打开存档中的文件而不解压存档

c++ - 使用 BNF 语法提取信息

android - 如何将图片放入应用程序的内存中?

java - 在Java中递归地解压缩不同文件夹中的多个gzip文件

Python,解压 .jar 文件,不起作用

C++ unix 32 位到在 x86_64 芯片(64 位服务器)上运行的 Red Hat Linux