【发布时间】:2011-02-14 10:46:02
【问题描述】:
我正在用java解压一个巨大的gz文件,gz文件大约2 gb,解压后的文件大约6 gb。解压缩过程有时会花费很长时间(数小时),有时会在合理的时间内完成(例如不到 10 分钟或更快)。
我有一个相当强大的盒子(8GB ram,4-cpu),有没有办法改进下面的代码?还是使用完全不同的库?
我还使用 Xms256m 和 Xmx4g 到 vm。
public static File unzipGZ(File file, File outputDir) {
GZIPInputStream in = null;
OutputStream out = null;
File target = null;
try {
// Open the compressed file
in = new GZIPInputStream(new FileInputStream(file));
// Open the output file
target = new File(outputDir, FileUtil.stripFileExt(file.getName()));
out = new FileOutputStream(target);
// Transfer bytes from the compressed file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Close the file and stream
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return target;
}
【问题讨论】:
-
@user121196:“十亿”和 Java 不太匹配。如果你已经控制了系统并且它是一个 Un*x 盒子,我会考虑在这里调用一个外部进程。这不是很好,但有一个原因是为什么软件操作非常大的文件或非常大量的文件(如 Git、Mercurial 等)不是用 Java 编写的......
-
我最终使用了linux原生进程gunzip,它甚至比IOUtil.moveFile更快
标签: java compression gzip