• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Create .tar.gz

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my first question here .

I am trying to create a .tar.gz file using java programatically. Below code creates .tar.gz but I cannot untar it and gives an error. Could anyone please help me with this code.



I invoke this my calling makeCompress() passing name of parent of directory structure in main method.
Many Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Look at lines 10-13, in the method compressFiles.

You have: TarArchiveOutputStream(GZIPOutputStream(BufferedOutputStream(FileOutputStream)))

I think this is the wrong order in which you are wrapping things. A .tar.gz file is a TAR archive which is GZIPped. What you seem to be doing is creating a TAR archive in which each file inside is GZIPped. The GZIPping should be done as the outermost thing instead of the TARing.

In other words, you need: GZIPOutputStream(TarArchiveOutputStream(BufferedOutputStream(FileOutputStream)))
 
indeewari akarawita
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much,

I change the order, then should I write to GZIPOutputStream or TarArchiveOutputStream? What are the changes I should made to addFileToCompression method in my program.

I gave a try out only for .tar then program worked only for folders. But if there is any kind of file .tar cannot be extracted.
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:You have: TarArchiveOutputStream(GZIPOutputStream(BufferedOutputStream(FileOutputStream)))


I think that is correct. Any bytes written to the TarArchiveOutputStream are passed to the GZIPOutputStream, which in turn passes its bytes (after GZIP applied) to the BufferedOutputStream which in turn passed them to the FileOutputStream which finally writes the bytes to disk.

If the TarArchiveOutputStream and GZIPOutputStream are switched you're applying GZIP before applying TAR, which would be something like a .gz.tar file.

The rule is:
- With OutputStream (and Writer), the innermost OutputStream (Writer) will be applied last.
- With InputStream (and Reader), the innermost InputStream (Reader) will be applied first.

Because with .tar.gz TAR must be applied before GZIP, wrapping the GZIPOutputStream in the TarArchiveOutputStream is the correct way. When reading it back in, GZIP must be applied before TAR so the GZIPInputStream would need to be wrapped in the TarArchiveInputStream.
 
Rob Spoor
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeewari, how have you been trying to unpack the created .tar.gz? On a UNIX/Linux system using tar -xz?

One more thing: you're not closing the FileInputStreams created on line 44. Using the try-with-resources you can make your code a bit cleaner. Also, declare as List, not ArrayList.
As you can see I only keep a reference to the TarArchiveOutputStream. That's because the other ones are irrelevant. If you close a FilterOutputStream (which is the base class of GZIPOutputStream and BufferedOutputStream) it closes the OutputStream it wraps. TarArchiveOutputStream explicitly mentions that it does the same. So if you close the TarArchiveOutputStream, that closes the GZIPOutputStream which closes the BufferedOutputStream which closes the FileOutputStream. In the end everything is closed.
 
indeewari akarawita
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tired on Ubuntu; tried both tar -xz command and run extract from right click menu option. Following error prompted.

gzip: stdin: unexpected end of file
tar: Child returned status of 1
tar: Error is not recoverable. exiting now.
 
Rob Spoor
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What works for me (Windows 7, some old Ubuntu, Java 8):
The only important changes are the added .gz to the destination file name, and the way the TarArchiveEntry is created - without the "/" added to the entry name. With the "/" I get a .tar.gz file with only folders, not files.

There's just one issue that remains, and that's an empty folder called ".".
 
indeewari akarawita
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr. Spoor,

It worked fine. Thanks a lot for the help.
 
Rob Spoor
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic