我正在解壓縮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