2 package IO::Compress::Gzip ;
13 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $GzipError);
15 $VERSION = '2.000_05';
18 @ISA = qw(Exporter IO::BaseDeflate);
19 @EXPORT_OK = qw( $GzipError gzip ) ;
20 %EXPORT_TAGS = %IO::BaseDeflate::EXPORT_TAGS ;
21 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
22 Exporter::export_ok_tags('all');
27 return IO::BaseDeflate::new($pkg, 'rfc1952', undef, \$GzipError, @_);
33 return IO::BaseDeflate::_def(__PACKAGE__, 'rfc1952', \$GzipError, @_);
36 package IO::BaseDeflate;
39 use Compress::Zlib 2 ;
40 use Compress::Zlib::Common;
41 use Compress::Zlib::FileConstants;
42 use Compress::Zlib::ParseParameters;
43 use Compress::Gzip::Constants;
44 use IO::Uncompress::Gunzip;
53 our (@ISA, $VERSION, @EXPORT_OK, %EXPORT_TAGS, $got_encode);
54 @ISA = qw(Exporter IO::File);
55 %EXPORT_TAGS = ( flush => [qw{
81 foreach (keys %EXPORT_TAGS )
83 push @{$EXPORT_TAGS{constants}},
87 $EXPORT_TAGS{all} = $EXPORT_TAGS{constants} ;
90 Exporter::export_ok_tags('all');
95 if (defined &utf8::downgrade )
96 { *noUTF8 = \&utf8::downgrade }
102 $VERSION = '2.000_03';
104 #Can't locate object method "SWASHNEW" via package "utf8" (perhaps you forgot to load "utf8"?) at .../ext/Compress-Zlib/Gzip/blib/lib/Compress/Zlib/Common.pm line 16.
110 # Encode->import('encode', 'find_encoding');
113 #$got_encode = 1 unless $@;
118 ${ *$self->{ErrorNo} } = shift() + 0 ;
119 ${ *$self->{Error} } = '' ;
121 return ${ *$self->{ErrorNo} } ;
129 ${ *$self->{Error} } = shift ;
130 ${ *$self->{ErrorNo} } = shift() + 0 if @_ ;
138 return ${ *$self->{Error} } ;
144 return ${ *$self->{ErrorNo} } ;
154 return $into | (($value & $mask) << $offset ) ;
157 sub mkDeflateHdr($$$;$)
162 my $fdict_adler = shift ;
167 $fdict = 1 if defined $fdict_adler;
169 $cmf = bitmask($cmf, $method, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS);
170 $cmf = bitmask($cmf, $cinfo, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS);
172 $flg = bitmask($flg, $fdict, ZLIB_FLG_FDICT_OFFSET, ZLIB_FLG_FDICT_BITS);
173 $flg = bitmask($flg, $level, ZLIB_FLG_LEVEL_OFFSET, ZLIB_FLG_LEVEL_BITS);
175 my $fcheck = 31 - ($cmf * 256 + $flg) % 31 ;
176 $flg = bitmask($flg, $fcheck, ZLIB_FLG_FCHECK_OFFSET, ZLIB_FLG_FCHECK_BITS);
178 my $hdr = pack("CC", $cmf, $flg) ;
179 $hdr .= pack("N", $fdict_adler) if $fdict ;
184 sub mkDeflateHeader ($)
188 my $level = $param->value('Level');
189 my $strategy = $param->value('Strategy');
193 if $level == Z_DEFAULT_COMPRESSION ;
195 if (ZLIB_VERNUM >= 0x1210)
197 if ($strategy >= Z_HUFFMAN_ONLY || $level < 2)
198 { $lflag = ZLIB_FLG_LEVEL_FASTEST }
200 { $lflag = ZLIB_FLG_LEVEL_FAST }
202 { $lflag = ZLIB_FLG_LEVEL_DEFAULT }
204 { $lflag = ZLIB_FLG_LEVEL_SLOWEST }
208 $lflag = ($level - 1) >> 1 ;
209 $lflag = 3 if $lflag > 3 ;
212 #my $wbits = (MAX_WBITS - 8) << 4 ;
214 mkDeflateHdr(ZLIB_CMF_CM_DEFLATED, $wbits, $lflag);
221 # stort-circuit if a minimal header is requested.
222 return GZIP_MINIMUM_HEADER if $param->value('Minimal') ;
225 my $method = $param->valueOrDefault('Method', GZIP_CM_DEFLATED) ;
228 my $flags = GZIP_FLG_DEFAULT ;
229 $flags |= GZIP_FLG_FTEXT if $param->value('TextFlag') ;
230 $flags |= GZIP_FLG_FHCRC if $param->value('HeaderCRC') ;
231 $flags |= GZIP_FLG_FEXTRA if $param->wantValue('ExtraField') ;
232 $flags |= GZIP_FLG_FNAME if $param->wantValue('Name') ;
233 $flags |= GZIP_FLG_FCOMMENT if $param->wantValue('Comment') ;
236 my $time = $param->valueOrDefault('Time', GZIP_MTIME_DEFAULT) ;
239 my $extra_flags = $param->valueOrDefault('ExtraFlags', GZIP_XFL_DEFAULT);
242 my $os_code = $param->valueOrDefault('OS_Code', GZIP_OS_DEFAULT) ;
245 my $out = pack("C4 V C C",
248 $method, # Compression Method
250 $time, # Modification Time
251 $extra_flags, # Extra Flags
252 $os_code, # Operating System Code
256 if ($flags & GZIP_FLG_FEXTRA) {
257 my $extra = $param->value('ExtraField') ;
258 $out .= pack("v", length $extra) . $extra ;
262 if ($flags & GZIP_FLG_FNAME) {
263 my $name .= $param->value('Name') ;
264 $name =~ s/\x00.*$//;
266 # Terminate the filename with NULL unless it already is
267 $out .= GZIP_NULL_BYTE
269 substr($name, 1, -1) ne GZIP_NULL_BYTE ;
273 if ($flags & GZIP_FLG_FCOMMENT) {
274 my $comment .= $param->value('Comment') ;
275 $comment =~ s/\x00.*$//;
277 # Terminate the comment with NULL unless it already is
278 $out .= GZIP_NULL_BYTE
279 if ! length $comment or
280 substr($comment, 1, -1) ne GZIP_NULL_BYTE;
284 $out .= pack("v", crc32($out) & 0x00FF ) if $param->value('HeaderCRC') ;
293 return "Error with ExtraField Parameter: $_[0]" ;
296 sub validateExtraFieldPair
301 return ExtraFieldError("Not an array ref")
302 unless ref $pair && ref $pair eq 'ARRAY';
304 return ExtraFieldError("SubField must have two parts")
307 return ExtraFieldError("SubField ID is a reference")
310 return ExtraFieldError("SubField Data is a reference")
313 # ID is exactly two chars
314 return ExtraFieldError("SubField ID not two chars long")
315 unless length $pair->[0] == GZIP_FEXTRA_SUBFIELD_ID_SIZE ;
317 # Check that the 2nd byte of the ID isn't 0
318 return ExtraFieldError("SubField ID 2nd byte is 0x00")
319 if ! $lax && substr($pair->[0], 1, 1) eq "\x00" ;
321 return ExtraFieldError("SubField Data too long")
322 if length $pair->[1] > GZIP_FEXTRA_SUBFIELD_MAX_SIZE ;
336 my $XLEN = length $data ;
338 return ExtraFieldError("Too Large")
339 if $XLEN > GZIP_FEXTRA_MAX_SIZE;
342 while ($offset < $XLEN) {
344 return ExtraFieldError("FEXTRA Body")
345 if $offset + GZIP_FEXTRA_SUBFIELD_HEADER_SIZE > $XLEN ;
347 my $id = substr($data, $offset, GZIP_FEXTRA_SUBFIELD_ID_SIZE);
348 $offset += GZIP_FEXTRA_SUBFIELD_ID_SIZE;
350 my $subLen = unpack("v", substr($data, $offset,
351 GZIP_FEXTRA_SUBFIELD_LEN_SIZE));
352 $offset += GZIP_FEXTRA_SUBFIELD_LEN_SIZE ;
354 return ExtraFieldError("FEXTRA Body")
355 if $offset + $subLen > $XLEN ;
357 my $bad = validateExtraFieldPair( [$id,
358 substr($data, $offset, $subLen)], $lax );
359 return $bad if $bad ;
373 # ExtraField can be any of
375 # -ExtraField => $data
376 # -ExtraField => [$id1, $data1,
380 # -ExtraField => [ [$id1 => $data1],
384 # -ExtraField => { $id1 => $data1,
391 unless $got->parsed('ExtraField') ;
393 return parseExtra($got->value('ExtraField'), $lax)
394 unless ref $got->value('ExtraField') ;
396 my $data = $got->value('ExtraField');
399 if (ref $data eq 'ARRAY') {
400 if (ref $data->[0]) {
402 foreach my $pair (@$data) {
403 return ExtraFieldError("Not list of lists")
404 unless ref $pair eq 'ARRAY' ;
406 my $bad = validateExtraFieldPair($pair, $lax) ;
407 return $bad if $bad ;
409 $out .= $pair->[0] . pack("v", length $pair->[1]) .
414 return ExtraFieldError("Not even number of elements")
415 unless @$data % 2 == 0;
417 for (my $ix = 0; $ix <= length(@$data) -1 ; $ix += 2) {
418 my $bad = validateExtraFieldPair([$data->[$ix], $data->[$ix+1]], $lax) ;
419 return $bad if $bad ;
421 $out .= $data->[$ix] . pack("v", length $data->[$ix+1]) .
426 elsif (ref $data eq 'HASH') {
427 while (my ($id, $info) = each %$data) {
428 my $bad = validateExtraFieldPair([$id, $info], $lax);
429 return $bad if $bad ;
431 $out .= $id . pack("v", length $info) . $info ;
435 return ExtraFieldError("Not a scalar, array ref or hash ref") ;
438 $got->value('ExtraField' => $out);
448 my $rfc1952 = ($type eq 'rfc1952');
449 my $rfc1950 = ($type eq 'rfc1950');
451 my $got = Compress::Zlib::ParseParameters::new();
456 'AutoClose'=> [Parse_boolean, 0],
457 #'Encoding'=> [Parse_any, undef],
458 'Strict' => [Parse_boolean, 1],
459 'Append' => [Parse_boolean, 0],
460 'Merge' => [Parse_boolean, 0],
461 'BinModeIn' => [Parse_boolean, 0],
464 #'Method' => [Parse_unsigned, Z_DEFLATED],
465 'Level' => [Parse_signed, Z_DEFAULT_COMPRESSION],
466 'Strategy' => [Parse_signed, Z_DEFAULT_STRATEGY],
469 'Minimal' => [Parse_boolean, 0],
470 'Comment' => [Parse_any, undef],
471 'Name' => [Parse_any, undef],
472 'Time' => [Parse_any, undef],
473 'TextFlag' => [Parse_boolean, 0],
474 'HeaderCRC' => [Parse_boolean, 0],
475 'OS_Code' => [Parse_unsigned, $Compress::Zlib::gzip_os_code],
476 'ExtraField'=> [Parse_string, undef],
477 'ExtraFlags'=> [Parse_any, undef],
481 'AutoClose' => [Parse_boolean, 0],
482 #'Encoding' => [Parse_any, undef],
483 'CRC32' => [Parse_boolean, 0],
484 'ADLER32' => [Parse_boolean, 0],
485 'Strict' => [Parse_boolean, 1],
486 'Append' => [Parse_boolean, 0],
487 'Merge' => [Parse_boolean, 0],
488 'BinModeIn' => [Parse_boolean, 0],
491 #'Method' => [Parse_unsigned, Z_DEFLATED],
492 'Level' => [Parse_signed, Z_DEFAULT_COMPRESSION],
493 'Strategy' => [Parse_signed, Z_DEFAULT_STRATEGY],
495 @_) or croak "${class}: $got->{Error}" ;
505 my $error_ref = shift ;
507 croak("$class: Missing Output parameter")
510 my $outValue = shift ;
516 $got = checkParams($class, $type, @_)
520 my $rfc1952 = ($type eq 'rfc1952');
521 my $rfc1950 = ($type eq 'rfc1950');
522 my $rfc1951 = ($type eq 'rfc1951');
524 my $obj = bless Symbol::gensym(), ref($class) || $class;
525 tie *$obj, $obj if $] >= 5.005;
527 *$obj->{Closed} = 1 ;
529 *$obj->{Error} = $error_ref ;
531 my $lax = ! $got->value('Strict') ;
533 my $outType = whatIsOutput($outValue);
535 ckOutputParam($class, $outValue, $error_ref)
538 if ($outType eq 'buffer') {
539 *$obj->{Buffer} = $outValue;
543 *$obj->{Buffer} = \$buff ;
546 # Merge implies Append
547 my $merge = $got->value('Merge') ;
548 my $appendOutput = $got->value('Append') || $merge ;
552 # Switch off Merge mode if output file/buffer is empty/doesn't exist
553 if (($outType eq 'buffer' && length $$outValue == 0 ) ||
554 ($outType ne 'buffer' && (! -e $outValue || (-w _ && -z _))) )
558 # If output is a file, check that it is writable
559 if ($outType eq 'filename' && -e $outValue && ! -w _)
560 { return $obj->saveErrorString(undef, "Output file '$outValue' is not writable" ) }
562 elsif ($outType eq 'handle' && ! -w $outValue)
563 { return $obj->saveErrorString(undef, "Output filehandle is not writable" ) }
567 # if ($got->parsed('Encoding')) {
568 # croak("$class: Encode module needed to use -Encoding")
571 # my $want_encoding = $got->value('Encoding');
572 # my $encoding = find_encoding($want_encoding);
574 # croak("$class: Encoding '$want_encoding' is not available")
577 # *$obj->{Encoding} = $encoding;
580 if ($rfc1952 && ! $merge) {
582 if (! $got->parsed('Time') ) {
583 # Modification time defaults to now.
584 $got->value('Time' => time) ;
587 # Check that the Name & Comment don't have embedded NULLs
588 # Also check that they only contain ISO 8859-1 chars.
589 if ($got->parsed('Name') && defined $got->value('Name')) {
590 my $name = $got->value('Name');
592 return $obj->saveErrorString(undef, "Null Character found in Name",
594 if ! $lax && $name =~ /\x00/ ;
596 return $obj->saveErrorString(undef, "Non ISO 8859-1 Character found in Name",
598 if ! $lax && $name =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ;
601 if ($got->parsed('Comment') && defined $got->value('Comment')) {
602 my $comment = $got->value('Comment');
604 return $obj->saveErrorString(undef, "Null Character found in Comment",
606 if ! $lax && $comment =~ /\x00/ ;
608 return $obj->saveErrorString(undef, "Non ISO 8859-1 Character found in Comment",
610 if ! $lax && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o;
613 if ($got->parsed('OS_Code') ) {
614 my $value = $got->value('OS_Code');
616 return $obj->saveErrorString(undef, "OS_Code must be between 0 and 255, got '$value'")
617 if $value < 0 || $value > 255 ;
621 # gzip only supports Deflate at present
622 $got->value('Method' => Z_DEFLATED) ;
624 if ( ! $got->parsed('ExtraFlags')) {
625 $got->value('ExtraFlags' => 2)
626 if $got->value('Level') == Z_BEST_SPEED ;
627 $got->value('ExtraFlags' => 4)
628 if $got->value('Level') == Z_BEST_COMPRESSION ;
631 if ($got->parsed('ExtraField')) {
633 my $bad = $obj->parseExtraField($got, $lax) ;
634 return $obj->saveErrorString(undef, $bad, Z_DATA_ERROR)
637 my $len = length $got->value('ExtraField') ;
638 return $obj->saveErrorString(undef, ExtraFieldError("Too Large"),
640 if $len > GZIP_FEXTRA_MAX_SIZE;
644 $obj->saveStatus(Z_OK) ;
650 (*$obj->{Deflate}, $status) = new Compress::Zlib::Deflate
652 -CRC32 => $rfc1952 || $got->value('CRC32'),
653 -ADLER32 => $rfc1950 || $got->value('ADLER32'),
654 -Level => $got->value('Level'),
655 -Strategy => $got->value('Strategy'),
656 -WindowBits => - MAX_WBITS;
657 return $obj->saveErrorString(undef, "Cannot create Deflate object: $status" )
658 if $obj->saveStatus($status) != Z_OK ;
660 *$obj->{BytesWritten} = 0 ;
663 *$obj->{Header} = mkDeflateHeader($got)
667 *$obj->{Header} = mkGzipHeader($got)
670 if ( $outType eq 'buffer') {
671 ${ *$obj->{Buffer} } = ''
672 unless $appendOutput ;
673 ${ *$obj->{Buffer} } .= *$obj->{Header};
676 if ($outType eq 'handle') {
678 *$obj->{FH} = $outValue ;
679 setBinModeOutput(*$obj->{FH}) ;
680 *$obj->{Handle} = 1 ;
683 seek(*$obj->{FH}, 0, SEEK_END)
684 or return $obj->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
688 elsif ($outType eq 'filename') {
692 *$obj->{FH} = new IO::File "$mode $outValue"
693 or return $obj->saveErrorString(undef, "cannot open file '$outValue': $!", $!) ;
694 *$obj->{StdIO} = ($outValue eq '-');
695 setBinModeOutput(*$obj->{FH}) ;
699 defined *$obj->{FH}->write(*$obj->{Header}, length(*$obj->{Header}))
700 or return $obj->saveErrorString(undef, $!, $!) ;
706 my %mapping = ( 'rfc1952' => ['IO::Uncompress::Gunzip', \$IO::Uncompress::Gunzip::GunzipError],
707 'rfc1950' => ['IO::Uncompress::Inflate', \$IO::Uncompress::Inflate::InflateError],
708 'rfc1951' => ['IO::Uncompress::RawInflate', \$IO::Uncompress::RawInflate::RawInflateError],
711 my $inf = IO::BaseInflate::new($mapping{$type}[0],
713 $error_ref, 0, $outValue,
719 return $obj->saveErrorString(undef, "Cannot create InflateScan object: $$error_ref" )
723 or return $obj->saveErrorString(undef, "Error Scanning: $$error_ref", $inf->errorNo) ;
724 $inf->zap($end_offset)
725 or return $obj->saveErrorString(undef, "Error Zapping: $$error_ref", $inf->errorNo) ;
727 (*$obj->{Deflate}, $status) = $inf->createDeflate();
729 *$obj->{Header} = *$inf->{Info}{Header};
731 *$obj->{ISize} = *$obj->{BytesWritten} = *$inf->{ISize} ;
733 if ( $outType eq 'buffer')
734 { substr( ${ *$obj->{Buffer} }, $end_offset) = '' }
735 elsif ($outType eq 'handle' || $outType eq 'filename') {
736 *$obj->{FH} = *$inf->{FH} ;
738 *$obj->{FH}->flush() ;
739 *$obj->{Handle} = 1 if $outType eq 'handle';
741 #seek(*$obj->{FH}, $end_offset, SEEK_SET)
742 *$obj->{FH}->seek($end_offset, SEEK_SET)
743 or return $obj->saveErrorString(undef, $!, $!) ;
747 *$obj->{Closed} = 0 ;
748 *$obj->{AutoClose} = $got->value('AutoClose') ;
749 *$obj->{OutputGzip} = $rfc1952;
750 *$obj->{OutputDeflate} = $rfc1950;
751 *$obj->{OutputRawDeflate} = $rfc1951;
752 *$obj->{Output} = $outValue;
753 *$obj->{ClassName} = $class;
763 my $error_ref = shift ;
765 my $name = (caller(1))[3] ;
767 croak "$name: expected at least 1 parameters\n"
774 my $x = new Validator($class, $type, $error_ref, $name, $input, $output)
777 push @_, $output if $haveOut && $x->{Hash};
779 my $got = checkParams($name, $type, @_)
783 $x->{ParsedTime} = $got->parsed('Time') ;
784 $x->{ParsedName} = $got->parsed('Name') ;
788 while (my($k, $v) = each %$input)
793 _singleTarget($x, 1, $k, $v, @_)
797 return keys %$input ;
803 foreach my $pair (@{ $x->{Pairs} })
805 my ($from, $to) = @$pair ;
806 _singleTarget($x, 1, $from, $to, @_)
810 return scalar @{ $x->{Pairs} } ;
813 if (! $x->{oneOutput} )
815 my $inFile = ($x->{inType} eq 'filenames'
816 || $x->{inType} eq 'filename');
818 $x->{inType} = $inFile ? 'filename' : 'buffer';
820 foreach my $in ($x->{oneInput} ? $input : @$input)
825 _singleTarget($x, $inFile, $in, \$out, @_)
828 if ($x->{outType} eq 'array')
829 { push @$output, \$out }
831 { $output->{$in} = \$out }
837 # finally the 1 to 1 and n to 1
838 return _singleTarget($x, 1, $input, $output, @_);
840 croak "should not be here" ;
846 my $inputIsFilename = shift;
850 # For gzip, if input is simple filename, populate Name & Time in
851 # gzip header from filename by default.
852 if ($x->{Type} eq 'rfc1952' and isaFilename($input) and $inputIsFilename)
854 my $defaultTime = (stat($input))[8] ;
856 $x->{Got}->value('Name' => $input)
857 if ! $x->{ParsedName};
859 $x->{Got}->value('Time' => $defaultTime)
860 if ! $x->{ParsedTime};
863 my $gzip = new($x->{Class}, $x->{Type}, $x->{Got}, $x->{Error}, @_)
869 defined $gzip->_wr2($input, $inputIsFilename)
875 my $inputIsFilename = ($x->{inType} ne 'array');
877 for my $element ( ($x->{inType} eq 'hash') ? keys %$input : @$input)
879 if ( $afterFirst ++ )
881 defined addInterStream($gzip, $x, $element, $inputIsFilename)
885 defined $gzip->_wr2($element, $inputIsFilename)
890 return $gzip->close() ;
898 my $inputIsFilename = shift;
900 my $input = $source ;
901 if (! $inputIsFilename)
907 if ( ref $input && ref $input eq 'SCALAR' )
909 return $self->syswrite($input, @_) ;
912 if ( ! ref $input || isaFilehandle($input))
914 my $isFilehandle = isaFilehandle($input) ;
918 if ( ! $isFilehandle )
920 $fh = new IO::File "<$input"
921 or return $self->saveErrorString(undef, "cannot open file '$input': $!", $!) ;
923 binmode $fh if *$self->{Got}->valueOrDefault('BinModeIn') ;
928 while (($status = read($fh, $buff, 4096)) > 0) {
929 $count += length $buff;
930 defined $self->syswrite($buff, @_)
934 return $self->saveErrorString(undef, $!, $!)
937 if ( (!$isFilehandle || *$self->{AutoClose}) && $input ne '-')
946 croak "Should no be here";
955 my $inputIsFilename = shift ;
957 if ($x->{Got}->value('MultiStream'))
959 # For gzip, if input is simple filename, populate Name & Time in
960 # gzip header from filename by default.
961 if ($x->{Type} eq 'rfc1952' and isaFilename($input) and $inputIsFilename)
963 my $defaultTime = (stat($input))[8] ;
965 $x->{Got}->value('Name' => $input)
966 if ! $x->{ParsedName};
968 $x->{Got}->value('Time' => $defaultTime)
969 if ! $x->{ParsedTime};
972 # TODO -- newStream needs to allow gzip header to be modified
973 return $gzip->newStream();
975 elsif ($x->{Got}->value('AutoFlush'))
977 return $gzip->flush(Z_FULL_FLUSH);
985 return $_[0] if ref($_[0]);
999 # TODO - memory leak with 5.8.0 - this isn't called until
1000 # global destruction
1009 # my $class = shift ;
1011 # #local $Carp::CarpLevel = 1;
1013 # if ( ! ref $_[0] ||
1014 # ref $_[0] eq 'SCALAR' ||
1015 # #ref $_[0] eq 'CODE' ||
1016 # isaFilehandle($_[0]) )
1018 # my $inType = whatIs($_[0]);
1019 # my $outType = whatIs($_[1]);
1021 # if ($inType eq 'filename' )
1023 # croak "$class: input filename is undef or null string"
1024 # if ! defined $_[0] || $_[0] eq '' ;
1026 # if ($_[0] ne '-' && ! -e $_[0] )
1028 # ${$_[2]} = "input file '$_[0]' does not exist";
1035 # ${$_[2]} = "cannot open file '$_[0]': $!";
1040 # elsif ($inType eq 'fileglob' )
1045 # croak("$class: input and output $inType are identical")
1046 # if defined $outType && $inType eq $outType && $_[0] eq $_[1] ;
1051 # croak "$class: input parameter not a filename, filehandle, array ref or scalar ref"
1052 # unless ref $_[0] eq 'ARRAY' ;
1054 # my $array = shift @_ ;
1055 # foreach my $element ( @{ $array } )
1058 # unless validateInput($class, $element, @_);
1067 # my $self = shift ;
1069 # if ( isaFilehandle $_[0] )
1071 # return $self->_wr(@_);
1076 # if ( ref $_[0] eq 'SCALAR' )
1077 # { return $self->syswrite(@_) }
1079 # if ( ref $_[0] eq 'ARRAY' )
1082 # validateInput(*$self->{ClassName} . "::write", $_[0], *$self->{Output}, \$str, $num)
1083 # or return $self->saveErrorString(undef, $str, $num);
1085 # return $self->_wr(@_);
1088 # croak *$self->{ClassName} . "::write: input parameter not a filename, filehandle, array ref or scalar ref";
1091 # # Not a reference or a filehandle
1092 # return $self->syswrite(@_) ;
1097 # my $self = shift ;
1099 # if ( ref $_[0] && ref $_[0] eq 'SCALAR' )
1101 # return $self->syswrite(@_) ;
1104 # if ( ! ref $_[0] || isaFilehandle($_[0]))
1106 # my $item = shift @_ ;
1107 # my $isFilehandle = isaFilehandle($item) ;
1111 # if ( ! $isFilehandle )
1113 # $fh = new IO::File "<$item"
1114 # or return $self->saveErrorString(undef, "cannot open file '$item': $!", $!) ;
1120 # while (($status = read($fh, $buff, 4096)) > 0) {
1121 # $count += length $buff;
1122 # defined $self->syswrite($buff, @_)
1126 # return $self->saveErrorString(undef, $!, $!)
1130 # if ( !$isFilehandle || *$self->{AutoClose} )
1139 # #if ref $_[0] eq 'CODE' ;
1141 # # then must be ARRAY ref
1143 # my $array = shift @_ ;
1144 # foreach my $element ( @{ $array } )
1146 # my $got = $self->_wr($element, @_) ;
1149 # unless defined $got ;
1164 croak *$self->{ClassName} . "::write: not a scalar reference"
1165 unless ref $_[0] eq 'SCALAR' ;
1173 my $slen = defined $$buffer ? length($$buffer) : 0;
1176 $len = $_[1] if $_[1] < $len;
1179 $offset = $_[2] || 0;
1180 croak *$self->{ClassName} . "::write: offset outside string" if $offset > $slen;
1183 croak *$self->{ClassName} . "::write: offset outside string" if $offset < 0;
1185 my $rem = $slen - $offset;
1186 $len = $rem if $rem < $len;
1189 $buffer = \substr($$buffer, $offset, $len) ;
1192 my $buffer_length = defined $$buffer ? length($$buffer) : 0 ;
1193 *$self->{BytesWritten} += $buffer_length ;
1194 my $rest = GZIP_ISIZE_MAX - *$self->{ISize} ;
1195 if ($buffer_length > $rest) {
1196 *$self->{ISize} = $buffer_length - $rest - 1;
1199 *$self->{ISize} += $buffer_length ;
1202 # if (*$self->{Encoding}) {
1203 # $$buffer = *$self->{Encoding}->encode($$buffer);
1206 #my $length = length $$buffer;
1207 my $status = *$self->{Deflate}->deflate($buffer, *$self->{Buffer}) ;
1209 return $self->saveErrorString(undef,"Deflate Error: $status")
1210 if $self->saveStatus($status) != Z_OK ;
1212 if ( defined *$self->{FH} and length ${ *$self->{Buffer} }) {
1213 defined *$self->{FH}->write( ${ *$self->{Buffer} }, length ${ *$self->{Buffer} } )
1214 or return $self->saveErrorString(undef, $!, $!);
1215 ${ *$self->{Buffer} } = '' ;
1218 return $buffer_length;
1226 # $self = *$self{GLOB} ;
1231 defined $self->syswrite(join($,, @_) . $\);
1233 defined $self->syswrite(join("", @_) . $\);
1237 defined $self->syswrite(join($,, @_));
1239 defined $self->syswrite(join("", @_));
1248 defined $self->syswrite(sprintf($fmt, @_));
1256 my $opt = shift || Z_FINISH ;
1257 my $status = *$self->{Deflate}->flush(*$self->{Buffer}, $opt) ;
1258 return $self->saveErrorString(0,"Deflate Error: $status")
1259 if $self->saveStatus($status) != Z_OK ;
1261 if ( defined *$self->{FH} ) {
1262 *$self->{FH}->clearerr();
1263 defined *$self->{FH}->write(${ *$self->{Buffer} }, length ${ *$self->{Buffer} })
1264 or return $self->saveErrorString(0, $!, $!);
1265 ${ *$self->{Buffer} } = '' ;
1275 $self->_writeTrailer(GZIP_MINIMUM_HEADER)
1278 my $status = *$self->{Deflate}->deflateReset() ;
1279 return $self->saveErrorString(0,"Deflate Error: $status")
1280 if $self->saveStatus($status) != Z_OK ;
1282 *$self->{BytesWritten} = 0 ;
1283 *$self->{ISize} = 0 ;
1291 my $nextHeader = shift || '' ;
1293 my $status = *$self->{Deflate}->flush(*$self->{Buffer}) ;
1294 return $self->saveErrorString(0,"Deflate Error: $status")
1295 if $self->saveStatus($status) != Z_OK ;
1297 if (*$self->{OutputGzip}) {
1298 ${ *$self->{Buffer} } .= pack("V V", *$self->{Deflate}->crc32(),
1300 ${ *$self->{Buffer} } .= $nextHeader ;
1303 if (*$self->{OutputDeflate}) {
1304 ${ *$self->{Buffer} } .= pack("N", *$self->{Deflate}->adler32() );
1305 ${ *$self->{Buffer} } .= *$self->{Header} ;
1308 return 1 if ! defined *$self->{FH} ;
1310 defined *$self->{FH}->write(${ *$self->{Buffer} }, length ${ *$self->{Buffer} })
1311 or return $self->saveErrorString(0, $!, $!);
1313 ${ *$self->{Buffer} } = '' ;
1322 return 1 if *$self->{Closed} || ! *$self->{Deflate} ;
1323 *$self->{Closed} = 1 ;
1329 $self->_writeTrailer()
1335 my $status = *$self->{Deflate}->flush(*$self->{Buffer}) ;
1336 return $self->saveErrorString(0,"Deflate Error: $status")
1337 if $self->saveStatus($status) != Z_OK ;
1339 if (*$self->{OutputGzip}) {
1340 ${ *$self->{Buffer} } .= pack("V V", *$self->{Deflate}->crc32(),
1344 if (*$self->{OutputDeflate}) {
1345 ${ *$self->{Buffer} } .= pack("N", *$self->{Deflate}->adler32() );
1349 return 1 if ! defined *$self->{FH} ;
1351 defined *$self->{FH}->write(${ *$self->{Buffer} }, length( ${ *$self->{Buffer} } ))
1352 or return $self->saveErrorString(0, $!, $!);
1354 ${ *$self->{Buffer} } = '' ;
1357 if (defined *$self->{FH}) {
1358 #if (! *$self->{Handle} || *$self->{AutoClose}) {
1359 if ((! *$self->{Handle} || *$self->{AutoClose}) && ! *$self->{StdIO}) {
1361 *$self->{FH}->close()
1362 or return $self->saveErrorString(0, $!, $!);
1364 delete *$self->{FH} ;
1365 # This delete can set $! in older Perls, so reset the errno
1376 my $strategy = shift ;
1378 my $status = *$self->{Deflate}->deflateParams(-Level => $level,
1379 -Strategy => $strategy) ;
1380 return $self->saveErrorString(0,"deflateParams Error: $status")
1381 if $self->saveStatus($status) != Z_OK ;
1393 # my $self = shift ;
1394 # return *$self->{Deflate}->crc32() ;
1399 # my $self = shift ;
1400 # return *$self->{Deflate}->msg() ;
1405 # my $self = shift ;
1406 # return *$self->{Deflate}->dict_adler() ;
1411 # my $self = shift ;
1412 # return *$self->{Deflate}->get_Level() ;
1417 # my $self = shift ;
1418 # return *$self->{Deflate}->get_Strategy() ;
1426 #return *$self->{Deflate}->total_in();
1427 return *$self->{BytesWritten} ;
1434 return *$self->{Closed} ;
1441 my $position = shift;
1442 my $whence = shift ;
1444 my $here = $self->tell() ;
1447 #use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END);
1450 if ($whence == IO::Handle::SEEK_SET) {
1451 $target = $position ;
1453 elsif ($whence == IO::Handle::SEEK_CUR || $whence == IO::Handle::SEEK_END) {
1454 $target = $here + $position ;
1457 croak *$self->{ClassName} . "::seek: unknown value, $whence, for whence parameter";
1460 # short circuit if seeking to current offset
1461 return 1 if $target == $here ;
1463 # Outlaw any attempt to seek backwards
1464 croak *$self->{ClassName} . "::seek: cannot seek backwards"
1465 if $target < $here ;
1467 # Walk the file to the new offset
1468 my $offset = $target - $here ;
1471 defined $self->syswrite("\x00" x $offset)
1480 # my $self = shift ;
1481 # return defined *$self->{FH}
1482 # ? binmode *$self->{FH}
1489 return defined *$self->{FH}
1490 ? *$self->{FH}->fileno()
1497 return sub { croak "$name Not Available: File opened only for output" ; } ;
1500 *read = _notAvailable('read');
1501 *READ = _notAvailable('read');
1502 *readline = _notAvailable('readline');
1503 *READLINE = _notAvailable('readline');
1504 *getc = _notAvailable('getc');
1505 *GETC = _notAvailable('getc');
1510 *WRITE = \&syswrite;
1511 *write = \&syswrite;
1516 *BINMODE = \&binmode;
1518 #*sysread = \&_notAvailable;
1519 #*syswrite = \&_write;
1527 IO::Compress::Gzip - Perl interface to write RFC 1952 files/buffers
1531 use IO::Compress::Gzip qw(gzip $GzipError) ;
1534 my $status = gzip $input => $output [,OPTS]
1535 or die "gzip failed: $GzipError\n";
1537 my $z = new IO::Compress::Gzip $output [,OPTS]
1538 or die "gzip failed: $GzipError\n";
1541 $z->printf($format, $string);
1543 $z->syswrite($string [, $length, $offset]);
1547 $z->seek($position, $whence);
1551 $z->deflateParams();
1559 printf $z $format, $string;
1560 syswrite $z, $string [, $length, $offset];
1564 seek $z, $position, $whence
1574 B<WARNING -- This is a Beta release>.
1578 =item * DO NOT use in production code.
1580 =item * The documentation is incomplete in places.
1582 =item * Parts of the interface defined here are tentative.
1584 =item * Please report any problems you find.
1590 This module provides a Perl interface that allows writing compressed
1591 data to files or buffer as defined in RFC 1952.
1594 All the gzip headers defined in RFC 1952 can be created using
1600 For reading RFC 1952 files/buffers, see the companion module
1601 L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>.
1604 =head1 Functional Interface
1606 A top-level function, C<gzip>, is provided to carry out "one-shot"
1607 compression between buffers and/or files. For finer control over the compression process, see the L</"OO Interface"> section.
1609 use IO::Compress::Gzip qw(gzip $GzipError) ;
1611 gzip $input => $output [,OPTS]
1612 or die "gzip failed: $GzipError\n";
1615 or die "gzip failed: $GzipError\n";
1617 The functional interface needs Perl5.005 or better.
1620 =head2 gzip $input => $output [, OPTS]
1622 If the first parameter is not a hash reference C<gzip> expects
1623 at least two parameters, C<$input> and C<$output>.
1625 =head3 The C<$input> parameter
1627 The parameter, C<$input>, is used to define the source of
1628 the uncompressed data.
1630 It can take one of the following forms:
1636 If the C<$input> parameter is a simple scalar, it is assumed to be a
1637 filename. This file will be opened for reading and the input data
1638 will be read from it.
1642 If the C<$input> parameter is a filehandle, the input data will be
1644 The string '-' can be used as an alias for standard input.
1646 =item A scalar reference
1648 If C<$input> is a scalar reference, the input data will be read
1651 =item An array reference
1653 If C<$input> is an array reference, the input data will be read from each
1654 element of the array in turn. The action taken by C<gzip> with
1655 each element of the array will depend on the type of data stored
1656 in it. You can mix and match any of the types defined in this list,
1657 excluding other array or hash references.
1658 The complete array will be walked to ensure that it only
1659 contains valid data types before any data is compressed.
1661 =item An Input FileGlob string
1663 If C<$input> is a string that is delimited by the characters "<" and ">"
1664 C<gzip> will assume that it is an I<input fileglob string>. The
1665 input is the list of files that match the fileglob.
1667 If the fileglob does not match any files ...
1669 See L<File::GlobMapper|File::GlobMapper> for more details.
1674 If the C<$input> parameter is any other type, C<undef> will be returned.
1678 In addition, if C<$input> is a simple filename, the default values for
1679 two of the gzip header fields created by this function will be sourced
1680 from that file -- the NAME gzip header field will be populated with
1681 the filename itself, and the MTIME header field will be set to the
1682 modification time of the file.
1683 The intention here is to mirror part of the behavior of the gzip
1685 If you do not want to use these defaults they can be overridden by
1686 explicitly setting the C<Name> and C<Time> options.
1690 =head3 The C<$output> parameter
1692 The parameter C<$output> is used to control the destination of the
1693 compressed data. This parameter can take one of these forms.
1699 If the C<$output> parameter is a simple scalar, it is assumed to be a filename.
1700 This file will be opened for writing and the compressed data will be
1705 If the C<$output> parameter is a filehandle, the compressed data will
1707 The string '-' can be used as an alias for standard output.
1710 =item A scalar reference
1712 If C<$output> is a scalar reference, the compressed data will be stored
1716 =item A Hash Reference
1718 If C<$output> is a hash reference, the compressed data will be written
1719 to C<$output{$input}> as a scalar reference.
1721 When C<$output> is a hash reference, C<$input> must be either a filename or
1722 list of filenames. Anything else is an error.
1725 =item An Array Reference
1727 If C<$output> is an array reference, the compressed data will be pushed
1730 =item An Output FileGlob
1732 If C<$output> is a string that is delimited by the characters "<" and ">"
1733 C<gzip> will assume that it is an I<output fileglob string>. The
1734 output is the list of files that match the fileglob.
1736 When C<$output> is an fileglob string, C<$input> must also be a fileglob
1737 string. Anything else is an error.
1741 If the C<$output> parameter is any other type, C<undef> will be returned.
1743 =head2 gzip \%hash [, OPTS]
1745 If the first parameter is a hash reference, C<\%hash>, this will be used to
1746 define both the source of uncompressed data and to control where the
1747 compressed data is output. Each key/value pair in the hash defines a
1748 mapping between an input filename, stored in the key, and an output
1749 file/buffer, stored in the value. Although the input can only be a filename,
1750 there is more flexibility to control the destination of the compressed
1751 data. This is determined by the type of the value. Valid types are
1757 If the value is C<undef> the compressed data will be written to the
1758 value as a scalar reference.
1762 If the value is a simple scalar, it is assumed to be a filename. This file will
1763 be opened for writing and the compressed data will be written to it.
1767 If the value is a filehandle, the compressed data will be
1769 The string '-' can be used as an alias for standard output.
1772 =item A scalar reference
1774 If the value is a scalar reference, the compressed data will be stored
1775 in the buffer that is referenced by the scalar.
1778 =item A Hash Reference
1780 If the value is a hash reference, the compressed data will be written
1781 to C<$hash{$input}> as a scalar reference.
1783 =item An Array Reference
1785 If C<$output> is an array reference, the compressed data will be pushed
1790 Any other type is a error.
1794 When C<$input> maps to multiple files/buffers and C<$output> is a single
1795 file/buffer the compressed input files/buffers will all be stored in
1796 C<$output> as a single compressed stream.
1800 =head2 Optional Parameters
1802 Unless specified below, the optional parameters for C<gzip>,
1803 C<OPTS>, are the same as those used with the OO interface defined in the
1804 L</"Constructor Options"> section below.
1808 =item AutoClose =E<gt> 0|1
1810 This option applies to any input or output data streams to C<gzip>
1811 that are filehandles.
1813 If C<AutoClose> is specified, and the value is true, it will result in all
1814 input and/or output filehandles being closed once C<gzip> has
1817 This parameter defaults to 0.
1821 =item -Append =E<gt> 0|1
1832 To read the contents of the file C<file1.txt> and write the compressed
1833 data to the file C<file1.txt.gz>.
1837 use IO::Compress::Gzip qw(gzip $GzipError) ;
1839 my $input = "file1.txt";
1840 gzip $input => "$input.gz"
1841 or die "gzip failed: $GzipError\n";
1844 To read from an existing Perl filehandle, C<$input>, and write the
1845 compressed data to a buffer, C<$buffer>.
1849 use IO::Compress::Gzip qw(gzip $GzipError) ;
1852 my $input = new IO::File "<file1.txt"
1853 or die "Cannot open 'file1.txt': $!\n" ;
1855 gzip $input => \$buffer
1856 or die "gzip failed: $GzipError\n";
1858 To compress all files in the directory "/my/home" that match "*.txt"
1859 and store the compressed data in the same directory
1863 use IO::Compress::Gzip qw(gzip $GzipError) ;
1865 gzip '</my/home/*.txt>' => '<*.gz>'
1866 or die "gzip failed: $GzipError\n";
1868 and if you want to compress each file one at a time, this will do the trick
1872 use IO::Compress::Gzip qw(gzip $GzipError) ;
1874 for my $input ( glob "/my/home/*.txt" )
1876 my $output = "$input.gz" ;
1877 gzip $input => $output
1878 or die "Error compressing '$input': $GzipError\n";
1886 The format of the constructor for C<IO::Compress::Gzip> is shown below
1888 my $z = new IO::Compress::Gzip $output [,OPTS]
1889 or die "IO::Compress::Gzip failed: $GzipError\n";
1891 It returns an C<IO::Compress::Gzip> object on success and undef on failure.
1892 The variable C<$GzipError> will contain an error message on failure.
1894 If you are running Perl 5.005 or better the object, C<$z>, returned from
1895 IO::Compress::Gzip can be used exactly like an L<IO::File|IO::File> filehandle.
1896 This means that all normal output file operations can be carried out
1898 For example, to write to a compressed file/buffer you can use either of
1901 $z->print("hello world\n");
1902 print $z "hello world\n";
1904 The mandatory parameter C<$output> is used to control the destination
1905 of the compressed data. This parameter can take one of these forms.
1911 If the C<$output> parameter is a simple scalar, it is assumed to be a
1912 filename. This file will be opened for writing and the compressed data
1913 will be written to it.
1917 If the C<$output> parameter is a filehandle, the compressed data will be
1919 The string '-' can be used as an alias for standard output.
1922 =item A scalar reference
1924 If C<$output> is a scalar reference, the compressed data will be stored
1929 If the C<$output> parameter is any other type, C<IO::Compress::Gzip>::new will
1932 =head2 Constructor Options
1934 C<OPTS> is any combination of the following options:
1938 =item -AutoClose =E<gt> 0|1
1940 This option is only valid when the C<$output> parameter is a filehandle. If
1941 specified, and the value is true, it will result in the C<$output> being closed
1942 once either the C<close> method is called or the C<IO::Compress::Gzip> object is
1945 This parameter defaults to 0.
1947 =item -Append =E<gt> 0|1
1949 Opens C<$output> in append mode.
1951 The behaviour of this option is dependant on the type of C<$output>.
1957 If C<$output> is a buffer and C<Append> is enabled, all compressed data will be
1958 append to the end if C<$output>. Otherwise C<$output> will be cleared before
1959 any data is written to it.
1963 If C<$output> is a filename and C<Append> is enabled, the file will be opened
1964 in append mode. Otherwise the contents of the file, if any, will be truncated
1965 before any compressed data is written to it.
1967 =item * A Filehandle
1969 If C<$output> is a filehandle, the file pointer will be positioned to the end
1970 of the file via a call to C<seek> before any compressed data is written to it.
1971 Otherwise the file pointer will not be moved.
1975 This parameter defaults to 0.
1977 =item -Merge =E<gt> 0|1
1979 This option is used to compress input data and append it to an existing
1980 compressed data stream in C<$output>. The end result is a single compressed
1981 data stream stored in C<$output>.
1985 It is a fatal error to attempt to use this option when C<$output> is not an RFC
1990 There are a number of other limitations with the C<Merge> option:
1996 This module needs to have been built with zlib 1.2.1 or better to work. A fatal
1997 error will be thrown if C<Merge> is used with an older version of zlib.
2001 If C<$output> is a file or a filehandle, it must be seekable.
2006 This parameter defaults to 0.
2010 Defines the compression level used by zlib. The value should either be
2011 a number between 0 and 9 (0 means no compression and 9 is maximum
2012 compression), or one of the symbolic constants defined below.
2017 Z_DEFAULT_COMPRESSION
2019 The default is Z_DEFAULT_COMPRESSION.
2021 Note, these constants are not imported by C<IO::Compress::Gzip> by default.
2023 use IO::Compress::Gzip qw(:strategy);
2024 use IO::Compress::Gzip qw(:constants);
2025 use IO::Compress::Gzip qw(:all);
2029 Defines the strategy used to tune the compression. Use one of the symbolic
2030 constants defined below.
2038 The default is Z_DEFAULT_STRATEGY.
2044 =item -Mimimal =E<gt> 0|1
2046 If specified, this option will force the creation of the smallest possible
2047 compliant gzip header (which is exactly 10 bytes long) as defined in
2050 See the section titled "Compliance" in RFC 1952 for a definition
2051 of the values used for the fields in the gzip header.
2053 All other parameters that control the content of the gzip header will
2054 be ignored if this parameter is set to 1.
2056 This parameter defaults to 0.
2058 =item -Comment =E<gt> $comment
2060 Stores the contents of C<$comment> in the COMMENT field in
2062 By default, no comment field is written to the gzip file.
2064 If the C<-Strict> option is enabled, the comment can only consist of ISO
2065 8859-1 characters plus line feed.
2067 If the C<-Strict> option is disabled, the comment field can contain any
2068 character except NULL. If any null characters are present, the field
2069 will be truncated at the first NULL.
2071 =item -Name =E<gt> $string
2073 Stores the contents of C<$string> in the gzip NAME header field. If
2074 C<Name> is not specified, no gzip NAME field will be created.
2076 If the C<-Strict> option is enabled, C<$string> can only consist of ISO
2079 If C<-Strict> is disabled, then C<$string> can contain any character
2080 except NULL. If any null characters are present, the field will be
2081 truncated at the first NULL.
2083 =item -Time =E<gt> $number
2085 Sets the MTIME field in the gzip header to $number.
2087 This field defaults to the time the C<IO::Compress::Gzip> object was created
2088 if this option is not specified.
2090 =item -TextFlag =E<gt> 0|1
2092 This parameter controls the setting of the FLG.FTEXT bit in the gzip header. It
2093 is used to signal that the data stored in the gzip file/buffer is probably
2098 =item -HeaderCRC =E<gt> 0|1
2100 When true this parameter will set the FLG.FHCRC bit to 1 in the gzip header and
2101 set the CRC16 header field to the CRC of the complete gzip header except the
2104 B<Note> that gzip files created with the C<HeaderCRC> flag set to 1 cannot be
2105 read by most, if not all, of the the standard gunzip utilities, most notably
2106 gzip version 1.2.4. You should therefore avoid using this option if you want to
2107 maximise the portability of your gzip files.
2109 This parameter defaults to 0.
2111 =item -OS_Code =E<gt> $value
2113 Stores C<$value> in the gzip OS header field. A number between 0 and
2116 If not specified, this parameter defaults to the OS code of the Operating
2117 System this module was built on. The value 3 is used as a catch-all for all
2118 Unix variants and unknown Operating Systems.
2120 =item -ExtraField =E<gt> $data
2122 This parameter allows additional metadata to be stored in the ExtraField in the
2123 gzip header. An RFC1952 compliant ExtraField consists of zero or more
2124 subfields. Each subfield consists of a two byte header followed by the subfield
2127 The list of subfields can be supplied in any of the following formats
2129 -ExtraField => [$id1, $data1,
2133 -ExtraField => [ [$id1 => $data1],
2137 -ExtraField => { $id1 => $data1,
2142 Where C<$id1>, C<$id2> are two byte subfield ID's. The second byte of
2143 the ID cannot be 0, unless the C<Strict> option has been disabled.
2145 If you use the hash syntax, you have no control over the order in which
2146 the ExtraSubFields are stored, plus you cannot have SubFields with
2149 Alternatively the list of subfields can by supplied as a scalar, thus
2151 -ExtraField => $rawdata
2153 If you use the raw format, and the C<Strict> option is enabled,
2154 C<IO::Compress::Gzip> will check that C<$rawdata> consists of zero or more
2155 conformant sub-fields. When C<Strict> is disabled, C<$rawdata> can
2156 consist of any arbitrary byte stream.
2158 The maximum size of the Extra Field 65535 bytes.
2160 =item -ExtraFlags =E<gt> $value
2162 Sets the XFL byte in the gzip header to C<$value>.
2164 If this option is not present, the value stored in XFL field will be determined
2165 by the setting of the C<Level> option.
2167 If C<Level =E<gt> Z_BEST_SPEED> has been specified then XFL is set to 2.
2168 If C<Level =E<gt> Z_BEST_COMPRESSION> has been specified then XFL is set to 4.
2169 Otherwise XFL is set to 0.
2173 =item -Strict =E<gt> 0|1
2177 C<Strict> will optionally police the values supplied with other options
2178 to ensure they are compliant with RFC1952.
2180 This option is enabled by default.
2182 If C<Strict> is enabled the following behavior will be policed:
2188 The value supplied with the C<Name> option can only contain ISO 8859-1
2193 The value supplied with the C<Comment> option can only contain ISO 8859-1
2194 characters plus line-feed.
2198 The values supplied with the C<-Name> and C<-Comment> options cannot
2199 contain multiple embedded nulls.
2203 If an C<ExtraField> option is specified and it is a simple scalar,
2204 it must conform to the sub-field structure as defined in RFC1952.
2208 If an C<ExtraField> option is specified the second byte of the ID will be
2209 checked in each subfield to ensure that it does not contain the reserved
2214 When C<Strict> is disabled the following behavior will be policed:
2220 The value supplied with C<-Name> option can contain
2221 any character except NULL.
2225 The value supplied with C<-Comment> option can contain any character
2230 The values supplied with the C<-Name> and C<-Comment> options can contain
2231 multiple embedded nulls. The string written to the gzip header will
2232 consist of the characters up to, but not including, the first embedded
2237 If an C<ExtraField> option is specified and it is a simple scalar, the
2238 structure will not be checked. The only error is if the length is too big.
2242 The ID header in an C<ExtraField> sub-field can consist of any two bytes.
2263 Compresses and outputs the contents of the C<$data> parameter. This
2264 has the same behavior as the C<print> built-in.
2266 Returns true if successful.
2272 $z->printf($format, $data)
2273 printf $z $format, $data
2275 Compresses and outputs the contents of the C<$data> parameter.
2277 Returns true if successful.
2284 $z->syswrite $data, $length
2285 $z->syswrite $data, $length, $offset
2288 syswrite $z, $data, $length
2289 syswrite $z, $data, $length, $offset
2291 Compresses and outputs the contents of the C<$data> parameter.
2293 Returns the number of uncompressed bytes written, or C<undef> if
2301 $z->write $data, $length
2302 $z->write $data, $length, $offset
2304 Compresses and outputs the contents of the C<$data> parameter.
2306 Returns the number of uncompressed bytes written, or C<undef> if
2314 $z->flush($flush_type);
2316 flush $z $flush_type;
2318 Flushes any pending compressed data to the output file/buffer.
2320 This method takes an optional parameter, C<$flush_type>, that controls
2321 how the flushing will be carried out. By default the C<$flush_type>
2322 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
2323 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
2324 strongly recommended that you only set the C<flush_type> parameter if
2325 you fully understand the implications of what it does - overuse of C<flush>
2326 can seriously degrade the level of compression achieved. See the C<zlib>
2327 documentation for details.
2329 Returns true on success.
2339 Returns the uncompressed file offset.
2350 Returns true if the C<close> method has been called.
2356 $z->seek($position, $whence);
2357 seek($z, $position, $whence);
2362 Provides a sub-set of the C<seek> functionality, with the restriction
2363 that it is only legal to seek forward in the output file/buffer.
2364 It is a fatal error to attempt to seek backward.
2366 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
2370 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
2371 SEEK_CUR or SEEK_END.
2373 Returns 1 on success, 0 on failure.
2382 This is a noop provided for completeness.
2389 If the C<$z> object is associated with a file, this method will return
2390 the underlying filehandle.
2392 If the C<$z> object is is associated with a buffer, this method will
2402 Flushes any pending compressed data and then closes the output file/buffer.
2406 For most versions of Perl this method will be automatically invoked if
2407 the IO::Compress::Gzip object is destroyed (either explicitly or by the
2408 variable with the reference to the object going out of scope). The
2409 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
2410 these cases, the C<close> method will be called automatically, but
2411 not until global destruction of all live objects when the program is
2414 Therefore, if you want your scripts to be able to run on all versions
2415 of Perl, you should call C<close> explicitly and not rely on automatic
2418 Returns true on success, otherwise 0.
2420 If the C<AutoClose> option has been enabled when the IO::Compress::Gzip
2421 object was created, and the object is associated with a file, the
2422 underlying file will also be closed.
2435 =head2 deflateParams
2445 A number of symbolic constants are required by some methods in
2446 C<IO::Compress::Gzip>. None are imported by default.
2452 Imports C<gzip>, C<$GzipError> and all symbolic
2453 constants that can be used by C<IO::Compress::Gzip>. Same as doing this
2455 use IO::Compress::Gzip qw(gzip $GzipError :constants) ;
2459 Import all symbolic constants. Same as doing this
2461 use IO::Compress::Gzip qw(:flush :level :strategy) ;
2465 These symbolic constants are used by the C<flush> method.
2477 These symbolic constants are used by the C<Level> option in the constructor.
2482 Z_DEFAULT_COMPRESSION
2487 These symbolic constants are used by the C<Strategy> option in the constructor.
2510 L<Compress::Zlib>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Uncompress::AnyInflate>
2512 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
2514 L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
2515 L<IO::Zlib|IO::Zlib>
2517 For RFC 1950, 1951 and 1952 see
2518 F<http://www.faqs.org/rfcs/rfc1950.html>,
2519 F<http://www.faqs.org/rfcs/rfc1951.html> and
2520 F<http://www.faqs.org/rfcs/rfc1952.html>
2522 The primary site for the gzip program is F<http://www.gzip.org>.
2526 The I<IO::Compress::Gzip> module was written by Paul Marquess,
2527 F<pmqs@cpan.org>. The latest copy of the module can be
2528 found on CPAN in F<modules/by-module/Compress/Compress-Zlib-x.x.tar.gz>.
2530 The I<zlib> compression library was written by Jean-loup Gailly
2531 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
2533 The primary site for the I<zlib> compression library is
2534 F<http://www.zlib.org>.
2536 =head1 MODIFICATION HISTORY
2538 See the Changes file.
2540 =head1 COPYRIGHT AND LICENSE
2543 Copyright (c) 2005 Paul Marquess. All rights reserved.
2544 This program is free software; you can redistribute it and/or
2545 modify it under the same terms as Perl itself.