#! /usr/bin/perl -w
use strict;
use Compress::Zlib;
my $file = shift || 'testdata.gz';
my $file2 = shift || 'testdata2.gz';
my @data = <DATA>;
my $gz = gzopen( $file, 'wb' ) or die "Cannot open $file for gzwrite:
+$gzerrno\n";
my $line;
foreach $line( @data ) {
$gz->gzwrite( $line )
or die "Could not write gzipped data to $file: $gzerrno\n";
}
$gz->gzclose();
my( $gzstat, $gzout );
($gz, $gzstat) = deflateInit( { -Level => Z_BEST_COMPRESSION } )
or die "Could not construct gz writer: $gzstat\n";
open OUT, ">$file2" or die "Could not open $file2 for output: $!\n";
binmode OUT;
foreach $line( @data ) {
($gzout, $gzstat) = $gz->deflate($line);
die "Could not deflate data: $gzstat\n$line\n" unless $gzstat == Z
+_OK;
print OUT $gzout;
}
($gzout, $gzstat) = $gz->flush();
die "Could not flush gz writer: $gzstat\n" unless $gzstat == Z_OK;
print OUT $gzout;
close OUT;
__DATA__
foo
bar
Judge my vow, sphinx of black quartz
__END__
If I use the funky hex viewer by OeufMayo, I see that these two files are not the same:
+--------------------------------------------------+------------------
++
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | 0123456789ABCDEF
+|
+--------------------------------------------------+------------------
++
| 1F 8B 08 00 00 00 00 00 00 03 4B CB CF E7 4A 4A | ï........K-¤þJJ
+|
| 2C E2 F2 2A 4D 49 4F 55 C8 AD 54 28 CB 2F D7 51 | ,Ô_*MIOU+¡T(-/ÎQ
+|
| 28 2E C8 C8 CC AB 50 C8 4F 53 48 CA 49 4C CE 56 | (.++¦½P+OSH-IL+V
+|
| 28 2C 4D 2C 2A A9 E2 02 00 7B AE 4A 0D 2D 00 00 | (,M,*®Ô..{«J.-..
+|
| 00 | .
+|
+--------------------------------------------------+------------------
++
compared to
+--------------------------------------------------+------------------
++
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | 0123456789ABCDEF
+|
+--------------------------------------------------+------------------
++
| 78 DA 4B CB CF E7 4A 4A 2C E2 F2 2A 4D 49 4F 55 | x+K-¤þJJ,Ô_*MIOU
+|
| C8 AD 54 28 CB 2F D7 51 28 2E C8 C8 CC AB 50 C8 | +¡T(-/ÎQ(.++¦½P+
+|
| 4F 53 48 CA 49 4C CE 56 28 2C 4D 2C 2A A9 E2 02 | OSH-IL+V(,M,*®Ô.
+|
| 00 66 87 0F C8 | .fç +
+|
+--------------------------------------------------+------------------
++
They are nearly the same, but they have different headers and footers, which explains why the latter cannot be decoded
via the following snippet:
$gz = gzopen( $file, 'rb' ) or die "Cannot open $file for gzread: $gze
+rrno\n";
while( $gz->gzreadline($line) > 0 ) {
print $line;
}
die "Error reading from $file: [$gzerrno]\n" unless Z_STREAM_END == $g
+zerrno;
$gz->gzclose();
Can someone enlighten me as to what is going on?
--g r i n d e r