ira_ira
ira_ira

Reputation: 275

gzip archive with multiple files inside

I need to create a gzip archive with multiple files inside, how can I do this without ArchiveEntry available for java.util.zip.GZIPOutputStream class?

Upvotes: 13

Views: 16755

Answers (3)

user2023577
user2023577

Reputation: 2121

Well, you can produce multiple concatenated gzip members each with a filename in the header (with apache's common compress gzip and its GzipParameters) but you'd have to write the reader code too, because generic gzip readers will not likely pay attention to the filename in the 1st gzip members, much less all others. They don't even expect a file at all, just a stream.

In other words, yes you can, but for your own writer+reader code on a desert island.

Upvotes: 0

ArjunShankar
ArjunShankar

Reputation: 23680

According to the Wikipedia entry on gzip:

Although its file format also allows for multiple such streams to be concatenated (zipped files are simply decompressed concatenated as if they were originally one file), gzip is normally used to compress just single files.

This is why GZIPOutputStream doesn't support ArchiveEntry.

Normally, multiple files are archived into one with tar, then compressed with gzip to produce a .tar.gz compressed archive.

You could create a tar.gz in this way by using the Apache Commons Compress implementation for tar:

file_out = new FileOutputStream (new File ("archive.tar.gz"));
buffer_out = new BufferedOutputStream (file_out);
gzip_out = new GzipCompressorOutputStream (buffer_out);
tar_out = new TarArchiveOutputStream (gzip_out);

// .. and then talk to 'tar_out' to write stuff

Here is a more thorough example that compresses entire directories.

Upvotes: 15

ControlAltDel
ControlAltDel

Reputation: 35096

GZip compresses a stream. Typically, when people use GZip with multiple files, they also use tar to munch them together

You can find a java tar libarary here: http://www.trustice.com/java/tar/

Or you could use Zip instead of GZip

Upvotes: 6

Related Questions