16

Lets say that I have 3 file: 1.txt, 2.txt and 3.txt which have all be gzipped. I'm aware that gzip allows multiple files to be combined using cat:

cat 1.gz 2.gz 3.gz > 123.gz

However when 123.gz is extracted it will produce the original 3 files.

Is it possible to combine the three archives in a way that the individual files within the archive will be combined into a single file as well?

slayton
  • 20,123
  • 10
  • 60
  • 89
  • the traditional Unix way is to tar them. AFAIK [xz](http://tukaani.org/xz/) can do what you want, but those ain't zips. – djf May 23 '13 at 13:42
  • 1
    When 123.gz is extracted by what? gunzip _will_ extract that to a single file named 123. – Mark Adler May 23 '13 at 14:54

2 Answers2

26

Surprisingly, this is actually possible.

The GNU zip man page states: multiple compressed files can be concatenated. In this case, gunzip will extract all members at once.

Example:

You can build the zip like this:

echo 1 > 1.txt ; echo 2 > 2.txt; echo 3 > 3.txt;
gzip 1.txt; gzip 2.txt; gzip 3.txt;
cat 1.txt.gz 2.txt.gz 3.txt.gz > all.gz

Then extract it:

gunzip -c all.gz > all.txt

The contents of all.txt should now be:

1
2
3

Which is the same as:

cat 1.txt 2.txt 3.txt

And - as you requested - "gunzip will extract all members at once".

BeniBela
  • 16,412
  • 4
  • 45
  • 52
djf
  • 6,592
  • 6
  • 44
  • 62
4

In order to concatenate multiple files, try:

gzip -c 1.txt > 123.gz
gzip -c 2.txt >> 123.gz
gzip -c 3.txt >> 123.gz

Subsequently, gzip -dc 123.gz would be equivalent to cat 1.txt 2.txt 3.txt.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • Thanks, this doesn't quite work in my case as each archive is written by a different reducer in a Hadoop job. – slayton May 23 '13 at 13:53