Thread: How to unzip/zip a file in C/C++ that was compressed using sharpZipLib

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    3

    How to unzip/zip a file in C/C++ that was compressed using sharpZipLib

    I've been at it for a week now and I'm lost among all the libraries and all the formats which is very frustrating since what I need is very simple.
    I have a file that is zipped using this code in C#, with sharpZipLib:

    Code:
    ZipOutputStream zipOutputStream = new ZipOutputStream(zippedData);
    zipOutputStream.SetLevel(3);
    ZipEntry entry = new ZipEntry("name");
    zipOutputStream.PutNextEntry(entry);
    zipOutputStream.CloseEntry();
    zipOutputStream.Close();
    Now, I need to unzip this file in C++.
    Once that is done, I need to zip it back up again, preferably in a way that C# unzip code won't be any slower to unzip it than it would be were it zipped by the C# zip code. This is the original C# code for unzipping:

    Code:
    ZipInputStream zipInputStream = new ZipInputStream(zippedData);
    zipInputStream.GetNextEntry();
    I've tried miniz, but that's zlib, not zip, which apparently is not the same. The I struggled with minizip as well, just to realize it was also zlib. I've also tried some other tools/libraries, but I was unable to write the code that would just zip/unzip a file. Some of them would require input to be a file (which I don't have, I get my data as a char[]) (ziplib), while others were to heavyweight for this kind of task.

    I have the data I need to unzip as an array char[] - but I can change it into some other related type. Any type of output would also be fine, if I can get it to char[], vector or something similar. The files are small enough (<1MB) not to need any buffers or some extensive memory management. It's also just a single entry.
    It's literally the simplest example possible.

    Do you know of any library that is compatible with sharpZipLib (preferably while being fast and lightweight) which I can use to do this?

  2. #2
    Guest
    Guest
    Looking at miniz's description, it should be up to the task, and it's probably the one I'd give a try:
    • A fairly complete (but totally optional) set of .ZIP archive manipulation and extraction API's. The archive functionality is intended to solve common problems
    It might not be anywhere near as clean as your C# example though, given that it's written in C, not C++. Just do a websearch for examples, and you might be able to recreate the procedure in your program.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    3
    Miniz works with zlib, not zip. It's amazing that there's no effin' library for something so basic like this. I can't find a single one, at least not one that will read it from memory.

    Besides, I tried with miniz, it didn't work.

  4. #4
    Guest
    Guest
    ZIP uses the deflate algorithm which zlib supports. The ZIP format is just more than the core algorithm, but miniz explicitly states that i handles ZIP archives. I don't have the time to prove your wrong, but maybe someone else can. I agree that there should be a convenient library for these kind of tasks.

  5. #5
    Guest
    Guest
    Maybe this library is of interest? It looks to be focused on ZIP archives.

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    3
    Here's the code I've been trying to use. I can't figure out why doesn't it work. It fails if I use any other flag. But even if it's successful, it doesn't return the size of the stream, so it's useless again (outBytes gets set to 0):


    Code:
    unsigned char *Unzip::unzipData(std::vector<unsigned char> data)
    {
    
    std::vector<unsigned char> outBuffer(1024 * 1024);
    
        tinfl_decompressor inflator;
        tinfl_status status;
        tinfl_init(&inflator);
    
        size_t inBytes = data.size();
        size_t outBytes = 1024 * 1024;
    
        status = tinfl_decompress(&inflator,
            (const mz_uint8 *)&data[0],
            &inBytes,
            (mz_uint8 *)outBuffer.data(),
            (mz_uint8 *)outBuffer.data(),
            &outBytes,
            TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); <- I have no idea which flags to use. This is the only one that doesn't fail, but the outBytes is still 1.
    
        outBuffer.resize(outBytes);
        outBuffer.shrink_to_fit();
    
        return /*something when the code actually starts working*/
    }
    This is the raw data I'm feeding it:

    Data - Pastebin.com (EDIT: there's a byte missing right there at the beginning, it's 50 (char "P"))

    Any idea why the code doesn't work?

    I'm sorry, I've been at this for a week, and it's supposed to be the simplest thing. I'm tired and annoyed of it and I may come off as rude, which I don't want. Also, the linked library above deals with zip files. I have an array already loaded into memory. Writing it to disk only to be read again isn't really viable... The miniz seems like the simple solution I'm looking for, but I don't know why doesn't it work...
    Last edited by Karlovsky120; 05-20-2017 at 03:50 PM.

  7. #7
    Guest
    Guest
    Yeah, no need to apologize, I know exactly how frustrating these things can be. I once wrote a wrapper for a part of zlib (piece-wise gzip decompression), because of how convoluted the library is to work with.

    Alas, I really don't have to time to experiment with this myself. If nobody else can chime in, and you cannot find a solution, consider trying another forum to get more eyes on the code. Questions about C and C++ themselves usually find answers pretty quickly, but this is knowledge requiring first hand experience to answer well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compressed file project
    By paltech in forum C++ Programming
    Replies: 6
    Last Post: 05-20-2008, 01:35 PM
  2. working with compressed files
    By tonyjeffs in forum C Programming
    Replies: 4
    Last Post: 10-02-2007, 11:59 AM
  3. Linux Unzip
    By zzz in forum Tech Board
    Replies: 11
    Last Post: 02-24-2006, 06:46 PM
  4. Reading compressed files
    By clancyPC in forum C Programming
    Replies: 4
    Last Post: 10-04-2005, 10:38 AM
  5. Compressed Earth Perspective
    By DISGUISED in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 11-22-2003, 01:27 PM

Tags for this Thread