6

This question stems from a Crypto++ library question on Stack Overflow: How to add filename to archive if compressing using Gzip class?.

Gzip, as specified in RFC 1952 has an optional field for the original filename:

  (if FLG.FNAME set)

     +=========================================+
     |...original file name, zero-terminated...| (more-->)
     +=========================================+

If the FNAME bit is set in the flag field, then the original filename is present.

We added the feature to Crypto++ and tested it on OS X. On OS X, it seems the gzip program, The Unarchiver (the default archive program), and the Archive Browser (App Store purchase) do not honor the original filename. That is, each decompresses to a filename which is the archive name sans the gz extension; and not the original filename as it appears in the header.

For example, here's an image under the Archive Browser. The original filename field is set to test-filename.txt, but the tool shows the filename as gzip-test, and unpacks it to a file named gzip-test:

enter image description here

GZip (and Gunzip) is not an IEEE standard Unix Command, so I can't really figure out where to seek knowledge on the expected behavior.

Is this expected behavior? Or am I seeing a bug in three different programs?

If its expected, then what practical purpose does the original filename serve?

1

2 Answers 2

5

According to the gzip man page, using the -N or --name option when using gunzip recovers the original file name. -N is the default when compressing (so gzip always saves the original file name) but not when decompressing so it has to be used explicitly with gunzip.

I tested this as follows:

$ ls
test.txt

$ gzip test.txt
$ ls
test.txt.gz

$ mv test.txt.gz widget.gz

$ gunzip -N widget.gz
$ ls
test.txt

... which is the result we were looking for.

3
  • So I'm clear... I would use gunzip -N to ensure the original filename is used (I'm not near the OS X box at the moment)?
    – jww
    Jan 3, 2015 at 0:41
  • 1
    I've added a test to show that the -N option works -- at least in Cygwin on my Windows PC.
    – Simon
    Jan 3, 2015 at 1:17
  • Perfect, thanks. I can confirm the results on OS X using gunzip.
    – jww
    Jan 3, 2015 at 1:59
3

gzip / gunzip (version 1.6 in Linux Mint / Ubuntu) doesn't appear to remember or use the original filenames, but it apparently can. My original answer, after some basic testing, was that it didn't save the filenames, but I was tricked by the misleading --list output (probably like the OP was too).

The man page indicates that it can save & use the original filename, but even when creating a .gz file with the -N or --name option, when listing (-l) the contents of that .gz file the uncompressed_name column lists the current filename "base".

$ echo test > t1
$ gzip -vkN t1
t1: -40.0% -- replaced with t1.gz

$ gzip -vl t1.gz 
method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla 3bb935c6 Jan  3 14:59                  28                   5 -40.0% t1

$ cp t1.gz N1.gz
$ gzip -vl N1.gz 
method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla 3bb935c6 Jan  3 15:04                  28                   5 -40.0% N1

Unless you also use the -N option when testing/listing, then it shows the original filename

$ gzip -vlN t1.gz 
method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla 3bb935c6 Jan  3 14:59                  28                   5 -40.0% t1
$ gzip -vlN N1.gz 
method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla 3bb935c6 Jan  3 14:59                  28                   5 -40.0% t1

Uncompressing without using the -N option doesn't restore the original filename either,

I didn't think gzip ever had saved or used the original filename, it's more of a "one file at a time" compression often used for .tar files, with tar saving multiple files and their names the usefulness of gzip saving names seems limited.

Other compressed archive formats like .zip, .7z, also remember filenames, since they archive & compress multiple files, they're like a .tar.gz in one.

Speaking of crypto, gpg can remember & use original filenames (definitely when it uses it's -c symmetric encryption on a single file, should work for public key encryption too).

2
  • As indicated by the original question, and Simon's answer, gzip does in fact remember the original file name. The fact that it only compresses one file is a non sequitur.
    – psusi
    Jan 3, 2015 at 4:01
  • @psusi I was tricked by --list's misleading output, updated answer with some interesting test results
    – Xen2050
    Jan 3, 2015 at 22:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .