Saturday 21 July 2012

Untar a tar.gz file with Apache Commons-Compress in Java

In this section , you will learn about how to untar a tar.gz file using apache  commons-compress. 

The complete source code is given below:


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;

public class UnTarFile {
 final static int BUFFER = 2048;

 /**
  * Command line arguments :
  * argv[0]-----> Source tar.gz file.
  * argv[1]-----> DestarInation directory.
  **/
 public static void main(String[] args) throws IOException {

  /** create a TarArchiveInputStream object. **/

  FileInputStream fin = new FileInputStream(args[0]);
  BufferedInputStream in = new BufferedInputStream(fin);
  GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
  TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

  TarArchiveEntry entry = null;

  /** Read the tar entries using the getNextEntry method **/

  while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

   System.out.println("Extracting: " + entry.getName());

   /** If the entry is a directory, create the directory. **/

   if (entry.isDirectory()) {

    File f = new File(args[1] + entry.getName());
    f.mkdirs();
   }
   /**
    * If the entry is a file,write the decompressed file to the disk
    * and close destination stream.
    **/
   else {
    int count;
    byte data[] = new byte[BUFFER];

    FileOutputStream fos = new FileOutputStream(args[1]
      + entry.getName());
    BufferedOutputStream dest = new BufferedOutputStream(fos,
      BUFFER);
    while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
     dest.write(data, 0, count);
    }
    dest.close();
   }
  }

  /** Close the input stream **/

  tarIn.close();
  System.out.println("untar completed successfully!!");
 }

}

16 comments:

  1. Thank you! but I am getting
    Extracting: plugin/project.vim
    Exception in thread "main" java.io.FileNotFoundException: E:\Tarfileplugin\project.vim (The system cannot find the path specified)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.(FileOutputStream.java:213)
    at java.io.FileOutputStream.(FileOutputStream.java:101)
    at org.murari.javalearning.UnzipJava.main(UnzipJava.java:48)

    ReplyDelete
  2. Nice and Informative article... Good one.. Keep rocks and sharing...

    Java Training in Chennai

    ReplyDelete
  3. Extracting: ./._node
    untar completed successfully!!
    Exception in thread "main" java.io.IOException: input buffer is closed
    at org.apache.commons.compress.archivers.tar.TarBuffer.readRecord(TarBuffer.java:192)
    at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getRecord(TarArchiveInputStream.java:252)
    at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:191)
    at org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextEntry(TarArchiveInputStream.java:380)
    at UnTarFile.main(UnTarFile.java:21)

    ReplyDelete
  4. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.
    Java training in Bangalore | Java training in Marathahalli

    Java training in Bangalore | Java training in Btm layout

    Java training in Bangalore |Java training in Rajaji nagar

    Java training in Bangalore | Java training in Kalyan nagar

    ReplyDelete
  5. I recently came across your blog and have been reading along. I thought I would leave my first comment.
    Best Devops Training in pune
    excel advanced excel training in bangalore

    ReplyDelete
  6. I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    angularjs interview questions and answers

    ReplyDelete
  7. Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post
    rpa training in bangalore
    rpa training in pune
    rpa online training
    best rpa training in bangalore

    ReplyDelete
  8. Such a great information for blogger iam a professional blogger thanks…

    Upgrade your career Learn SAP MM Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.

    ReplyDelete