我正在创建使用 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));
}
}