I thought I had the solution to my Compress::Zlib problem when I did a Super Search and came across this node. Unfortunately, I want to build gzipped files playing around with Z_BEST_SPEED and Z_BEST_COMPRESSION, to see the differences. I can do that, but unfortunately I can't read the data back in... I just get garbage.

Consider the two following ways to write a gzipped data file:

#! /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

In reply to Different compression behaviours with Compress::Zlib by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.