8

I want my program to be able to use zlib, lzma, lzo and bzip2 compression algorithms.

Is there any compression library for C which simlifies working with multiple algorithms (like libmcrypt supporting multiple encryption modes and algorithms)?

Expecting something like this:

struct compressor c;
universal_compressor_init(&c, "lzma", 7 /* compression level */);
universal_compressor_compress(&c, inputbuf, inputsize, outputbuf, &outputsize);
universal_compressor_save_state(&c, statebuf, &statesize);

Note: It is not about zip/rar/7z/tar/cpio and other archive formats, it's about compression of raw buffers. Think of compressed networking protocol or about random access to compressed block device (like cloop).

2 Answers 2

3

LibArchive fulfills your requirements.

From the introduction:

The libarchive library features:

  • Support for a variety of archive and compression formats.
  • Robust automatic format detection, including archive/compression combinations such as tar.gz. ...

EDIT: for handling raw streams (at least until they split libfilter from libarchive), consider using Boost::iostreams or libbu++ (just found on GitHub)

Sign up to request clarification or add additional context in comments.

8 Comments

Is it suitable for many little independent headerless compressed buffers?
Looks like primary goal of libarchive is working with archives as bundles of files, not as compression embedded in some protocol.
Can I use libarchive filters (without the rest libarchive) with libarchive's public API?
Unfortunately, not: that feature is planned for a future release. Updated answer.
If I understood what you are about to do, you might find squashfs very interesting with respect to block compression.
|
0

I recommend 7-zip (http://www.7-zip.org/sdk.html). I haven't found anything to beat it for both compression speed and size.

5 Comments

I want to compress big file preserving random access inside it. So I want to compress little blocks and store index. Does 7-zip provide an API to do it efficiently?
Yes, that was one of the needs I had (fast single file extraction from large archives). You might post on the 7-zip forum for more info: sourceforge.net/p/sevenzip/discussion/45797
Will it really handle 13107200 little files of 4096 bytes each well?
Good question - I'm sure it would only take a few lines of code to find out.
7-zip's primary algorithm (lzma) is in fact listed in question. The purpose of the library in question is to be able to switch algorithms easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.