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!!"); } }
Nice Article. visit more java examples
ReplyDeleteThank you! but I am getting
ReplyDeleteExtracting: 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)
ah s0
DeleteNice and Informative article... Good one.. Keep rocks and sharing...
ReplyDeleteJava Training in Chennai
Merci!
ReplyDeleteExtracting: ./._node
ReplyDeleteuntar 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)
Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
ReplyDeleteJava training in Bangalore | Java training in Electronic city
Java training in Bangalore | Java training in Marathahalli
Java training in Bangalore | Java training in Btm layout
Java training in Bangalore | Java training in Jaya nagar
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.
ReplyDeleteJava 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
I recently came across your blog and have been reading along. I thought I would leave my first comment.
ReplyDeleteBest Devops Training in pune
excel advanced excel training in bangalore
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. . .
ReplyDeleteangularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
angularjs interview questions and answers
Thanks for sharing this coding, it is understandable. Keep sharing.
ReplyDeleteRobotics Process Automation Training in Chennai
RPA course in Chennai
UiPath Training Institutes in Chennai
Blue Prism Training Chennai
Data Science Course in Chennai
Big Data Analytics Training in Chennai
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
ReplyDeleterpa training in bangalore
rpa training in pune
rpa online training
best rpa training in bangalore
Such a great information for blogger iam a professional blogger thanks…
ReplyDeleteUpgrade your career Learn SAP MM Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.
Thanks for Posting this Blog. this blog is easily clarifying the Doubts for The Beginners.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
I feel very grateful that I read this. keep it up!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
slot siteleri
ReplyDeletekralbet
betpark
tipobet
betmatik
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
ZX1C
شركة تنظيف بالقصيم sBZrtFK0Du
ReplyDelete