How to extract XZ files faster in Java?

The size of my SQLite DB file is 85MB, compressed in XZ format, and its size has been reduced to 16MB. I use the following code (and jar provided by XZ for Java) to decompress it in Android jelly bean:

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 completed successfully, but the completion time was more than two minutes. I think this is very long because the compressed file is only 16MB and the uncompressed file is only 85MB

I want to know if I did something wrong with the code, or if there is a way to speed up the decompression process

I think you can do very little to speed up. If it takes 2 minutes to decompress 16MB to 85MB, most of the time is spent in the actual decompression, and a large part of the rest is in the actual file I / O... Physical layer

Of course, your code is not obviously inefficient. You are using bufferedinputstream for reading and large buffer for decoding / writing. Therefore, you will make I / O system calls effectively. (adding bufferedoutputstream will not make any difference because you have capitalized from 8192 byte buffer.)

The best way I suggest is that you can analyze the code to understand the real location of hotspots. But I doubt you can find anything to improve to change them

Then, the extra CPU time for decompression is the price paid for using the compression algorithm to reach the maximum. You need to determine which is more important to your users: faster download speed or faster database decompression (installation?)

Fwiw, zip decompression may be implemented in native libraries rather than in pure Java. It is certainly applicable to Oracle / openjdk JVM

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>