Java – How can I decompress XZ files faster in Java?

How can I decompress XZ files faster in Java?… here is a solution to the problem.

How can I decompress XZ files faster in Java?

My 85MB SQLite database file is compressed using XZ format and its size has been reduced to 16MB. I unzipped it in Android Jelly Bean using the following code (along with JAR provided by XZ for Java).

try { 
    FileInputStream fin = new FileInputStream(path + "myFile.xz");
    BufferedInputStream in = new BufferedInputStream(fin);
    FileOutputStream out = new FileOutputStream(des + "myDecompressed");
    XZInputStream xzIn = new XZInputStream(in);
    final byte[] buffer = new byte[8192];
    int n = 0;
    while (-1 != (n = xzIn.read(buffer))) {
        out.write(buffer, 0, n);
    } 
    out.close();
    xzIn.close();
}
catch(Exception e) { 
    Log.e("Decompress", "unzip", e); 
}

The decompression is successful, but it takes more than two minutes to complete. I think this is long because the compressed file is only 16MB and the uncompressed file is only 85MB.

I was

wondering if I was doing something wrong with the code or if there was any way to speed up this decompression process.

Solution

I think there’s little you can do to make it faster. If it takes 2 minutes to decompress 16Mb to 85Mb, then most of the time is most likely spent on actual decompression and most of the rest on actual file I/O… On a physical level.

Of course, there is no obvious inefficiency in your code. You are using BufferedInputStream to read and decode/write using large buffers. As a result, you will perform I/O system calls efficiently. (Adding BufferedOutputStream won’t have any effect because you’re already writing heavily from an 8192-byte buffer.) )


The best approach I can suggest is to analyze your code to see where the hotspots really are. But I suspect you won’t find anything that can be improved enough to make an impact.


I want to go for XZ because it has the best compression level in my case, which somewhat saves the downloading time… (with zip, the unzipping of this file takes only about 15 seconds!

Well, the extra CPU decompression time is the biggest price you pay with the compression algorithm. You need to decide which is more important to your users: faster downloads, or faster database decompression (installation?). )。

FWIW, ZIP decompression may be implemented in the native library, not in pure Java. It certainly applies to the Oracle/OpenJDK JVM.

Related Problems and Solutions