【发布时间】:2019-09-15 12:54:56
【问题描述】:
我正在创建使用 xz 压缩方法的压缩和解压缩应用程序。但是与使用相同压缩方法的另一个应用程序相比,压缩和解压缩速度较慢。例如,我尝试将 15mb 文件解压为 40mb 文件,我的代码大约需要 18 秒,而在另一个应用程序上只需要大约 4 秒。
我正在使用来自 XZ for Java 的 XZInputStream 和来自 Apache Common Compress 的 TarArchiveInputStream
public static void decompress(File file, String targetPath) {
try {
File outputFile = new File(targetPath);
FileInputStream fileInputStream = new FileInputStream(file);
XZInputStream xzInputStream = new XZInputStream(fileInputStream);
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(xzInputStream);
TarArchiveEntry entry;
while ((entry = tarInputStream.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
File curFile = new File(outputFile, entry.getName());
File parent = curFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
IOUtils.copy(tarInputStream, new FileOutputStream(curFile));
}
} catch (FileNotFoundException e) {
Log.e("Exception", Log.getStackTraceString(e));
} catch (IOException e) {
Log.e("Exception", Log.getStackTraceString(e));
}
}
【问题讨论】:
-
在
FileInputStream周围放置一个BufferedInputStream,在FileOutputStream周围放置一个BufferedOutputStream。 -
@user207421 已经试过了,但没有任何改善。
标签: java android performance compression xz