我正在解压缩java中的巨大gz文件,gz文件大约2 GB,解压缩文件大约6 GB。有时它会在合理的时间内完成(比如在10分钟或更快的时间内)。
我有一个相当强大的盒子(8GB内存,4-cpu),有没有办法改进下面的代码?或使用完全不同的库?
另外我使用了Xms256m和Xmx4g到vm。在Java中解压缩巨大的gz文件和性能
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编写的... – Gugussee 2011-02-14 10:52:36