c++iosobjective-c7zipbzip2

Uncompressing bzip2 in an iOS app


I'm working on an iOS app that needs to download ~50 MB of data from time to time. bzip2 gives me the best compression rate (reduces the size to 8 MB).

The problem is: how to decompress the data in the app?

I've done some research - the only two things I found was Keka (but the full source is not public) and C++ source of bzip2 command line tool, which is much too long and too complicated for me to make the necessary adjustments for my app in the time I'm given.

I am looking for something like http://commons.apache.org/proper/commons-compress/ which is used by Android version of the app.

If you know how to do it with 7zip instead of bzip2, that will do - it was just a bit less effective.


Solution

  • bzip2 source code builds to a libbzip2 that you can link to in your code as long as you have a sufficiently good compiler (and I believe that is the case for iOS - although I haven't tried...)

    The code you need to write would look something like this:

    int error;
    const int MAXSIZE = 4096;    // Or some other decent size.
    char buffer[MAXSIZE];
    FILE *f = fopen("somefile.bz2", "rb"); 
    BZFILE* b = BZ2_bzReadOpen(&error, f, 0, 0, NULL, 0);
    do {
       BZ2_bzRead(&error, b, buffer, MAXSIZE);
    } while(error != BZ_OK);
    BZ2_bzReadClose(&error, b);
    

    You may need to add a few more checks for "error", but the concept should work [I think - I just typed all that code in based in the docs and my experience from using this and similar packages in the past].