Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract a .tar.gz file in java (JSP)

I can't seem to import the packages needed or find any online examples of how to extract a .tar.gz file in java.

What makes it worse is I'm using JSP pages and am having trouble importing packages into my project. I'm copying the .jar's into WebContent/WEB-INF/lib/ and then right clicking on the project and selecting import external jar and importing it. Sometimes the packages resolve, other times they don't. Can't seem to get GZIP to import either. The imports in eclipse for jsp aren't intuitive like they are in normal Java code where you can right click a recognized package and select import.

I've tried the Apache commons library, the ice and another one called JTar. Ice has imported, but I can't find any examples of how to use it?

I guess I need to uncompress the gzipped part first, then open it with the tarstream?

Any help is greatly appreciated.

like image 531
FredoAF Avatar asked Jan 18 '13 15:01

FredoAF


1 Answers

The accepted answer works fine, but I think it is redundant to have a write to file operation.

You could use something like

 TarArchiveInputStream tarInput = 
      new TarArchiveInputStream(new GZipInputStream(new FileInputStream("Your file name")));

 TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
 while(currentEntry != null) {
      File f = currentEntry.getFile();
      // TODO write to file as usual
 }

Hope this help.

Maven Repo

like image 134
simpletron Avatar answered Sep 20 '22 00:09

simpletron