15

I was reading about compression in programs and I started to create a new simple project, a zipper (just a zipper, not an unzipper), but I only found zLib, and it's for C. I know that C libraries can be used in C++, but I like to use C++ libraries. Does anyone know a good one to suggest?

Best Regards.

6
  • 5
    If you compile the C library with a C++ compiler it will probably work (and then be C++).
    – Carl Norum
    Oct 14, 2009 at 0:35
  • 3
    If you are going to create a compression utility that doesn't/can't decompress, what could be better than rm BIG_FILE? Oct 14, 2009 at 0:36
  • probably? not probably... it will work.
    – sean riley
    Oct 14, 2009 at 0:36
  • @Carl It's a good idea. @Mark lol Oct 14, 2009 at 0:37
  • Looks like I should have made that an answer, I'm missing out on reputation!
    – Carl Norum
    Oct 14, 2009 at 0:40

7 Answers 7

12

You could do this easily using Boost iostream zlib filter

10

Most compression libraries that I know of are written in C for two reasons: one, the general age of good compression algorithms; and two, the high portability (and stability) of C across platforms.

I suggest any of the following. If you want good licenses select one of the top two, otherwise if you're open to using GPL code pick one of the last two.

2

I would suggest using zlib. It is designed for C, but it works fine in C++.

Using native C++ libraries really only helps when the library is sufficiently big and complex that it can benefit from object oriented design. zlib is relatively simple, and doesn't need object oriented features.

2

Use libzip: http://www.nih.at/libzip/ The license is pretty permissive and it does all you need, from C or C++ code.

6
  • I have checked out the source code for libzip and it is yukky. I dont know if I missed a variable setup but src.stat_error had undefined values right through zip_open() method; and src.stat_error was used in a comparision twice. Apart from that, it initializes variables multipe times, sometimes even before using the variable. Makes tonnes of function_pointer calls. Some uses of function_pointers felt stupid and the function calls felt circular in nature; even though they weren't. And libzip does all allocations in zip_open method; which is wasteful since I only want to enumerate files. Sep 12, 2023 at 10:35
  • Like libzip never had to do all those allocation in one go. It could have simply allocated an index table enumerated all the elements of the zip file; and allocate and fetch the required data later. Even better would be to add members to the index table as soon as they are enumerated later by the user. libzip would then have an additional get_next_entry() function. And zip_stat_index is a waste of processor time and memory; to return every information, when I just need one piece of information. Sep 12, 2023 at 10:40
  • And finally, I was studying the code thoroughly to decipher how libzip got the end of central directory header. At first I was thrilled, but then I figured that, trying to find End Of Central Directory Signature from front of file must lead to errors. At that point I was too tired to analyze the code further. Sep 12, 2023 at 10:50
  • It's just a shame that zip file format was designed poorly. If you read it from the front, local header is not guaranteed to be there, local headers you encounter might be dangling; and searching for ECOD signature; compressed data could contain that signature. Reading zip backwards, well ECOD comment comes before comment length. So you can't just seek past length comments and then seek 22 bytes to the ECOD signature. Sep 12, 2023 at 10:56
  • I see three possible solutions. 1. Seek clamp(file_length-65578,0) SEEK_SET. read in 4 bytes and compare them to ECOD sig. Then validate the rest of ECOD members. since ECOD marks the end of a zip file accordingg to wikipedia, comment pos + comment_len should be end of file. If comment length is not 65546 chars, some time is wasted. 2. Go to end of file - 22. Store bytes in a 25 bytes array. On first iteration only compare the first 4 bytes. If fail. seek back another 22 bytes. move bytes 1-3 to index 22 in array. copy 22 bytes in array. compare the entire array. repeat until you find ECOD sig Sep 12, 2023 at 11:19
2

Zlib is good, but you might want to check the LZMA SDK as well (you could compress to .7z format, in addition to zip)

1

zlib is strongly recommended. It is well written and the interface is fairly clean. I do not see how much a C++ wrapper can simplify APIs. Furthermore, in my view, zlib achieves a good balance between (de)compression speed and file size. Bzip2 is much slower while the compression ratio of LZO and UCL is worse.

2
  • Zlib interface looks a bit weird to me. I prefer the boost::iostreams wrap, much saner.
    – piotr
    Oct 14, 2009 at 5:12
  • Is Zlib still confined to Deflate compression method? If so, can I use Delate method to open any zip archive entry? Sep 12, 2023 at 10:26
1

This is what I like (it is really fast and has good compression rate): http://www.quicklz.com/

2
  • This seems to work perfectly and easily, thanks.
    – Aidan
    Nov 9, 2020 at 16:51
  • broken link ~~~
    – frank
    Feb 13 at 23:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.