Search code examples
javavfstruezip

copy .tar.gz files to folder


I want to copy contents of a .tar.gz files to 2 folder , It has around 20 files and total unzipped size will be >20 GB.
I used Truezip for this .

 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.

Solution

  • Thanks npe , this is the final I have done, any way it tooks less time than tar xzf. Final code snippet like this.

    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);
    }
    

    Hope I can do some more optimizations, will do later. Thanks a lot