gzip vs pipe gzip: produce different file size

Hi All,

I have a random test file: test.txt, size: 146
$ ll test.txt
$ 146 test.txt

Take 1:
$ cat test.txt | gzip > test.txt.gz
$ ll test.txt.gz
$ 124 test.txt.gz

Take 2:
$ gzip test.txt
$ ll test.txt.gz
$ 133 test.txt.gz

As you can see, gzipping a file and piping into gzip produce different sized *gz files. The problem currently is that some archivers, such as WinZip and Cygwin's gunzip have to unzip the piped gzip file twice before it can be read as a plain text file. Why is this and how can I ensure piping into gzip produces the same file as gzipping a file?

Thanks,

Hi.

I did not need to unzip twice. Perhaps this will help explain the difference in lengths:

#!/usr/bin/env bash

# @(#) s1	Demonstrate gzip lengths.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C spit gzip

rm -f f1 f1.gz f2 f2.gz f3 f3.gz f4 f4.gz f5 f5.gz
spit -c 10 -r 5 > f1
cp f1 f2
cp f1 f3

pl " Input data files f1, f2:"
wc f1 f2

pl " Results, re-direction:"
cat f1 | gzip > f1.gz
wc f1.gz
rm -f f1

pl " Results, by filename:"
gzip -n f2
wc f2.gz

pl " Results, by filename, plain:"
gzip f3
wc f3.gz

cp f1.gz f4.gz
cp f3.gz f5.gz

pl " All files compressed, contents:"
ls -lgG f*
pe
file f*

pl " All files uncompressed:"
for f in *.gz
do
  gunzip $f
done
ls -lgG f*

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
spit - ( local: RepRev 1.3, ~/bin/spit, 2011-04-29 )
gzip 1.3.12

-----
 Input data files f1, f2:
  5  50 100 f1
  5  50 100 f2
 10 100 200 total

-----
 Results, re-direction:
 0  1 30 f1.gz

-----
 Results, by filename:
 0  1 30 f2.gz

-----
 Results, by filename, plain:
 0  1 33 f3.gz

-----
 All files compressed, contents:
-rw-r--r-- 1 30 Jun  3 21:45 f1.gz
-rw-r--r-- 1 30 Jun  3 21:45 f2.gz
-rw-r--r-- 1 33 Jun  3 21:45 f3.gz
-rw-r--r-- 1 30 Jun  3 21:45 f4.gz
-rw-r--r-- 1 33 Jun  3 21:45 f5.gz

f1.gz: gzip compressed data, from Unix, last modified: Fri Jun  3 21:45:43 2011
f2.gz: gzip compressed data, from Unix
f3.gz: gzip compressed data, was "f3", from Unix, last modified: Fri Jun  3 21:45:43 2011
f4.gz: gzip compressed data, from Unix, last modified: Fri Jun  3 21:45:43 2011
f5.gz: gzip compressed data, was "f3", from Unix, last modified: Fri Jun  3 21:45:43 2011

-----
 All files uncompressed:
-rw-r--r-- 1 100 Jun  3 21:45 f1
-rw-r--r-- 1 100 Jun  3 21:45 f2
-rw-r--r-- 1 100 Jun  3 21:45 f3
-rw-r--r-- 1 100 Jun  3 21:45 f4
-rw-r--r-- 1 100 Jun  3 21:45 f5

See man gzip for details.

Best wishes ... cheers, drl