2 package Compress::Zlib;
9 use Scalar::Util qw(dualvar);
11 use IO::Compress::Base::Common ;
12 use Compress::Raw::Zlib;
13 use IO::Compress::Gzip;
14 use IO::Uncompress::Gunzip;
19 our ($VERSION, $XS_VERSION, @ISA, @EXPORT, $AUTOLOAD);
21 $VERSION = '2.000_11';
22 $XS_VERSION = $VERSION;
23 $VERSION = eval $VERSION;
26 # Items to export into callers namespace by default. Note: do not export
27 # names by default without a very good reason. Use EXPORT_OK instead.
28 # Do not simply export all your public functions/methods/constants.
30 deflateInit inflateInit
37 push @EXPORT, @Compress::Raw::Zlib::EXPORT ;
41 *zlib_version = \&Compress::Raw::Zlib::zlib_version;
46 ($constname = $AUTOLOAD) =~ s/.*:://;
47 my ($error, $val) = Compress::Raw::Zlib::constant($constname);
48 Carp::croak $error if $error;
50 *{$AUTOLOAD} = sub { $val };
54 use constant FLAG_APPEND => 1 ;
55 use constant FLAG_CRC => 2 ;
56 use constant FLAG_ADLER => 4 ;
57 use constant FLAG_CONSUME_INPUT => 8 ;
62 "need dictionary", # Z_NEED_DICT 2
63 "stream end", # Z_STREAM_END 1
65 "file error", # Z_ERRNO (-1)
66 "stream error", # Z_STREAM_ERROR (-2)
67 "data error", # Z_DATA_ERROR (-3)
68 "insufficient memory", # Z_MEM_ERROR (-4)
69 "buffer error", # Z_BUF_ERROR (-5)
70 "incompatible version",# Z_VERSION_ERROR(-6)
79 $Compress::Zlib::gzerrno = 0 ;
81 elsif ($value == Z_ERRNO() || $value > 2) {
82 $Compress::Zlib::gzerrno = $! ;
85 $Compress::Zlib::gzerrno = dualvar($value+0, $my_z_errmsg[2 - $value]);
94 my $test_eof = shift ;
96 my $value = $gz->errorNo() || 0 ;
99 #my $gz = $self->[0] ;
100 # gzread uses Z_STREAM_END to denote a successful end
101 $value = Z_STREAM_END() if $gz->eof() && $value == 0 ;
109 my ($file, $mode) = @_ ;
112 my %defOpts = (Level => Z_DEFAULT_COMPRESSION(),
113 Strategy => Z_DEFAULT_STRATEGY(),
117 $writing = ! ($mode =~ /r/i) ;
118 $writing = ($mode =~ /[wa]/i) ;
120 $defOpts{Level} = $1 if $mode =~ /(\d)/;
121 $defOpts{Strategy} = Z_FILTERED() if $mode =~ /f/i;
122 $defOpts{Strategy} = Z_HUFFMAN_ONLY() if $mode =~ /h/i;
124 my $infDef = $writing ? 'deflate' : 'inflate';
127 croak "gzopen: file parameter is not a filehandle or filename"
128 unless isaFilehandle $file || isaFilename $file ;
130 return undef unless $mode =~ /[rwa]/i ;
135 $gz = new IO::Compress::Gzip($file, Minimal => 1, AutoClose => 1,
137 or $Compress::Zlib::gzerrno = $IO::Compress::Gzip::GzipError;
140 $gz = new IO::Uncompress::Gunzip($file,
145 or $Compress::Zlib::gzerrno = $IO::Uncompress::Gunzip::GunzipError;
151 bless [$gz, $infDef], 'Compress::Zlib::gzFile';
154 sub Compress::Zlib::gzFile::gzread
158 return _set_gzerr(Z_STREAM_ERROR())
159 if $self->[1] ne 'inflate';
161 if ($self->gzeof()) {
162 # Zap the output buffer to match ver 1 behaviour.
167 my $gz = $self->[0] ;
168 my $status = $gz->read($_[0], defined $_[1] ? $_[1] : 4096) ;
173 sub Compress::Zlib::gzFile::gzreadline
177 my $gz = $self->[0] ;
178 $_[0] = $gz->getline() ;
180 return defined $_[0] ? length $_[0] : 0 ;
183 sub Compress::Zlib::gzFile::gzwrite
186 my $gz = $self->[0] ;
188 return _set_gzerr(Z_STREAM_ERROR())
189 if $self->[1] ne 'deflate';
191 my $status = $gz->write($_[0]) ;
196 sub Compress::Zlib::gzFile::gztell
199 my $gz = $self->[0] ;
200 my $status = $gz->tell() ;
205 sub Compress::Zlib::gzFile::gzseek
211 my $gz = $self->[0] ;
213 eval { $status = $gz->seek($offset, $whence) ; };
217 $error =~ s/^.*: /gzseek: /;
218 $error =~ s/ at .* line \d+\s*$//;
225 sub Compress::Zlib::gzFile::gzflush
230 my $gz = $self->[0] ;
231 my $status = $gz->flush($f) ;
236 sub Compress::Zlib::gzFile::gzclose
239 my $gz = $self->[0] ;
241 my $status = $gz->close() ;
246 sub Compress::Zlib::gzFile::gzeof
249 my $gz = $self->[0] ;
252 if $self->[1] ne 'inflate';
254 my $status = $gz->eof() ;
259 sub Compress::Zlib::gzFile::gzsetparams
262 croak "Usage: Compress::Zlib::gzFile::gzsetparams(file, level, strategy)"
265 my $gz = $self->[0] ;
267 my $strategy = shift;
269 return _set_gzerr(Z_STREAM_ERROR())
270 if $self->[1] ne 'deflate';
272 my $status = *$gz->{Compress}->deflateParams(-Level => $level,
273 -Strategy => $strategy);
278 sub Compress::Zlib::gzFile::gzerror
281 my $gz = $self->[0] ;
283 return $Compress::Zlib::gzerrno ;
289 my ($x, $output, $err, $in) =('', '', '', '') ;
293 croak "not a scalar reference" unless ref $in eq 'SCALAR' ;
299 my $level = (@_ == 2 ? $_[1] : Z_DEFAULT_COMPRESSION() );
301 $x = new Compress::Raw::Zlib::Deflate -AppendOutput => 1, -Level => $level
304 $err = $x->deflate($in, $output) ;
305 return undef unless $err == Z_OK() ;
307 $err = $x->flush($output) ;
308 return undef unless $err == Z_OK() ;
316 my ($x, $output, $err, $in) =('', '', '', '') ;
320 croak "not a scalar reference" unless ref $in eq 'SCALAR' ;
326 $x = new Compress::Raw::Zlib::Inflate -ConsumeInput => 0 or return undef ;
328 $err = $x->inflate($in, $output) ;
329 return undef unless $err == Z_STREAM_END() ;
338 my ($got) = ParseParameters(0,
340 'Bufsize' => [1, 1, Parse_unsigned, 4096],
341 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()],
342 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()],
343 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()],
344 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
345 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
346 'Dictionary' => [1, 1, Parse_any, ""],
349 croak "Compress::Zlib::deflateInit: Bufsize must be >= 1, you specified " .
350 $got->value('Bufsize')
351 unless $got->value('Bufsize') >= 1;
357 Compress::Raw::Zlib::_deflateInit(0,
358 $got->value('Level'),
359 $got->value('Method'),
360 $got->value('WindowBits'),
361 $got->value('MemLevel'),
362 $got->value('Strategy'),
363 $got->value('Bufsize'),
364 $got->value('Dictionary')) ;
366 my $x = ($status == Z_OK() ? bless $obj, "Zlib::OldDeflate" : undef) ;
367 return wantarray ? ($x, $status) : $x ;
372 my ($got) = ParseParameters(0,
374 'Bufsize' => [1, 1, Parse_unsigned, 4096],
375 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()],
376 'Dictionary' => [1, 1, Parse_any, ""],
380 croak "Compress::Zlib::inflateInit: Bufsize must be >= 1, you specified " .
381 $got->value('Bufsize')
382 unless $got->value('Bufsize') >= 1;
386 ($obj, $status) = Compress::Raw::Zlib::_inflateInit(FLAG_CONSUME_INPUT,
387 $got->value('WindowBits'),
388 $got->value('Bufsize'),
389 $got->value('Dictionary')) ;
391 my $x = ($status == Z_OK() ? bless $obj, "Zlib::OldInflate" : undef) ;
393 wantarray ? ($x, $status) : $x ;
396 package Zlib::OldDeflate ;
399 @ISA = qw(Compress::Raw::Zlib::deflateStream);
407 my $status = $self->SUPER::deflate($_[0], $output) ;
408 wantarray ? ($output, $status) : $output ;
415 my $flag = shift || Compress::Zlib::Z_FINISH();
416 my $status = $self->SUPER::flush($output, $flag) ;
418 wantarray ? ($output, $status) : $output ;
421 package Zlib::OldInflate ;
424 @ISA = qw(Compress::Raw::Zlib::inflateStream);
430 my $status = $self->SUPER::inflate($_[0], $output) ;
431 wantarray ? ($output, $status) : $output ;
434 package Compress::Zlib ;
436 use IO::Compress::Gzip::Constants;
440 my $x = new Compress::Raw::Zlib::Deflate(
444 -Level => Z_BEST_COMPRESSION(),
445 -WindowBits => - MAX_WBITS(),
449 # write a minimal gzip header
450 my $output = GZIP_MINIMUM_HEADER ;
452 # if the deflation buffer isn't a reference, make it one
453 my $string = (ref $_[0] ? $_[0] : \$_[0]) ;
455 my $status = $x->deflate($string, \$output) ;
459 $status = $x->flush(\$output) ;
463 return $output . pack("V V", $x->crc32(), $x->total_in()) ;
468 sub _removeGzipHeader($)
472 return Z_DATA_ERROR()
473 if length($$string) < GZIP_MIN_HEADER_SIZE ;
475 my ($magic1, $magic2, $method, $flags, $time, $xflags, $oscode) =
476 unpack ('CCCCVCC', $$string);
478 return Z_DATA_ERROR()
479 unless $magic1 == GZIP_ID1 and $magic2 == GZIP_ID2 and
480 $method == Z_DEFLATED() and !($flags & GZIP_FLG_RESERVED) ;
481 substr($$string, 0, GZIP_MIN_HEADER_SIZE) = '' ;
484 if ($flags & GZIP_FLG_FEXTRA)
486 return Z_DATA_ERROR()
487 if length($$string) < GZIP_FEXTRA_HEADER_SIZE ;
489 my ($extra_len) = unpack ('v', $$string);
490 $extra_len += GZIP_FEXTRA_HEADER_SIZE;
491 return Z_DATA_ERROR()
492 if length($$string) < $extra_len ;
494 substr($$string, 0, $extra_len) = '';
498 if ($flags & GZIP_FLG_FNAME)
500 my $name_end = index ($$string, GZIP_NULL_BYTE);
501 return Z_DATA_ERROR()
503 substr($$string, 0, $name_end + 1) = '';
507 if ($flags & GZIP_FLG_FCOMMENT)
509 my $comment_end = index ($$string, GZIP_NULL_BYTE);
510 return Z_DATA_ERROR()
511 if $comment_end == -1 ;
512 substr($$string, 0, $comment_end + 1) = '';
516 if ($flags & GZIP_FLG_FHCRC)
518 return Z_DATA_ERROR()
519 if length ($$string) < GZIP_FHCRC_SIZE ;
520 substr($$string, 0, GZIP_FHCRC_SIZE) = '';
529 # if the buffer isn't a reference, make it one
530 my $string = (ref $_[0] ? $_[0] : \$_[0]);
532 _removeGzipHeader($string) == Z_OK()
535 my $bufsize = length $$string > 4096 ? length $$string : 4096 ;
536 my $x = new Compress::Raw::Zlib::Inflate({-WindowBits => - MAX_WBITS(),
537 -Bufsize => $bufsize})
542 my $status = $x->inflate($string, $output);
544 unless $status == Z_STREAM_END();
546 if (length $$string >= 8)
548 my ($crc, $len) = unpack ("VV", substr($$string, 0, 8));
549 substr($$string, 0, 8) = '';
551 unless $len == length($output) and
552 $crc == crc32($output);
561 # Autoload methods go after __END__, and are processed by the autosplit program.
569 Compress::Zlib - Interface to zlib compression library
575 ($d, $status) = deflateInit( [OPT] ) ;
576 $status = $d->deflate($input, $output) ;
577 $status = $d->flush($output [, $flush_type]) ;
578 $d->deflateParams(OPTS) ;
579 $d->deflateTune(OPTS) ;
590 ($i, $status) = inflateInit( [OPT] ) ;
591 $status = $i->inflate($input, $output [, $eof]) ;
592 $status = $i->inflateSync($input) ;
601 $dest = compress($source) ;
602 $dest = uncompress($source) ;
604 $gz = gzopen($filename or filehandle, $mode) ;
605 $bytesread = $gz->gzread($buffer [,$size]) ;
606 $bytesread = $gz->gzreadline($line) ;
607 $byteswritten = $gz->gzwrite($buffer) ;
608 $status = $gz->gzflush($flush) ;
609 $offset = $gz->gztell() ;
610 $status = $gz->gzseek($offset, $whence) ;
611 $status = $gz->gzclose() ;
612 $status = $gz->gzeof() ;
613 $status = $gz->gzsetparams($level, $strategy) ;
614 $errstring = $gz->gzerror() ;
617 $dest = Compress::Zlib::memGzip($buffer) ;
618 $dest = Compress::Zlib::memGunzip($buffer) ;
620 $crc = adler32($buffer [,$crc]) ;
621 $crc = crc32($buffer [,$crc]) ;
623 $crc = adler32_combine($crc1, $crc2, $len2)l
624 $crc = crc32_combine($adler1, $adler2, $len2)
633 The I<Compress::Zlib> module provides a Perl interface to the I<zlib>
634 compression library (see L</AUTHOR> for details about where to get
637 The C<Compress::Zlib> module can be split into two general areas of
638 functionality, namely a simple read/write interface to I<gzip> files
639 and a low-level in-memory compression/decompression interface.
641 Each of these areas will be discussed separately below.
643 =head2 Notes for users of Compress::Zlib version 1
645 Version 2 of this module is a total rewrite. The net result of this is that
646 C<Compress::Zlib> does not now access the zlib library directly.
648 It now uses the C<IO::Compress::Gzip> and C<IO::Uncompress::Gunzip> modules
649 for reading/writing gzip files, and the C<Compress::Raw::Zlib> module for
650 low-level zlib access.
652 If you are writing new code, your first port of call should be to use one
655 =head1 GZIP INTERFACE
657 A number of functions are supplied in I<zlib> for reading and writing
658 I<gzip> files that conform to RFC 1952. This module provides an interface
661 If you are upgrading from C<Compress::Zlib> 1.x, the following
662 enhancements/changes have been made to the C<gzopen> interface:
668 If you want to to open either STDIN or STDOUT with C<gzopen>, you can now
669 optionally use the special filename "C<->" as a synonym for C<\*STDIN> and
674 In C<Compress::Zlib> version 1.x, C<gzopen> used the zlib library to open
675 the underlying file. This made things especially tricky when a Perl
676 filehandle was passed to C<gzopen>. Behind the scenes the numeric C file
677 descriptor had to be extracted from the Perl filehandle and this passed to
680 Apart from being non-portable to some operating systems, this made it
681 difficult to use C<gzopen> in situations where you wanted to extract/create
682 a gzip data stream that is embedded in a larger file, without having to
683 resort to opening and closing the file multiple times.
685 It also made it impossible to pass a perl filehandle that wasn't associated
686 with a real filesystem file, like, say, an C<IO::String>.
688 In C<Compress::Zlib> version 2.x, the C<gzopen> interface has been
689 completely rewritten to use the L<IO::Compress::Gzip|IO::Compress::Gzip>
690 for writing gzip files and L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>
691 for reading gzip files. None of the limitations mentioned above apply.
695 Addition of C<gzseek> to provide a restricted C<seek> interface.
703 A more complete and flexible interface for reading/writing gzip
704 files/buffers is included with the module C<IO-Compress-ZLib>. See
705 L<IO::Compress::Gzip|IO::Compress::Gzip> and
706 L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> for more details.
710 =item B<$gz = gzopen($filename, $mode)>
712 =item B<$gz = gzopen($filehandle, $mode)>
714 This function opens either the I<gzip> file C<$filename> for reading or
715 writing or attaches to the opened filehandle, C<$filehandle>.
716 It returns an object on success and C<undef> on failure.
718 When writing a gzip file this interface will always create the smallest
719 possible gzip header (exactly 10 bytes). If you want greater control over
720 the information stored in the gzip header (like the original filename or a
721 comment) use L<IO::Compress::Gzip|IO::Compress::Gzip> instead.
723 The second parameter, C<$mode>, is used to specify whether the file is
724 opened for reading or writing and to optionally specify a compression
725 level and compression strategy when writing. The format of the C<$mode>
726 parameter is similar to the mode parameter to the 'C' function C<fopen>,
727 so "rb" is used to open for reading and "wb" for writing.
729 To specify a compression level when writing, append a digit between 0
730 and 9 to the mode string -- 0 means no compression and 9 means maximum
732 If no compression level is specified Z_DEFAULT_COMPRESSION is used.
734 To specify the compression strategy when writing, append 'f' for filtered
735 data, 'h' for Huffman only compression, or 'R' for run-length encoding.
736 If no strategy is specified Z_DEFAULT_STRATEGY is used.
738 So, for example, "wb9" means open for writing with the maximum compression
739 using the default strategy and "wb4R" means open for writing with compression
740 level 4 and run-length encoding.
742 Refer to the I<zlib> documentation for the exact format of the C<$mode>
746 =item B<$bytesread = $gz-E<gt>gzread($buffer [, $size]) ;>
748 Reads C<$size> bytes from the compressed file into C<$buffer>. If
749 C<$size> is not specified, it will default to 4096. If the scalar
750 C<$buffer> is not large enough, it will be extended automatically.
752 Returns the number of bytes actually read. On EOF it returns 0 and in
753 the case of an error, -1.
755 =item B<$bytesread = $gz-E<gt>gzreadline($line) ;>
757 Reads the next line from the compressed file into C<$line>.
759 Returns the number of bytes actually read. On EOF it returns 0 and in
760 the case of an error, -1.
762 It is legal to intermix calls to C<gzread> and C<gzreadline>.
764 In addition, C<gzreadline> fully supports the use of of the variable C<$/>
765 (C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
766 determine what constitutes an end of line. Both paragraph mode and file
767 slurp mode are supported.
770 =item B<$byteswritten = $gz-E<gt>gzwrite($buffer) ;>
772 Writes the contents of C<$buffer> to the compressed file. Returns the
773 number of bytes actually written, or 0 on error.
775 =item B<$status = $gz-E<gt>gzflush($flush_type) ;>
777 Flushes all pending output into the compressed file.
779 This method takes an optional parameter, C<$flush_type>, that controls
780 how the flushing will be carried out. By default the C<$flush_type>
781 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
782 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
783 strongly recommended that you only set the C<flush_type> parameter if
784 you fully understand the implications of what it does - overuse of C<flush>
785 can seriously degrade the level of compression achieved. See the C<zlib>
786 documentation for details.
788 Returns 1 on success, 0 on failure.
791 =item B<$offset = $gz-E<gt>gztell() ;>
793 Returns the uncompressed file offset.
795 =item B<$status = $gz-E<gt>gzseek($offset, $whence) ;>
797 Provides a sub-set of the C<seek> functionality, with the restriction
798 that it is only legal to seek forward in the compressed file.
799 It is a fatal error to attempt to seek backward.
801 When opened for writing, empty parts of the file will have NULL (0x00)
802 bytes written to them.
804 The C<$whence> parameter should be one of SEEK_SET, SEEK_CUR or SEEK_END.
806 Returns 1 on success, 0 on failure.
808 =item B<$gz-E<gt>gzclose>
810 Closes the compressed file. Any pending data is flushed to the file
813 Returns 1 on success, 0 on failure.
815 =item B<$gz-E<gt>gzsetparams($level, $strategy>
817 Change settings for the deflate stream C<$gz>.
819 The list of the valid options is shown below. Options not specified
820 will remain unchanged.
822 Note: This method is only available if you are running zlib 1.0.6 or better.
828 Defines the compression level. Valid values are 0 through 9,
829 C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
830 C<Z_DEFAULT_COMPRESSION>.
834 Defines the strategy used to tune the compression. The valid values are
835 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
839 =item B<$gz-E<gt>gzerror>
841 Returns the I<zlib> error message or number for the last operation
842 associated with C<$gz>. The return value will be the I<zlib> error
843 number when used in a numeric context and the I<zlib> error message
844 when used in a string context. The I<zlib> error number constants,
845 shown below, are available for use.
857 The C<$gzerrno> scalar holds the error code associated with the most
858 recent I<gzip> routine. Note that unlike C<gzerror()>, the error is
859 I<not> associated with a particular file.
861 As with C<gzerror()> it returns an error number in numeric context and
862 an error message in string context. Unlike C<gzerror()> though, the
863 error message will correspond to the I<zlib> message when the error is
864 associated with I<zlib> itself, or the UNIX error message when it is
865 not (i.e. I<zlib> returned C<Z_ERRORNO>).
867 As there is an overlap between the error numbers used by I<zlib> and
868 UNIX, C<$gzerrno> should only be used to check for the presence of
869 I<an> error in numeric context. Use C<gzerror()> to check for specific
870 I<zlib> errors. The I<gzcat> example below shows how the variable can
878 Here is an example script which uses the interface. It implements a
886 # use stdin if no files supplied
887 @ARGV = '-' unless @ARGV ;
889 foreach my $file (@ARGV) {
892 my $gz = gzopen($file, "rb")
893 or die "Cannot open $file: $gzerrno\n" ;
895 print $buffer while $gz->gzread($buffer) > 0 ;
897 die "Error reading from $file: $gzerrno" . ($gzerrno+0) . "\n"
898 if $gzerrno != Z_STREAM_END ;
903 Below is a script which makes use of C<gzreadline>. It implements a
904 very simple I<grep> like script.
911 die "Usage: gzgrep pattern [file...]\n"
914 my $pattern = shift ;
916 # use stdin if no files supplied
917 @ARGV = '-' unless @ARGV ;
919 foreach my $file (@ARGV) {
920 my $gz = gzopen($file, "rb")
921 or die "Cannot open $file: $gzerrno\n" ;
923 while ($gz->gzreadline($_) > 0) {
924 print if /$pattern/ ;
927 die "Error reading from $file: $gzerrno\n"
928 if $gzerrno != Z_STREAM_END ;
933 This script, I<gzstream>, does the opposite of the I<gzcat> script
934 above. It reads from standard input and writes a gzip data stream to
942 binmode STDOUT; # gzopen only sets it on the fd
944 my $gz = gzopen(\*STDOUT, "wb")
945 or die "Cannot open stdout: $gzerrno\n" ;
949 or die "error writing: $gzerrno\n" ;
954 =head2 Compress::Zlib::memGzip
956 This function is used to create an in-memory gzip file with the minimum
957 possible gzip header (exactly 10 bytes).
959 $dest = Compress::Zlib::memGzip($buffer) ;
961 If successful, it returns the in-memory gzip file, otherwise it returns
964 The C<$buffer> parameter can either be a scalar or a scalar reference.
966 See L<IO::Compress::Gzip|IO::Compress::Gzip> for an alternative way to
967 carry out in-memory gzip compression.
969 =head2 Compress::Zlib::memGunzip
971 This function is used to uncompress an in-memory gzip file.
973 $dest = Compress::Zlib::memGunzip($buffer) ;
975 If successful, it returns the uncompressed gzip file, otherwise it
978 The C<$buffer> parameter can either be a scalar or a scalar reference. The
979 contents of the C<$buffer> parameter are destroyed after calling this function.
981 See L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> for an alternative way
982 to carry out in-memory gzip uncompression.
984 =head1 COMPRESS/UNCOMPRESS
986 Two functions are provided to perform in-memory compression/uncompression of
987 RFC 1950 data streams. They are called C<compress> and C<uncompress>.
991 =item B<$dest = compress($source [, $level] ) ;>
993 Compresses C<$source>. If successful it returns the compressed
994 data. Otherwise it returns I<undef>.
996 The source buffer, C<$source>, can either be a scalar or a scalar
999 The C<$level> parameter defines the compression level. Valid values are
1000 0 through 9, C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>,
1001 C<Z_BEST_COMPRESSION>, and C<Z_DEFAULT_COMPRESSION>.
1002 If C<$level> is not specified C<Z_DEFAULT_COMPRESSION> will be used.
1005 =item B<$dest = uncompress($source) ;>
1007 Uncompresses C<$source>. If successful it returns the uncompressed
1008 data. Otherwise it returns I<undef>.
1010 The source buffer can either be a scalar or a scalar reference.
1014 Please note: the two functions defined above are I<not> compatible with
1015 the Unix commands of the same name.
1017 See L<IO::Deflate|IO::Deflate> and L<IO::Inflate|IO::Inflate> included with
1018 this distribution for an alternative interface for reading/writing RFC 1950
1022 =head1 Deflate Interface
1024 This section defines an interface that allows in-memory compression using
1025 the I<deflate> interface provided by zlib.
1027 Here is a definition of the interface available:
1030 =head2 B<($d, $status) = deflateInit( [OPT] )>
1032 Initialises a deflation stream.
1034 It combines the features of the I<zlib> functions C<deflateInit>,
1035 C<deflateInit2> and C<deflateSetDictionary>.
1037 If successful, it will return the initialised deflation stream, C<$d>
1038 and C<$status> of C<Z_OK> in a list context. In scalar context it
1039 returns the deflation stream, C<$d>, only.
1041 If not successful, the returned deflation stream (C<$d>) will be
1042 I<undef> and C<$status> will hold the exact I<zlib> error code.
1044 The function optionally takes a number of named options specified as
1045 C<-Name=E<gt>value> pairs. This allows individual options to be
1046 tailored without having to specify them all in the parameter list.
1048 For backward compatibility, it is also possible to pass the parameters
1049 as a reference to a hash containing the name=>value pairs.
1051 The function takes one optional parameter, a reference to a hash. The
1052 contents of the hash allow the deflation interface to be tailored.
1054 Here is a list of the valid options:
1060 Defines the compression level. Valid values are 0 through 9,
1061 C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
1062 C<Z_DEFAULT_COMPRESSION>.
1064 The default is C<-Level =E<gt>Z_DEFAULT_COMPRESSION>.
1068 Defines the compression method. The only valid value at present (and
1069 the default) is C<-Method =E<gt>Z_DEFLATED>.
1071 =item B<-WindowBits>
1073 To create an RFC 1950 data stream, set C<WindowBits> to a positive number.
1075 To create an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>.
1077 For a full definition of the meaning and valid values for C<WindowBits> refer
1078 to the I<zlib> documentation for I<deflateInit2>.
1080 Defaults to C<-WindowBits =E<gt>MAX_WBITS>.
1084 For a definition of the meaning and valid values for C<MemLevel>
1085 refer to the I<zlib> documentation for I<deflateInit2>.
1087 Defaults to C<-MemLevel =E<gt>MAX_MEM_LEVEL>.
1091 Defines the strategy used to tune the compression. The valid values are
1092 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
1094 The default is C<-Strategy =E<gt>Z_DEFAULT_STRATEGY>.
1096 =item B<-Dictionary>
1098 When a dictionary is specified I<Compress::Zlib> will automatically
1099 call C<deflateSetDictionary> directly after calling C<deflateInit>. The
1100 Adler32 value for the dictionary can be obtained by calling the method
1101 C<$d->dict_adler()>.
1103 The default is no dictionary.
1107 Sets the initial size for the deflation buffer. If the buffer has to be
1108 reallocated to increase the size, it will grow in increments of
1111 The default is 4096.
1115 Here is an example of using the C<deflateInit> optional parameter list
1116 to override the default buffer size and compression level. All other
1117 options will take their default values.
1119 deflateInit( -Bufsize => 300,
1120 -Level => Z_BEST_SPEED ) ;
1123 =head2 B<($out, $status) = $d-E<gt>deflate($buffer)>
1126 Deflates the contents of C<$buffer>. The buffer can either be a scalar
1127 or a scalar reference. When finished, C<$buffer> will be
1128 completely processed (assuming there were no errors). If the deflation
1129 was successful it returns the deflated output, C<$out>, and a status
1130 value, C<$status>, of C<Z_OK>.
1132 On error, C<$out> will be I<undef> and C<$status> will contain the
1135 In a scalar context C<deflate> will return C<$out> only.
1137 As with the I<deflate> function in I<zlib>, it is not necessarily the
1138 case that any output will be produced by this method. So don't rely on
1139 the fact that C<$out> is empty for an error test.
1142 =head2 B<($out, $status) = $d-E<gt>flush([flush_type])>
1144 Typically used to finish the deflation. Any pending output will be
1145 returned via C<$out>.
1146 C<$status> will have a value C<Z_OK> if successful.
1148 In a scalar context C<flush> will return C<$out> only.
1150 Note that flushing can seriously degrade the compression ratio, so it
1151 should only be used to terminate a decompression (using C<Z_FINISH>) or
1152 when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>).
1154 By default the C<flush_type> used is C<Z_FINISH>. Other valid values
1155 for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH>
1156 and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the
1157 C<flush_type> parameter if you fully understand the implications of
1158 what it does. See the C<zlib> documentation for details.
1160 =head2 B<$status = $d-E<gt>deflateParams([OPT])>
1162 Change settings for the deflate stream C<$d>.
1164 The list of the valid options is shown below. Options not specified
1165 will remain unchanged.
1171 Defines the compression level. Valid values are 0 through 9,
1172 C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
1173 C<Z_DEFAULT_COMPRESSION>.
1177 Defines the strategy used to tune the compression. The valid values are
1178 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
1182 =head2 B<$d-E<gt>dict_adler()>
1184 Returns the adler32 value for the dictionary.
1186 =head2 B<$d-E<gt>msg()>
1188 Returns the last error message generated by zlib.
1190 =head2 B<$d-E<gt>total_in()>
1192 Returns the total number of bytes uncompressed bytes input to deflate.
1194 =head2 B<$d-E<gt>total_out()>
1196 Returns the total number of compressed bytes output from deflate.
1201 Here is a trivial example of using C<deflate>. It simply reads standard
1202 input, deflates it and writes it to standard output.
1207 use Compress::Zlib ;
1211 my $x = deflateInit()
1212 or die "Cannot create a deflation stream\n" ;
1214 my ($output, $status) ;
1217 ($output, $status) = $x->deflate($_) ;
1220 or die "deflation failed\n" ;
1225 ($output, $status) = $x->flush() ;
1228 or die "deflation failed\n" ;
1232 =head1 Inflate Interface
1234 This section defines the interface available that allows in-memory
1235 uncompression using the I<deflate> interface provided by zlib.
1237 Here is a definition of the interface:
1240 =head2 B<($i, $status) = inflateInit()>
1242 Initialises an inflation stream.
1244 In a list context it returns the inflation stream, C<$i>, and the
1245 I<zlib> status code in C<$status>. In a scalar context it returns the
1246 inflation stream only.
1248 If successful, C<$i> will hold the inflation stream and C<$status> will
1251 If not successful, C<$i> will be I<undef> and C<$status> will hold the
1254 The function optionally takes a number of named options specified as
1255 C<-Name=E<gt>value> pairs. This allows individual options to be
1256 tailored without having to specify them all in the parameter list.
1258 For backward compatibility, it is also possible to pass the parameters
1259 as a reference to a hash containing the name=>value pairs.
1261 The function takes one optional parameter, a reference to a hash. The
1262 contents of the hash allow the deflation interface to be tailored.
1264 Here is a list of the valid options:
1268 =item B<-WindowBits>
1270 To uncompress an RFC 1950 data stream, set C<WindowBits> to a positive number.
1272 To uncompress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>.
1274 For a full definition of the meaning and valid values for C<WindowBits> refer
1275 to the I<zlib> documentation for I<inflateInit2>.
1277 Defaults to C<-WindowBits =E<gt>MAX_WBITS>.
1281 Sets the initial size for the inflation buffer. If the buffer has to be
1282 reallocated to increase the size, it will grow in increments of
1287 =item B<-Dictionary>
1289 The default is no dictionary.
1293 Here is an example of using the C<inflateInit> optional parameter to
1294 override the default buffer size.
1296 inflateInit( -Bufsize => 300 ) ;
1298 =head2 B<($out, $status) = $i-E<gt>inflate($buffer)>
1300 Inflates the complete contents of C<$buffer>. The buffer can either be
1301 a scalar or a scalar reference.
1303 Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the
1304 compressed data has been successfully reached.
1305 If not successful, C<$out> will be I<undef> and C<$status> will hold
1306 the I<zlib> error code.
1308 The C<$buffer> parameter is modified by C<inflate>. On completion it
1309 will contain what remains of the input buffer after inflation. This
1310 means that C<$buffer> will be an empty string when the return status is
1311 C<Z_OK>. When the return status is C<Z_STREAM_END> the C<$buffer>
1312 parameter will contains what (if anything) was stored in the input
1313 buffer after the deflated data stream.
1315 This feature is useful when processing a file format that encapsulates
1316 a compressed data stream (e.g. gzip, zip).
1318 =head2 B<$status = $i-E<gt>inflateSync($buffer)>
1320 Scans C<$buffer> until it reaches either a I<full flush point> or the
1323 If a I<full flush point> is found, C<Z_OK> is returned and C<$buffer>
1324 will be have all data up to the flush point removed. This can then be
1325 passed to the C<deflate> method.
1327 Any other return code means that a flush point was not found. If more
1328 data is available, C<inflateSync> can be called repeatedly with more
1329 compressed data until the flush point is found.
1332 =head2 B<$i-E<gt>dict_adler()>
1334 Returns the adler32 value for the dictionary.
1336 =head2 B<$i-E<gt>msg()>
1338 Returns the last error message generated by zlib.
1340 =head2 B<$i-E<gt>total_in()>
1342 Returns the total number of bytes compressed bytes input to inflate.
1344 =head2 B<$i-E<gt>total_out()>
1346 Returns the total number of uncompressed bytes output from inflate.
1350 Here is an example of using C<inflate>.
1355 use Compress::Zlib ;
1357 my $x = inflateInit()
1358 or die "Cannot create a inflation stream\n" ;
1364 my ($output, $status) ;
1365 while (read(STDIN, $input, 4096))
1367 ($output, $status) = $x->inflate(\$input) ;
1370 if $status == Z_OK or $status == Z_STREAM_END ;
1372 last if $status != Z_OK ;
1375 die "inflation failed\n"
1376 unless $status == Z_STREAM_END ;
1378 =head1 CHECKSUM FUNCTIONS
1380 Two functions are provided by I<zlib> to calculate checksums. For the
1381 Perl interface, the order of the two parameters in both functions has
1382 been reversed. This allows both running checksums and one off
1383 calculations to be done.
1385 $crc = adler32($buffer [,$crc]) ;
1386 $crc = crc32($buffer [,$crc]) ;
1388 The buffer parameters can either be a scalar or a scalar reference.
1390 If the $crc parameters is C<undef>, the crc value will be reset.
1392 If you have built this module with zlib 1.2.3 or better, two more
1393 CRC-related functions are available.
1395 $crc = adler32_combine($crc1, $crc2, $len2)l
1396 $crc = crc32_combine($adler1, $adler2, $len2)
1398 These functions allow checksums to be merged.
1400 =head1 ACCESSING ZIP FILES
1402 Although it is possible (with some effort on your part) to use this
1403 module to access .zip files, there is a module on CPAN that will do all
1404 the hard work for you. Check out the C<Archive::Zip> module on CPAN at
1406 http://www.cpan.org/modules/by-module/Archive/Archive-Zip-*.tar.gz
1411 All the I<zlib> constants are automatically imported when you make use
1412 of I<Compress::Zlib>.
1417 L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1419 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1421 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1422 L<Archive::Tar|Archive::Tar>,
1423 L<IO::Zlib|IO::Zlib>
1426 For RFC 1950, 1951 and 1952 see
1427 F<http://www.faqs.org/rfcs/rfc1950.html>,
1428 F<http://www.faqs.org/rfcs/rfc1951.html> and
1429 F<http://www.faqs.org/rfcs/rfc1952.html>
1431 The I<zlib> compression library was written by Jean-loup Gailly
1432 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1434 The primary site for the I<zlib> compression library is
1435 F<http://www.zlib.org>.
1437 The primary site for gzip is F<http://www.gzip.org>.
1444 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1448 =head1 MODIFICATION HISTORY
1450 See the Changes file.
1452 =head1 COPYRIGHT AND LICENSE
1454 Copyright (c) 1995-2006 Paul Marquess. All rights reserved.
1456 This program is free software; you can redistribute it and/or
1457 modify it under the same terms as Perl itself.