10 Replies Latest reply: Jan 28, 2009 6:13 PM by EJP RSS

    extracting .tar files

    807588
      I have the com.ice.tar package from http://www.trustice.com/java/tar/ and I have the code shown below compiling just fine. However, I need to extract .tar files and this code will only extract tar.gz files. Does anyone know what I need to change to my code to get it to extract .tar files? Thanks,

      import com.ice.tar.*;
      import javax.activation.*;
      import java.io.*;
      import java.util.zip.GZIPInputStream;

      public class untarFiles
      {

      public static void main(String args[]) {


      try {

      untar("c:/Files/AnyFile.tar.gz",new File("c:/Files/"));
      }

      catch(Exception e) {

      e.printStackTrace();


      System.out.println(e.getMessage());
      }

      }
      private static void untar(String tarFileName, File dest) throws IOException {

      //assuming the file you pass in is not a dir

      dest.mkdir();

      //create tar input stream from a .tar.gz file

      TarInputStream tin = new TarInputStream( new GZIPInputStream
      ( new FileInputStream(new File(tarFileName))));

      //get the first entry in the archive

      TarEntry tarEntry = tin.getNextEntry();

      while (tarEntry != null){//create a file with the same name as the tarEntry

      File destPath = new File(dest.toString() + File.separatorChar + tarEntry.getName());

      if(tarEntry.isDirectory()){

      destPath.mkdir();

      } else {

      FileOutputStream fout = new FileOutputStream(destPath);

      tin.copyEntryContents(fout);

      fout.close();
      }
      tarEntry = tin.getNextEntry();
      }
      tin.close();
      }
      }
        • 1. Re: extracting .tar files
          807588
          Cross-post. I've answered it in the other thread, although I can't be bothered to post a link.
          • 2. Re: extracting .tar files
            807588
            public static InputStream getInputStream(String tarFileName) throws Exception{
                  if(tarFileName.substring(tarFileName.lastIndexOf(".") + 1, tarFileName.lastIndexOf(".") + 3).equalsIgnoreCase("gz")){
                     System.out.println("Creating an GZIPInputStream for the file");
                     return new GZIPInputStream(new FileInputStream(new File(tarFileName)));
                  }else{
                     System.out.println("Creating an InputStream for the file");
                     return new FileInputStream(new File(tarFileName));
                  }
               }
            
               public static void readTar(InputStream in, String untarDir) throws IOException{
                  System.out.println("Reading TarInputStream... (using classes from http://www.trustice.com/java/tar/)");
                  TarInputStream tin = new TarInputStream(in);
                  TarEntry tarEntry = tin.getNextEntry();
                  if(new File(untarDir).exists()){
                       while (tarEntry != null){
                          File destPath = new File(untarDir + File.separatorChar + tarEntry.getName());
                          System.out.println("Processing " + destPath.getAbsoluteFile());
                          if(!tarEntry.isDirectory()){
                             FileOutputStream fout = new FileOutputStream(destPath);
                             tin.copyEntryContents(fout);
                             fout.close();
                          }else{
                             destPath.mkdir();
                          }
                          tarEntry = tin.getNextEntry();
                       }
                       tin.close();
                  }else{
                     System.out.println("That destination directory doesn't exist! " + untarDir);
                  }
               }
            • 3. Re: extracting .tar files
              807588
              This code works fine for some .tar.gz files but it is not working for all archive files with .tar.gz

              The problem is that tar does not need to (and often does not) "define" directories.
              Thus, you must always ensure the directory exists for any given path.

              Below is the added code between // new code comments to make it to work for all .tar.gz archive files:

              while (tarEntry != null){
                   File destPath = new File(untarDir + File.separatorChar + tarEntry.getName());
                   System.out.println("Processing " + destPath.getAbsoluteFile());
                   if(!tarEntry.isDirectory()){


              // start of new code
              String pathStr = destPath.getPath();
              int idx = pathStr.lastIndexOf(File.separatorChar);
              if (idx > 0) {
              File destDir = new File(pathStr.substring(0,idx));
              destDir.mkdirs();
              }
              // end of new code


                   FileOutputStream fout = new FileOutputStream(destPath);
                   tin.copyEntryContents(fout);
                   fout.close();
                   }else{
                   destPath.mkdir();
                   }
                   tarEntry = tin.getNextEntry();
                   }
                   tin.close();

              Ram Myneni
              • 4. Re: extracting .tar files
                807588
                hey ppl, i have the same assignment of extracting tar.Z files using java.
                I have the same trustice.com library com.ice.tar
                I've used basically the same code given in the previous post.
                It is givin me the followin exception when i try using tar.Z files :


                com.ice.tar.InvalidHeaderException: bad header in block 0 record 0, header magic is not 'ustar' or unix-style zeros, it is '-50112-51-424592-71', or (dec) -50, 112, -51, -42, 45, 92, -71
                     at com.ice.tar.TarInputStream.getNextEntry(Unknown Source)
                     at filterdialog.readTar(filterdialog.java:101)
                     at filterdialog$1.actionPerformed(filterdialog.java:80)
                     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
                     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
                     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
                     at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
                     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
                     at java.awt.Component.processMouseEvent(Unknown Source)
                     at javax.swing.JComponent.processMouseEvent(Unknown Source)
                     at java.awt.Component.processEvent(Unknown Source)
                     at java.awt.Container.processEvent(Unknown Source)
                     at java.awt.Component.dispatchEventImpl(Unknown Source)
                     at java.awt.Container.dispatchEventImpl(Unknown Source)
                     at java.awt.Component.dispatchEvent(Unknown Source)
                     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
                     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
                     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
                     at java.awt.Container.dispatchEventImpl(Unknown Source)
                     at java.awt.Window.dispatchEventImpl(Unknown Source)
                     at java.awt.Component.dispatchEvent(Unknown Source)
                     at java.awt.EventQueue.dispatchEvent(Unknown Source)
                     at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
                     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                     at java.awt.EventDispatchThread.run(Unknown Source)
                • 5. Re: extracting .tar files
                  807588
                  Well if you are using a .Z file rather than a .gz file then you cannot use the code listed above(since it uses a GZIPStream class). You would need to use some ZStream class instead in order for it to work correctly)
                  • 6. Re: extracting .tar files
                    807588
                    Hey Benjamin,
                    The code above creates a simple FileInputStream if the file isnt a .gz file(i,e. if its a .Z) file.....wont that work?

                    Looks like it doesnt :)

                    can u plz give me the link or point me to the ZStream class that u were talkin about?
                    • 7. Re: extracting .tar files
                      807588
                      I dont't know one off hand but my first suggestion would be to use GZip since it is supported.
                      • 8. Re: extracting .tar files
                        807588
                        i have the same issue of extracting files from a tar.gz ,
                        i used your code to do the work but get an java.util.zip.ZipException: incomplete distance tree

                        can anybody help with this one...

                        the code i used is

                        public static InputStream getInputStream(String tarFileName) throws Exception{

                        if(tarFileName.substring(tarFileName.lastIndexOf(".") + 1, tarFileName.lastIndexOf(".") + 3).equalsIgnoreCase("gz")){
                        System.out.println("Creating an GZIPInputStream for the file");
                        return new GZIPInputStream(new FileInputStream(new File(tarFileName)));

                        }else{
                        System.out.println("Creating an InputStream for the file");
                        return new FileInputStream(new File(tarFileName));
                        }
                        }

                        private static void untar(InputStream in, String untarDir) throws IOException {

                        System.out.println("Reading TarInputStream... ");
                        TarInputStream tin = new TarInputStream(in);
                        TarEntry tarEntry = tin.getNextEntry();_
                        System.out.println("UNTARDIR " + untarDir );
                        if(new File(untarDir).exists()){
                        while (tarEntry != null){
                        File destPath = new File(untarDir + File.separatorChar + tarEntry.getName());
                        //System.out.println("tarEntry " + tarEntry.getName() );

                        System.out.println("Processing " + destPath.getAbsoluteFile() );
                        if(!tarEntry.isDirectory()){
                        FileOutputStream fout = new FileOutputStream(destPath);
                        tin.copyEntryContents(fout);
                        fout.close();
                        }else{
                        destPath.mkdir();
                        }
                        tarEntry = tin.getNextEntry();
                        }
                        tin.close();
                        }else{
                        System.out.println("That destination directory doesn't exist! " + untarDir);
                        }

                        }

                        and the error i get is

                        java.util.zip.ZipException: incomplete distance tree
                        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:147)
                        at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:92)
                        at org.apache.tools.tar.TarBuffer.readBlock(TarBuffer.java:257)
                        at org.apache.tools.tar.TarBuffer.readRecord(TarBuffer.java:223)
                        at org.apache.tools.tar.TarInputStream.getNextEntry(TarInputStream.java:228)
                        at Extract_TAR_GZ_FILE.untar(Extract_TAR_GZ_FILE.java:31)

                        Extract_TAR_GZ_FILE is the name of my java class...the error is pointing to the underlined code

                        pls help
                        • 9. Re: extracting .tar files
                          EJP
                          This thread is nearly 4 years old. Please start your own.
                          • 10. This Thread is now moved
                            EJP
                            This thread is moved from "Reflections & Reference Objects" to here.