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],
463 #'Method' => [Parse_unsigned, Z_DEFLATED],
464 'Level' => [Parse_signed, Z_DEFAULT_COMPRESSION],
465 'Strategy' => [Parse_signed, Z_DEFAULT_STRATEGY],
468 'Minimal' => [Parse_boolean, 0],
469 'Comment' => [Parse_any, undef],
470 'Name' => [Parse_any, undef],
471 'Time' => [Parse_any, undef],
472 'TextFlag' => [Parse_boolean, 0],
473 'HeaderCRC' => [Parse_boolean, 0],
474 'OS_Code' => [Parse_unsigned, $Compress::Zlib::gzip_os_code],
475 'ExtraField'=> [Parse_string, undef],
476 'ExtraFlags'=> [Parse_any, undef],
480 'AutoClose' => [Parse_boolean, 0],
481 #'Encoding' => [Parse_any, undef],
482 'CRC32' => [Parse_boolean, 0],
483 'ADLER32' => [Parse_boolean, 0],
484 'Strict' => [Parse_boolean, 1],
485 'Append' => [Parse_boolean, 0],
486 'Merge' => [Parse_boolean, 0],
489 #'Method' => [Parse_unsigned, Z_DEFLATED],
490 'Level' => [Parse_signed, Z_DEFAULT_COMPRESSION],
491 'Strategy' => [Parse_signed, Z_DEFAULT_STRATEGY],
493 @_) or croak "${class}: $got->{Error}" ;
503 my $error_ref = shift ;
505 croak("$class: Missing Output parameter")
508 my $outValue = shift ;
514 $got = checkParams($class, $type, @_)
518 my $rfc1952 = ($type eq 'rfc1952');
519 my $rfc1950 = ($type eq 'rfc1950');
520 my $rfc1951 = ($type eq 'rfc1951');
522 my $obj = bless Symbol::gensym(), ref($class) || $class;
523 tie *$obj, $obj if $] >= 5.005;
525 *$obj->{Closed} = 1 ;
527 *$obj->{Error} = $error_ref ;
529 my $lax = ! $got->value('Strict') ;
531 my $outType = whatIsOutput($outValue);
533 ckOutputParam($class, $outValue, $error_ref)
536 if ($outType eq 'buffer') {
537 *$obj->{Buffer} = $outValue;
541 *$obj->{Buffer} = \$buff ;
544 # Merge implies Append
545 my $merge = $got->value('Merge') ;
546 my $appendOutput = $got->value('Append') || $merge ;
550 # Switch off Merge mode if output file/buffer is empty/doesn't exist
551 if (($outType eq 'buffer' && length $$outValue == 0 ) ||
552 ($outType ne 'buffer' && (! -e $outValue || (-w _ && -z _))) )
556 # If output is a file, check that it is writable
557 if ($outType eq 'filename' && -e $outValue && ! -w _)
558 { return $obj->saveErrorString(undef, "Output file '$outValue' is not writable" ) }
560 elsif ($outType eq 'handle' && ! -w $outValue)
561 { return $obj->saveErrorString(undef, "Output filehandle is not writable" ) }
565 # if ($got->parsed('Encoding')) {
566 # croak("$class: Encode module needed to use -Encoding")
569 # my $want_encoding = $got->value('Encoding');
570 # my $encoding = find_encoding($want_encoding);
572 # croak("$class: Encoding '$want_encoding' is not available")
575 # *$obj->{Encoding} = $encoding;
578 if ($rfc1952 && ! $merge) {
580 if (! $got->parsed('Time') ) {
581 # Modification time defaults to now.
582 $got->value('Time' => time) ;
585 # Check that the Name & Comment don't have embedded NULLs
586 # Also check that they only contain ISO 8859-1 chars.
587 if ($got->parsed('Name') && defined $got->value('Name')) {
588 my $name = $got->value('Name');
590 return $obj->saveErrorString(undef, "Null Character found in Name",
592 if ! $lax && $name =~ /\x00/ ;
594 return $obj->saveErrorString(undef, "Non ISO 8859-1 Character found in Name",
596 if ! $lax && $name =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ;
599 if ($got->parsed('Comment') && defined $got->value('Comment')) {
600 my $comment = $got->value('Comment');
602 return $obj->saveErrorString(undef, "Null Character found in Comment",
604 if ! $lax && $comment =~ /\x00/ ;
606 return $obj->saveErrorString(undef, "Non ISO 8859-1 Character found in Comment",
608 if ! $lax && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o;
611 if ($got->parsed('OS_Code') ) {
612 my $value = $got->value('OS_Code');
614 return $obj->saveErrorString(undef, "OS_Code must be between 0 and 255, got '$value'")
615 if $value < 0 || $value > 255 ;
619 # gzip only supports Deflate at present
620 $got->value('Method' => Z_DEFLATED) ;
622 if ( ! $got->parsed('ExtraFlags')) {
623 $got->value('ExtraFlags' => 2)
624 if $got->value('Level') == Z_BEST_SPEED ;
625 $got->value('ExtraFlags' => 4)
626 if $got->value('Level') == Z_BEST_COMPRESSION ;
629 if ($got->parsed('ExtraField')) {
631 my $bad = $obj->parseExtraField($got, $lax) ;
632 return $obj->saveErrorString(undef, $bad, Z_DATA_ERROR)
635 my $len = length $got->value('ExtraField') ;
636 return $obj->saveErrorString(undef, ExtraFieldError("Too Large"),
638 if $len > GZIP_FEXTRA_MAX_SIZE;
642 $obj->saveStatus(Z_OK) ;
648 (*$obj->{Deflate}, $status) = new Compress::Zlib::Deflate
650 -CRC32 => $rfc1952 || $got->value('CRC32'),
651 -ADLER32 => $rfc1950 || $got->value('ADLER32'),
652 -Level => $got->value('Level'),
653 -Strategy => $got->value('Strategy'),
654 -WindowBits => - MAX_WBITS;
655 return $obj->saveErrorString(undef, "Cannot create Deflate object: $status" )
656 if $obj->saveStatus($status) != Z_OK ;
658 *$obj->{BytesWritten} = 0 ;
661 *$obj->{Header} = mkDeflateHeader($got)
665 *$obj->{Header} = mkGzipHeader($got)
668 if ( $outType eq 'buffer') {
669 ${ *$obj->{Buffer} } = ''
670 unless $appendOutput ;
671 ${ *$obj->{Buffer} } .= *$obj->{Header};
674 if ($outType eq 'handle') {
676 *$obj->{FH} = $outValue ;
677 *$obj->{Handle} = 1 ;
680 seek(*$obj->{FH}, 0, SEEK_END)
681 or return $obj->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
685 elsif ($outType eq 'filename') {
689 *$obj->{FH} = new IO::File "$mode $outValue"
690 or return $obj->saveErrorString(undef, "cannot open file '$outValue': $!", $!) ;
691 *$obj->{StdIO} = ($outValue eq '-');
694 setBinModeOutput(*$obj->{FH}) ;
697 defined *$obj->{FH}->write(*$obj->{Header}, length(*$obj->{Header}))
698 or return $obj->saveErrorString(undef, $!, $!) ;
704 my %mapping = ( 'rfc1952' => ['IO::Uncompress::Gunzip', \$IO::Uncompress::Gunzip::GunzipError],
705 'rfc1950' => ['IO::Uncompress::Inflate', \$IO::Uncompress::Inflate::InflateError],
706 'rfc1951' => ['IO::Uncompress::RawInflate', \$IO::Uncompress::RawInflate::RawInflateError],
709 my $inf = IO::BaseInflate::new($mapping{$type}[0],
711 $error_ref, 0, $outValue,
717 return $obj->saveErrorString(undef, "Cannot create InflateScan object: $$error_ref" )
721 or return $obj->saveErrorString(undef, "Error Scanning: $$error_ref", $inf->errorNo) ;
722 $inf->zap($end_offset)
723 or return $obj->saveErrorString(undef, "Error Zapping: $$error_ref", $inf->errorNo) ;
725 (*$obj->{Deflate}, $status) = $inf->createDeflate();
727 *$obj->{Header} = *$inf->{Info}{Header};
729 *$obj->{ISize} = *$obj->{BytesWritten} = *$inf->{ISize} ;
731 if ( $outType eq 'buffer')
732 { substr( ${ *$obj->{Buffer} }, $end_offset) = '' }
733 elsif ($outType eq 'handle' || $outType eq 'filename') {
734 *$obj->{FH} = *$inf->{FH} ;
736 *$obj->{FH}->flush() ;
737 *$obj->{Handle} = 1 if $outType eq 'handle';
739 #seek(*$obj->{FH}, $end_offset, SEEK_SET)
740 *$obj->{FH}->seek($end_offset, SEEK_SET)
741 or return $obj->saveErrorString(undef, $!, $!) ;
745 *$obj->{Closed} = 0 ;
746 *$obj->{AutoClose} = $got->value('AutoClose') ;
747 *$obj->{OutputGzip} = $rfc1952;
748 *$obj->{OutputDeflate} = $rfc1950;
749 *$obj->{OutputRawDeflate} = $rfc1951;
750 *$obj->{Output} = $outValue;
751 *$obj->{ClassName} = $class;
760 my $error_ref = shift ;
762 my $name = (caller(1))[3] ;
764 croak "$name: expected at least 1 parameters\n"
771 my $x = new Validator($class, $type, $error_ref, $name, $input, $output)
774 push @_, $output if $haveOut && $x->{Hash};
776 my $got = checkParams($name, $type, @_)
780 $x->{ParsedTime} = $got->parsed('Time') ;
781 $x->{ParsedName} = $got->parsed('Name') ;
785 while (my($k, $v) = each %$input)
790 _singleTarget($x, 1, $k, $v, @_)
794 return keys %$input ;
800 foreach my $pair (@{ $x->{Pairs} })
802 my ($from, $to) = @$pair ;
803 _singleTarget($x, 1, $from, $to, @_)
807 return scalar @{ $x->{Pairs} } ;
810 if (! $x->{oneOutput} )
812 my $inFile = ($x->{inType} eq 'filenames'
813 || $x->{inType} eq 'filename');
815 $x->{inType} = $inFile ? 'filename' : 'buffer';
817 foreach my $in ($x->{oneInput} ? $input : @$input)
822 _singleTarget($x, $inFile, $in, \$out, @_)
825 if ($x->{outType} eq 'array')
826 { push @$output, \$out }
828 { $output->{$in} = \$out }
834 # finally the 1 to 1 and n to 1
835 return _singleTarget($x, 1, $input, $output, @_);
837 croak "should not be here" ;
843 my $inputIsFilename = shift;
847 # For gzip, if input is simple filename, populate Name & Time in
848 # gzip header from filename by default.
849 if ($x->{Type} eq 'rfc1952' and isaFilename($input) and $inputIsFilename)
851 my $defaultTime = (stat($input))[8] ;
853 $x->{Got}->value('Name' => $input)
854 if ! $x->{ParsedName};
856 $x->{Got}->value('Time' => $defaultTime)
857 if ! $x->{ParsedTime};
860 my $gzip = new($x->{Class}, $x->{Type}, $x->{Got}, $x->{Error}, @_)
866 defined $gzip->_wr2($input, $inputIsFilename)
872 my $inputIsFilename = ($x->{inType} ne 'array');
874 for my $element ( ($x->{inType} eq 'hash') ? keys %$input : @$input)
876 if ( $afterFirst ++ )
878 defined addInterStream($gzip, $x, $element, $inputIsFilename)
882 defined $gzip->_wr2($element, $inputIsFilename)
887 return $gzip->close() ;
895 my $inputIsFilename = shift;
897 my $input = $source ;
898 if (! $inputIsFilename)
904 if ( ref $input && ref $input eq 'SCALAR' )
906 return $self->syswrite($input, @_) ;
909 if ( ! ref $input || isaFilehandle($input))
911 my $isFilehandle = isaFilehandle($input) ;
915 if ( ! $isFilehandle )
917 $fh = new IO::File "<$input"
918 or return $self->saveErrorString(undef, "cannot open file '$input': $!", $!) ;
920 setBinModeInput($fh) ;
925 while (($status = read($fh, $buff, 4096)) > 0) {
926 $count += length $buff;
927 defined $self->syswrite($buff, @_)
931 return $self->saveErrorString(undef, $!, $!)
934 if ( (!$isFilehandle || *$self->{AutoClose}) && $input ne '-')
943 croak "Should no be here";
952 my $inputIsFilename = shift ;
954 if ($x->{Got}->value('MultiStream'))
956 # For gzip, if input is simple filename, populate Name & Time in
957 # gzip header from filename by default.
958 if ($x->{Type} eq 'rfc1952' and isaFilename($input) and $inputIsFilename)
960 my $defaultTime = (stat($input))[8] ;
962 $x->{Got}->value('Name' => $input)
963 if ! $x->{ParsedName};
965 $x->{Got}->value('Time' => $defaultTime)
966 if ! $x->{ParsedTime};
969 # TODO -- newStream needs to allow gzip header to be modified
970 return $gzip->newStream();
972 elsif ($x->{Got}->value('AutoFlush'))
974 return $gzip->flush(Z_FULL_FLUSH);
982 return $_[0] if ref($_[0]);
996 # TODO - memory leak with 5.8.0 - this isn't called until
1006 # my $class = shift ;
1008 # #local $Carp::CarpLevel = 1;
1010 # if ( ! ref $_[0] ||
1011 # ref $_[0] eq 'SCALAR' ||
1012 # #ref $_[0] eq 'CODE' ||
1013 # isaFilehandle($_[0]) )
1015 # my $inType = whatIs($_[0]);
1016 # my $outType = whatIs($_[1]);
1018 # if ($inType eq 'filename' )
1020 # croak "$class: input filename is undef or null string"
1021 # if ! defined $_[0] || $_[0] eq '' ;
1023 # if ($_[0] ne '-' && ! -e $_[0] )
1025 # ${$_[2]} = "input file '$_[0]' does not exist";
1032 # ${$_[2]} = "cannot open file '$_[0]': $!";
1037 # elsif ($inType eq 'fileglob' )
1042 # croak("$class: input and output $inType are identical")
1043 # if defined $outType && $inType eq $outType && $_[0] eq $_[1] ;
1048 # croak "$class: input parameter not a filename, filehandle, array ref or scalar ref"
1049 # unless ref $_[0] eq 'ARRAY' ;
1051 # my $array = shift @_ ;
1052 # foreach my $element ( @{ $array } )
1055 # unless validateInput($class, $element, @_);
1064 # my $self = shift ;
1066 # if ( isaFilehandle $_[0] )
1068 # return $self->_wr(@_);
1073 # if ( ref $_[0] eq 'SCALAR' )
1074 # { return $self->syswrite(@_) }
1076 # if ( ref $_[0] eq 'ARRAY' )
1079 # validateInput(*$self->{ClassName} . "::write", $_[0], *$self->{Output}, \$str, $num)
1080 # or return $self->saveErrorString(undef, $str, $num);
1082 # return $self->_wr(@_);
1085 # croak *$self->{ClassName} . "::write: input parameter not a filename, filehandle, array ref or scalar ref";
1088 # # Not a reference or a filehandle
1089 # return $self->syswrite(@_) ;
1094 # my $self = shift ;
1096 # if ( ref $_[0] && ref $_[0] eq 'SCALAR' )
1098 # return $self->syswrite(@_) ;
1101 # if ( ! ref $_[0] || isaFilehandle($_[0]))
1103 # my $item = shift @_ ;
1104 # my $isFilehandle = isaFilehandle($item) ;
1108 # if ( ! $isFilehandle )
1110 # $fh = new IO::File "<$item"
1111 # or return $self->saveErrorString(undef, "cannot open file '$item': $!", $!) ;
1117 # while (($status = read($fh, $buff, 4096)) > 0) {
1118 # $count += length $buff;
1119 # defined $self->syswrite($buff, @_)
1123 # return $self->saveErrorString(undef, $!, $!)
1127 # if ( !$isFilehandle || *$self->{AutoClose} )
1136 # #if ref $_[0] eq 'CODE' ;
1138 # # then must be ARRAY ref
1140 # my $array = shift @_ ;
1141 # foreach my $element ( @{ $array } )
1143 # my $got = $self->_wr($element, @_) ;
1146 # unless defined $got ;
1161 croak *$self->{ClassName} . "::write: not a scalar reference"
1162 unless ref $_[0] eq 'SCALAR' ;
1170 my $slen = defined $$buffer ? length($$buffer) : 0;
1173 $len = $_[1] if $_[1] < $len;
1176 $offset = $_[2] || 0;
1177 croak *$self->{ClassName} . "::write: offset outside string" if $offset > $slen;
1180 croak *$self->{ClassName} . "::write: offset outside string" if $offset < 0;
1182 my $rem = $slen - $offset;
1183 $len = $rem if $rem < $len;
1186 $buffer = \substr($$buffer, $offset, $len) ;
1189 my $buffer_length = defined $$buffer ? length($$buffer) : 0 ;
1190 *$self->{BytesWritten} += $buffer_length ;
1191 my $rest = GZIP_ISIZE_MAX - *$self->{ISize} ;
1192 if ($buffer_length > $rest) {
1193 *$self->{ISize} = $buffer_length - $rest - 1;
1196 *$self->{ISize} += $buffer_length ;
1199 # if (*$self->{Encoding}) {
1200 # $$buffer = *$self->{Encoding}->encode($$buffer);
1203 #my $length = length $$buffer;
1204 my $status = *$self->{Deflate}->deflate($buffer, *$self->{Buffer}) ;
1206 return $self->saveErrorString(undef,"Deflate Error: $status")
1207 if $self->saveStatus($status) != Z_OK ;
1209 if ( defined *$self->{FH} and length ${ *$self->{Buffer} }) {
1210 defined *$self->{FH}->write( ${ *$self->{Buffer} }, length ${ *$self->{Buffer} } )
1211 or return $self->saveErrorString(undef, $!, $!);
1212 ${ *$self->{Buffer} } = '' ;
1215 return $buffer_length;
1223 # $self = *$self{GLOB} ;
1228 defined $self->syswrite(join($,, @_) . $\);
1230 defined $self->syswrite(join("", @_) . $\);
1234 defined $self->syswrite(join($,, @_));
1236 defined $self->syswrite(join("", @_));
1245 defined $self->syswrite(sprintf($fmt, @_));
1253 my $opt = shift || Z_FINISH ;
1254 my $status = *$self->{Deflate}->flush(*$self->{Buffer}, $opt) ;
1255 return $self->saveErrorString(0,"Deflate Error: $status")
1256 if $self->saveStatus($status) != Z_OK ;
1258 if ( defined *$self->{FH} ) {
1259 *$self->{FH}->clearerr();
1260 defined *$self->{FH}->write(${ *$self->{Buffer} }, length ${ *$self->{Buffer} })
1261 or return $self->saveErrorString(0, $!, $!);
1262 ${ *$self->{Buffer} } = '' ;
1272 $self->_writeTrailer(GZIP_MINIMUM_HEADER)
1275 my $status = *$self->{Deflate}->deflateReset() ;
1276 return $self->saveErrorString(0,"Deflate Error: $status")
1277 if $self->saveStatus($status) != Z_OK ;
1279 *$self->{BytesWritten} = 0 ;
1280 *$self->{ISize} = 0 ;
1288 my $nextHeader = shift || '' ;
1290 my $status = *$self->{Deflate}->flush(*$self->{Buffer}) ;
1291 return $self->saveErrorString(0,"Deflate Error: $status")
1292 if $self->saveStatus($status) != Z_OK ;
1294 if (*$self->{OutputGzip}) {
1295 ${ *$self->{Buffer} } .= pack("V V", *$self->{Deflate}->crc32(),
1297 ${ *$self->{Buffer} } .= $nextHeader ;
1300 if (*$self->{OutputDeflate}) {
1301 ${ *$self->{Buffer} } .= pack("N", *$self->{Deflate}->adler32() );
1302 ${ *$self->{Buffer} } .= *$self->{Header} ;
1305 return 1 if ! defined *$self->{FH} ;
1307 defined *$self->{FH}->write(${ *$self->{Buffer} }, length ${ *$self->{Buffer} })
1308 or return $self->saveErrorString(0, $!, $!);
1310 ${ *$self->{Buffer} } = '' ;
1319 return 1 if *$self->{Closed} || ! *$self->{Deflate} ;
1320 *$self->{Closed} = 1 ;
1326 $self->_writeTrailer()
1332 my $status = *$self->{Deflate}->flush(*$self->{Buffer}) ;
1333 return $self->saveErrorString(0,"Deflate Error: $status")
1334 if $self->saveStatus($status) != Z_OK ;
1336 if (*$self->{OutputGzip}) {
1337 ${ *$self->{Buffer} } .= pack("V V", *$self->{Deflate}->crc32(),
1341 if (*$self->{OutputDeflate}) {
1342 ${ *$self->{Buffer} } .= pack("N", *$self->{Deflate}->adler32() );
1346 return 1 if ! defined *$self->{FH} ;
1348 defined *$self->{FH}->write(${ *$self->{Buffer} }, length( ${ *$self->{Buffer} } ))
1349 or return $self->saveErrorString(0, $!, $!);
1351 ${ *$self->{Buffer} } = '' ;
1354 if (defined *$self->{FH}) {
1355 #if (! *$self->{Handle} || *$self->{AutoClose}) {
1356 if ((! *$self->{Handle} || *$self->{AutoClose}) && ! *$self->{StdIO}) {
1358 *$self->{FH}->close()
1359 or return $self->saveErrorString(0, $!, $!);
1361 delete *$self->{FH} ;
1362 # This delete can set $! in older Perls, so reset the errno
1373 my $strategy = shift ;
1375 my $status = *$self->{Deflate}->deflateParams(-Level => $level,
1376 -Strategy => $strategy) ;
1377 return $self->saveErrorString(0,"deflateParams Error: $status")
1378 if $self->saveStatus($status) != Z_OK ;
1390 # my $self = shift ;
1391 # return *$self->{Deflate}->crc32() ;
1396 # my $self = shift ;
1397 # return *$self->{Deflate}->msg() ;
1402 # my $self = shift ;
1403 # return *$self->{Deflate}->dict_adler() ;
1408 # my $self = shift ;
1409 # return *$self->{Deflate}->get_Level() ;
1414 # my $self = shift ;
1415 # return *$self->{Deflate}->get_Strategy() ;
1423 #return *$self->{Deflate}->total_in();
1424 return *$self->{BytesWritten} ;
1431 return *$self->{Closed} ;
1438 my $position = shift;
1439 my $whence = shift ;
1441 my $here = $self->tell() ;
1444 #use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END);
1447 if ($whence == IO::Handle::SEEK_SET) {
1448 $target = $position ;
1450 elsif ($whence == IO::Handle::SEEK_CUR || $whence == IO::Handle::SEEK_END) {
1451 $target = $here + $position ;
1454 croak *$self->{ClassName} . "::seek: unknown value, $whence, for whence parameter";
1457 # short circuit if seeking to current offset
1458 return 1 if $target == $here ;
1460 # Outlaw any attempt to seek backwards
1461 croak *$self->{ClassName} . "::seek: cannot seek backwards"
1462 if $target < $here ;
1464 # Walk the file to the new offset
1465 my $offset = $target - $here ;
1468 defined $self->syswrite("\x00" x $offset)
1477 # my $self = shift ;
1478 # return defined *$self->{FH}
1479 # ? binmode *$self->{FH}
1486 return defined *$self->{FH}
1487 ? *$self->{FH}->fileno()
1494 return sub { croak "$name Not Available: File opened only for output" ; } ;
1497 *read = _notAvailable('read');
1498 *READ = _notAvailable('read');
1499 *readline = _notAvailable('readline');
1500 *READLINE = _notAvailable('readline');
1501 *getc = _notAvailable('getc');
1502 *GETC = _notAvailable('getc');
1507 *WRITE = \&syswrite;
1508 *write = \&syswrite;
1513 *BINMODE = \&binmode;
1515 #*sysread = \&_notAvailable;
1516 #*syswrite = \&_write;
1524 IO::Compress::Gzip - Perl interface to write RFC 1952 files/buffers
1528 use IO::Compress::Gzip qw(gzip $GzipError) ;
1531 my $status = gzip $input => $output [,OPTS]
1532 or die "gzip failed: $GzipError\n";
1534 my $z = new IO::Compress::Gzip $output [,OPTS]
1535 or die "gzip failed: $GzipError\n";
1538 $z->printf($format, $string);
1540 $z->syswrite($string [, $length, $offset]);
1544 $z->seek($position, $whence);
1548 $z->deflateParams();
1556 printf $z $format, $string;
1557 syswrite $z, $string [, $length, $offset];
1561 seek $z, $position, $whence
1571 B<WARNING -- This is a Beta release>.
1575 =item * DO NOT use in production code.
1577 =item * The documentation is incomplete in places.
1579 =item * Parts of the interface defined here are tentative.
1581 =item * Please report any problems you find.
1587 This module provides a Perl interface that allows writing compressed
1588 data to files or buffer as defined in RFC 1952.
1591 All the gzip headers defined in RFC 1952 can be created using
1597 For reading RFC 1952 files/buffers, see the companion module
1598 L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>.
1601 =head1 Functional Interface
1603 A top-level function, C<gzip>, is provided to carry out "one-shot"
1604 compression between buffers and/or files. For finer control over the compression process, see the L</"OO Interface"> section.
1606 use IO::Compress::Gzip qw(gzip $GzipError) ;
1608 gzip $input => $output [,OPTS]
1609 or die "gzip failed: $GzipError\n";
1612 or die "gzip failed: $GzipError\n";
1614 The functional interface needs Perl5.005 or better.
1617 =head2 gzip $input => $output [, OPTS]
1619 If the first parameter is not a hash reference C<gzip> expects
1620 at least two parameters, C<$input> and C<$output>.
1622 =head3 The C<$input> parameter
1624 The parameter, C<$input>, is used to define the source of
1625 the uncompressed data.
1627 It can take one of the following forms:
1633 If the C<$input> parameter is a simple scalar, it is assumed to be a
1634 filename. This file will be opened for reading and the input data
1635 will be read from it.
1639 If the C<$input> parameter is a filehandle, the input data will be
1641 The string '-' can be used as an alias for standard input.
1643 =item A scalar reference
1645 If C<$input> is a scalar reference, the input data will be read
1648 =item An array reference
1650 If C<$input> is an array reference, the input data will be read from each
1651 element of the array in turn. The action taken by C<gzip> with
1652 each element of the array will depend on the type of data stored
1653 in it. You can mix and match any of the types defined in this list,
1654 excluding other array or hash references.
1655 The complete array will be walked to ensure that it only
1656 contains valid data types before any data is compressed.
1658 =item An Input FileGlob string
1660 If C<$input> is a string that is delimited by the characters "<" and ">"
1661 C<gzip> will assume that it is an I<input fileglob string>. The
1662 input is the list of files that match the fileglob.
1664 If the fileglob does not match any files ...
1666 See L<File::GlobMapper|File::GlobMapper> for more details.
1671 If the C<$input> parameter is any other type, C<undef> will be returned.
1675 In addition, if C<$input> is a simple filename, the default values for
1676 two of the gzip header fields created by this function will be sourced
1677 from that file -- the NAME gzip header field will be populated with
1678 the filename itself, and the MTIME header field will be set to the
1679 modification time of the file.
1680 The intention here is to mirror part of the behavior of the gzip
1682 If you do not want to use these defaults they can be overridden by
1683 explicitly setting the C<Name> and C<Time> options.
1687 =head3 The C<$output> parameter
1689 The parameter C<$output> is used to control the destination of the
1690 compressed data. This parameter can take one of these forms.
1696 If the C<$output> parameter is a simple scalar, it is assumed to be a filename.
1697 This file will be opened for writing and the compressed data will be
1702 If the C<$output> parameter is a filehandle, the compressed data will
1704 The string '-' can be used as an alias for standard output.
1707 =item A scalar reference
1709 If C<$output> is a scalar reference, the compressed data will be stored
1713 =item A Hash Reference
1715 If C<$output> is a hash reference, the compressed data will be written
1716 to C<$output{$input}> as a scalar reference.
1718 When C<$output> is a hash reference, C<$input> must be either a filename or
1719 list of filenames. Anything else is an error.
1722 =item An Array Reference
1724 If C<$output> is an array reference, the compressed data will be pushed
1727 =item An Output FileGlob
1729 If C<$output> is a string that is delimited by the characters "<" and ">"
1730 C<gzip> will assume that it is an I<output fileglob string>. The
1731 output is the list of files that match the fileglob.
1733 When C<$output> is an fileglob string, C<$input> must also be a fileglob
1734 string. Anything else is an error.
1738 If the C<$output> parameter is any other type, C<undef> will be returned.
1740 =head2 gzip \%hash [, OPTS]
1742 If the first parameter is a hash reference, C<\%hash>, this will be used to
1743 define both the source of uncompressed data and to control where the
1744 compressed data is output. Each key/value pair in the hash defines a
1745 mapping between an input filename, stored in the key, and an output
1746 file/buffer, stored in the value. Although the input can only be a filename,
1747 there is more flexibility to control the destination of the compressed
1748 data. This is determined by the type of the value. Valid types are
1754 If the value is C<undef> the compressed data will be written to the
1755 value as a scalar reference.
1759 If the value is a simple scalar, it is assumed to be a filename. This file will
1760 be opened for writing and the compressed data will be written to it.
1764 If the value is a filehandle, the compressed data will be
1766 The string '-' can be used as an alias for standard output.
1769 =item A scalar reference
1771 If the value is a scalar reference, the compressed data will be stored
1772 in the buffer that is referenced by the scalar.
1775 =item A Hash Reference
1777 If the value is a hash reference, the compressed data will be written
1778 to C<$hash{$input}> as a scalar reference.
1780 =item An Array Reference
1782 If C<$output> is an array reference, the compressed data will be pushed
1787 Any other type is a error.
1791 When C<$input> maps to multiple files/buffers and C<$output> is a single
1792 file/buffer the compressed input files/buffers will all be stored in
1793 C<$output> as a single compressed stream.
1797 =head2 Optional Parameters
1799 Unless specified below, the optional parameters for C<gzip>,
1800 C<OPTS>, are the same as those used with the OO interface defined in the
1801 L</"Constructor Options"> section below.
1805 =item AutoClose =E<gt> 0|1
1807 This option applies to any input or output data streams to C<gzip>
1808 that are filehandles.
1810 If C<AutoClose> is specified, and the value is true, it will result in all
1811 input and/or output filehandles being closed once C<gzip> has
1814 This parameter defaults to 0.
1818 =item -Append =E<gt> 0|1
1829 To read the contents of the file C<file1.txt> and write the compressed
1830 data to the file C<file1.txt.gz>.
1834 use IO::Compress::Gzip qw(gzip $GzipError) ;
1836 my $input = "file1.txt";
1837 gzip $input => "$input.gz"
1838 or die "gzip failed: $GzipError\n";
1841 To read from an existing Perl filehandle, C<$input>, and write the
1842 compressed data to a buffer, C<$buffer>.
1846 use IO::Compress::Gzip qw(gzip $GzipError) ;
1849 my $input = new IO::File "<file1.txt"
1850 or die "Cannot open 'file1.txt': $!\n" ;
1852 gzip $input => \$buffer
1853 or die "gzip failed: $GzipError\n";
1855 To compress all files in the directory "/my/home" that match "*.txt"
1856 and store the compressed data in the same directory
1860 use IO::Compress::Gzip qw(gzip $GzipError) ;
1862 gzip '</my/home/*.txt>' => '<*.gz>'
1863 or die "gzip failed: $GzipError\n";
1865 and if you want to compress each file one at a time, this will do the trick
1869 use IO::Compress::Gzip qw(gzip $GzipError) ;
1871 for my $input ( glob "/my/home/*.txt" )
1873 my $output = "$input.gz" ;
1874 gzip $input => $output
1875 or die "Error compressing '$input': $GzipError\n";
1883 The format of the constructor for C<IO::Compress::Gzip> is shown below
1885 my $z = new IO::Compress::Gzip $output [,OPTS]
1886 or die "IO::Compress::Gzip failed: $GzipError\n";
1888 It returns an C<IO::Compress::Gzip> object on success and undef on failure.
1889 The variable C<$GzipError> will contain an error message on failure.
1891 If you are running Perl 5.005 or better the object, C<$z>, returned from
1892 IO::Compress::Gzip can be used exactly like an L<IO::File|IO::File> filehandle.
1893 This means that all normal output file operations can be carried out
1895 For example, to write to a compressed file/buffer you can use either of
1898 $z->print("hello world\n");
1899 print $z "hello world\n";
1901 The mandatory parameter C<$output> is used to control the destination
1902 of the compressed data. This parameter can take one of these forms.
1908 If the C<$output> parameter is a simple scalar, it is assumed to be a
1909 filename. This file will be opened for writing and the compressed data
1910 will be written to it.
1914 If the C<$output> parameter is a filehandle, the compressed data will be
1916 The string '-' can be used as an alias for standard output.
1919 =item A scalar reference
1921 If C<$output> is a scalar reference, the compressed data will be stored
1926 If the C<$output> parameter is any other type, C<IO::Compress::Gzip>::new will
1929 =head2 Constructor Options
1931 C<OPTS> is any combination of the following options:
1935 =item -AutoClose =E<gt> 0|1
1937 This option is only valid when the C<$output> parameter is a filehandle. If
1938 specified, and the value is true, it will result in the C<$output> being closed
1939 once either the C<close> method is called or the C<IO::Compress::Gzip> object is
1942 This parameter defaults to 0.
1944 =item -Append =E<gt> 0|1
1946 Opens C<$output> in append mode.
1948 The behaviour of this option is dependant on the type of C<$output>.
1954 If C<$output> is a buffer and C<Append> is enabled, all compressed data will be
1955 append to the end if C<$output>. Otherwise C<$output> will be cleared before
1956 any data is written to it.
1960 If C<$output> is a filename and C<Append> is enabled, the file will be opened
1961 in append mode. Otherwise the contents of the file, if any, will be truncated
1962 before any compressed data is written to it.
1964 =item * A Filehandle
1966 If C<$output> is a filehandle, the file pointer will be positioned to the end
1967 of the file via a call to C<seek> before any compressed data is written to it.
1968 Otherwise the file pointer will not be moved.
1972 This parameter defaults to 0.
1974 =item -Merge =E<gt> 0|1
1976 This option is used to compress input data and append it to an existing
1977 compressed data stream in C<$output>. The end result is a single compressed
1978 data stream stored in C<$output>.
1982 It is a fatal error to attempt to use this option when C<$output> is not an RFC
1987 There are a number of other limitations with the C<Merge> option:
1993 This module needs to have been built with zlib 1.2.1 or better to work. A fatal
1994 error will be thrown if C<Merge> is used with an older version of zlib.
1998 If C<$output> is a file or a filehandle, it must be seekable.
2003 This parameter defaults to 0.
2007 Defines the compression level used by zlib. The value should either be
2008 a number between 0 and 9 (0 means no compression and 9 is maximum
2009 compression), or one of the symbolic constants defined below.
2014 Z_DEFAULT_COMPRESSION
2016 The default is Z_DEFAULT_COMPRESSION.
2018 Note, these constants are not imported by C<IO::Compress::Gzip> by default.
2020 use IO::Compress::Gzip qw(:strategy);
2021 use IO::Compress::Gzip qw(:constants);
2022 use IO::Compress::Gzip qw(:all);
2026 Defines the strategy used to tune the compression. Use one of the symbolic
2027 constants defined below.
2035 The default is Z_DEFAULT_STRATEGY.
2041 =item -Mimimal =E<gt> 0|1
2043 If specified, this option will force the creation of the smallest possible
2044 compliant gzip header (which is exactly 10 bytes long) as defined in
2047 See the section titled "Compliance" in RFC 1952 for a definition
2048 of the values used for the fields in the gzip header.
2050 All other parameters that control the content of the gzip header will
2051 be ignored if this parameter is set to 1.
2053 This parameter defaults to 0.
2055 =item -Comment =E<gt> $comment
2057 Stores the contents of C<$comment> in the COMMENT field in
2059 By default, no comment field is written to the gzip file.
2061 If the C<-Strict> option is enabled, the comment can only consist of ISO
2062 8859-1 characters plus line feed.
2064 If the C<-Strict> option is disabled, the comment field can contain any
2065 character except NULL. If any null characters are present, the field
2066 will be truncated at the first NULL.
2068 =item -Name =E<gt> $string
2070 Stores the contents of C<$string> in the gzip NAME header field. If
2071 C<Name> is not specified, no gzip NAME field will be created.
2073 If the C<-Strict> option is enabled, C<$string> can only consist of ISO
2076 If C<-Strict> is disabled, then C<$string> can contain any character
2077 except NULL. If any null characters are present, the field will be
2078 truncated at the first NULL.
2080 =item -Time =E<gt> $number
2082 Sets the MTIME field in the gzip header to $number.
2084 This field defaults to the time the C<IO::Compress::Gzip> object was created
2085 if this option is not specified.
2087 =item -TextFlag =E<gt> 0|1
2089 This parameter controls the setting of the FLG.FTEXT bit in the gzip header. It
2090 is used to signal that the data stored in the gzip file/buffer is probably
2095 =item -HeaderCRC =E<gt> 0|1
2097 When true this parameter will set the FLG.FHCRC bit to 1 in the gzip header and
2098 set the CRC16 header field to the CRC of the complete gzip header except the
2101 B<Note> that gzip files created with the C<HeaderCRC> flag set to 1 cannot be
2102 read by most, if not all, of the the standard gunzip utilities, most notably
2103 gzip version 1.2.4. You should therefore avoid using this option if you want to
2104 maximise the portability of your gzip files.
2106 This parameter defaults to 0.
2108 =item -OS_Code =E<gt> $value
2110 Stores C<$value> in the gzip OS header field. A number between 0 and
2113 If not specified, this parameter defaults to the OS code of the Operating
2114 System this module was built on. The value 3 is used as a catch-all for all
2115 Unix variants and unknown Operating Systems.
2117 =item -ExtraField =E<gt> $data
2119 This parameter allows additional metadata to be stored in the ExtraField in the
2120 gzip header. An RFC1952 compliant ExtraField consists of zero or more
2121 subfields. Each subfield consists of a two byte header followed by the subfield
2124 The list of subfields can be supplied in any of the following formats
2126 -ExtraField => [$id1, $data1,
2130 -ExtraField => [ [$id1 => $data1],
2134 -ExtraField => { $id1 => $data1,
2139 Where C<$id1>, C<$id2> are two byte subfield ID's. The second byte of
2140 the ID cannot be 0, unless the C<Strict> option has been disabled.
2142 If you use the hash syntax, you have no control over the order in which
2143 the ExtraSubFields are stored, plus you cannot have SubFields with
2146 Alternatively the list of subfields can by supplied as a scalar, thus
2148 -ExtraField => $rawdata
2150 If you use the raw format, and the C<Strict> option is enabled,
2151 C<IO::Compress::Gzip> will check that C<$rawdata> consists of zero or more
2152 conformant sub-fields. When C<Strict> is disabled, C<$rawdata> can
2153 consist of any arbitrary byte stream.
2155 The maximum size of the Extra Field 65535 bytes.
2157 =item -ExtraFlags =E<gt> $value
2159 Sets the XFL byte in the gzip header to C<$value>.
2161 If this option is not present, the value stored in XFL field will be determined
2162 by the setting of the C<Level> option.
2164 If C<Level =E<gt> Z_BEST_SPEED> has been specified then XFL is set to 2.
2165 If C<Level =E<gt> Z_BEST_COMPRESSION> has been specified then XFL is set to 4.
2166 Otherwise XFL is set to 0.
2170 =item -Strict =E<gt> 0|1
2174 C<Strict> will optionally police the values supplied with other options
2175 to ensure they are compliant with RFC1952.
2177 This option is enabled by default.
2179 If C<Strict> is enabled the following behavior will be policed:
2185 The value supplied with the C<Name> option can only contain ISO 8859-1
2190 The value supplied with the C<Comment> option can only contain ISO 8859-1
2191 characters plus line-feed.
2195 The values supplied with the C<-Name> and C<-Comment> options cannot
2196 contain multiple embedded nulls.
2200 If an C<ExtraField> option is specified and it is a simple scalar,
2201 it must conform to the sub-field structure as defined in RFC1952.
2205 If an C<ExtraField> option is specified the second byte of the ID will be
2206 checked in each subfield to ensure that it does not contain the reserved
2211 When C<Strict> is disabled the following behavior will be policed:
2217 The value supplied with C<-Name> option can contain
2218 any character except NULL.
2222 The value supplied with C<-Comment> option can contain any character
2227 The values supplied with the C<-Name> and C<-Comment> options can contain
2228 multiple embedded nulls. The string written to the gzip header will
2229 consist of the characters up to, but not including, the first embedded
2234 If an C<ExtraField> option is specified and it is a simple scalar, the
2235 structure will not be checked. The only error is if the length is too big.
2239 The ID header in an C<ExtraField> sub-field can consist of any two bytes.
2260 Compresses and outputs the contents of the C<$data> parameter. This
2261 has the same behavior as the C<print> built-in.
2263 Returns true if successful.
2269 $z->printf($format, $data)
2270 printf $z $format, $data
2272 Compresses and outputs the contents of the C<$data> parameter.
2274 Returns true if successful.
2281 $z->syswrite $data, $length
2282 $z->syswrite $data, $length, $offset
2285 syswrite $z, $data, $length
2286 syswrite $z, $data, $length, $offset
2288 Compresses and outputs the contents of the C<$data> parameter.
2290 Returns the number of uncompressed bytes written, or C<undef> if
2298 $z->write $data, $length
2299 $z->write $data, $length, $offset
2301 Compresses and outputs the contents of the C<$data> parameter.
2303 Returns the number of uncompressed bytes written, or C<undef> if
2311 $z->flush($flush_type);
2313 flush $z $flush_type;
2315 Flushes any pending compressed data to the output file/buffer.
2317 This method takes an optional parameter, C<$flush_type>, that controls
2318 how the flushing will be carried out. By default the C<$flush_type>
2319 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
2320 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
2321 strongly recommended that you only set the C<flush_type> parameter if
2322 you fully understand the implications of what it does - overuse of C<flush>
2323 can seriously degrade the level of compression achieved. See the C<zlib>
2324 documentation for details.
2326 Returns true on success.
2336 Returns the uncompressed file offset.
2347 Returns true if the C<close> method has been called.
2353 $z->seek($position, $whence);
2354 seek($z, $position, $whence);
2359 Provides a sub-set of the C<seek> functionality, with the restriction
2360 that it is only legal to seek forward in the output file/buffer.
2361 It is a fatal error to attempt to seek backward.
2363 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
2367 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
2368 SEEK_CUR or SEEK_END.
2370 Returns 1 on success, 0 on failure.
2379 This is a noop provided for completeness.
2386 If the C<$z> object is associated with a file, this method will return
2387 the underlying filehandle.
2389 If the C<$z> object is is associated with a buffer, this method will
2399 Flushes any pending compressed data and then closes the output file/buffer.
2403 For most versions of Perl this method will be automatically invoked if
2404 the IO::Compress::Gzip object is destroyed (either explicitly or by the
2405 variable with the reference to the object going out of scope). The
2406 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
2407 these cases, the C<close> method will be called automatically, but
2408 not until global destruction of all live objects when the program is
2411 Therefore, if you want your scripts to be able to run on all versions
2412 of Perl, you should call C<close> explicitly and not rely on automatic
2415 Returns true on success, otherwise 0.
2417 If the C<AutoClose> option has been enabled when the IO::Compress::Gzip
2418 object was created, and the object is associated with a file, the
2419 underlying file will also be closed.
2432 =head2 deflateParams
2442 A number of symbolic constants are required by some methods in
2443 C<IO::Compress::Gzip>. None are imported by default.
2449 Imports C<gzip>, C<$GzipError> and all symbolic
2450 constants that can be used by C<IO::Compress::Gzip>. Same as doing this
2452 use IO::Compress::Gzip qw(gzip $GzipError :constants) ;
2456 Import all symbolic constants. Same as doing this
2458 use IO::Compress::Gzip qw(:flush :level :strategy) ;
2462 These symbolic constants are used by the C<flush> method.
2474 These symbolic constants are used by the C<Level> option in the constructor.
2479 Z_DEFAULT_COMPRESSION
2484 These symbolic constants are used by the C<Strategy> option in the constructor.
2507 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>
2509 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
2511 L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
2512 L<IO::Zlib|IO::Zlib>
2514 For RFC 1950, 1951 and 1952 see
2515 F<http://www.faqs.org/rfcs/rfc1950.html>,
2516 F<http://www.faqs.org/rfcs/rfc1951.html> and
2517 F<http://www.faqs.org/rfcs/rfc1952.html>
2519 The primary site for the gzip program is F<http://www.gzip.org>.
2523 The I<IO::Compress::Gzip> module was written by Paul Marquess,
2524 F<pmqs@cpan.org>. The latest copy of the module can be
2525 found on CPAN in F<modules/by-module/Compress/Compress-Zlib-x.x.tar.gz>.
2527 The I<zlib> compression library was written by Jean-loup Gailly
2528 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
2530 The primary site for the I<zlib> compression library is
2531 F<http://www.zlib.org>.
2533 =head1 MODIFICATION HISTORY
2535 See the Changes file.
2537 =head1 COPYRIGHT AND LICENSE
2540 Copyright (c) 2005 Paul Marquess. All rights reserved.
2541 This program is free software; you can redistribute it and/or
2542 modify it under the same terms as Perl itself.