我想将 .tar.gz 文件的内容复制到 2 个文件夹,它有大约 20 个文件,解压缩后的总大小将 > 20 GB。
我为此使用了 Truezip。
TFile archive = new TFile(absoluteZipName); // archive with .tar.gz
TFile[] archFiles = archive.listFiles(); // takes too much time
for (TFile t : archFiles) {
String fileName = t.getName();
if(fileName.endsWith(".dat"))
t.cp(new File(destination1+ t.getName()));
else if(fileName.endsWith(".txt")){
t.cp(new File(destination2+ t.getName()));
}
}
It takes 3 times above tar xzf command (untar linux) . Have any way to optimize this code for fast copying, memory not an issue.
The following code allows fast copying Thanks npe for the good advice.
(NB: I have no previledge to post the answe now that's why editing question itself)
InputStream is = new FileInputStream(absoluteZipName);
ArchiveInputStream input = new ArchiveStreamFactory()
.createArchiveInputStream(ArchiveStreamFactory.TAR, new GZIPInputStream(is));
ArchiveEntry entry;
while ((entry = input.getNextEntry()) != null) {
OutputStream outputFileStream=null;
if(entry.getName().endsWith(".dat")){
File outFile1= new File(destination1, entry.getName());
outputFileStream = new FileOutputStream(outFile1);
}
else if(entry.getName().endsWith(".txt")){
File outFile2= new File(destination2, entry.getName());
outputFileStream = new FileOutputStream(outFile2);
}
// use ArchiveEntry#getName() to do the conditional stuff...
IOUtils.copy(input, outputFileStream,10485760);
}
Is threading In file copy will reduce time..? In TZip didn't reduced as they already threading it. anyway I will try tomorrow and will let you Know.