我想將.tar.gz文件的內容複製到2個文件夾,它具有大約20個文件,並且總解壓縮大小將> 20 GB。
我爲此使用了Truezip。將.tar.gz文件複製到文件夾
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.
分兩步做? tar -xzvf ./some/tmp/destination + 2倍'mv'命令過濾擴展名?或者它是強制性的在Java中? – hovanessyan
它在java中是強制性的 – Abdul
在這種情況下,我認爲你不能優化那麼多。 – hovanessyan