简体   繁体   English

C ++:需要有关解密zip文件并将内容提取到内存的帮助

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

I have this requirement to be addressed. 我有这个要求要解决。 User inputs a encrypted zip file (only zip file is encrypted and not contents inside it) which contains a text file. 用户输入一个包含文本文件的加密的zip文件(只有zip文件是加密的,而不是其中的内容)。 The function should decrypt the zip file using the password or key provided and then unzip the file to memory as an array of chars and return the pointer to the char. 该函数应使用提供的密码或密钥解密zip文件,然后将其作为char数组解压缩到内存中,并将指针返回char。

I went through all the suggestions provided including using Minizip, microzip, zlib. 我仔细阅读了所提供的所有建议,包括使用Minizip,microzip,zlib。 But I am still not clear on what is the best fit for my requirement. 但是我仍然不清楚最适合我的要求的是什么。

So far I have implemented decrypting the zip file using the password and converting the zip file to a string. 到目前为止,我已经实现了使用密码解密zip文件并将zip文件转换为字符串的方法。 I am planning to use this string as an input to zip decompresser and extract it to memory. 我打算将此字符串用作zip解压缩器的输入并将其提取到内存中。 However, I am not sure if my approach is right. 但是,我不确定我的方法是否正确。 If there are better ways to do it, please provide your suggestions along with your recommendations on the library to use in my C++ application. 如果有更好的方法,请在库中提供您的建议和建议,以供我的C ++应用程序使用。

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

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

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

http://zlib.net/ http://zlib.net/

Many thanks 非常感谢

Please provide your suggestions. 请提供您的建议。

Most, if not all, of the most popular ZIP tools also support command-line usage. 大多数(如果不是全部)最受欢迎的ZIP工具也支持命令行使用。 So, if I were you I would just run a system command from your C++ program to unzip and decrypt the file using one of these popular ZIP tools. 因此,如果您是我,则只需使用这些流行的ZIP工具之一从您的C ++程序中运行system命令来解压缩和解密文件。 After the textfile has been unzip'ed and decrypted you can load it from the disk into internal memory to further process it from there. 将文本文件解压缩并解密后,您可以将其从磁盘加载到内部存储器中,以从那里进行进一步处理。 A simple solution, but efficient. 一个简单的解决方案,但有效。

Zero'd on using zlib. 零使用zlib。 This link helped me to do that. 该链接帮助我做到了。 Thought of sharing this so that it can help someone. 想共享它,以便它可以帮助某人。 In my case I am using that buffer directly instead of writing to a file. 就我而言,我直接使用该缓冲区,而不是写入文件。 http://www.gamedev.net/reference/articles/article2279.asp 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM