5

我的 85MB 大小的 SQLite db 文件使用 XZ 格式压缩,其大小已减少到 16MB。我使用以下代码(以及XZ 为 Java提供的 JAR )在 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); 
}

解压成功,但需要两分钟多才能完成。我认为这很长,因为压缩文件只有 16MB,未压缩文件只有 85MB。

我想知道我是否对代码做错了什么,或者有什么方法可以加快这个解压缩过程。

4

2 回答 2

2

I think that there is little you can do to make this faster. If it is taking 2 minutes to decompress 16Mb to 85Mb, the chances are that most of that time is spent in the actual decompression, and a significant part of the rest is in the actual file I/O ... at the physical level.

Certainly, there is nothing obviously inefficient about your code. You are reading using a BufferedInputStream and decoding / writing using a large buffer. So you will be doing the I/O syscalls efficiently. (Adding a BufferedOutputStream won't make any difference because you are already doing large writes from a 8192 byte buffer.)


The best I can suggest is that you profile your code to see where the hotspots really are. But I suspect that you won't find anything that can be improved enough to make a difference.


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, extra CPU time in decompression is the price that you pay for using a compression algorithm cranked up to the max. You need to decide which is more important to your users: faster downloads, or faster decompression (installation?) of the database.

FWIW, ZIP decompression is probably implemented in a native library, not in pure Java. It certainly is for Oracle / OpenJDK JVMs.

于 2014-03-15T10:35:38.400 回答
1

你应该做的至少是 wrap FileOutputStreamint a BufferedOutputStream,在极少数情况下你不应该使用BufferedInputStream/ BufferedOutputStream。试试看,看看现在需要多长时间。

于 2014-03-15T09:16:17.347 回答