1 2package IO::Uncompress::Gunzip ; 3 4require 5.006 ; 5 6# for RFC1952 7 8use strict ; 9use warnings; 10use bytes; 11 12use IO::Uncompress::RawInflate 2.212 ; 13 14use Compress::Raw::Zlib 2.212 () ; 15use IO::Compress::Base::Common 2.212 qw(:Status ); 16use IO::Compress::Gzip::Constants 2.212 ; 17use IO::Compress::Zlib::Extra 2.212 ; 18 19require Exporter ; 20 21our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $GunzipError); 22 23@ISA = qw(IO::Uncompress::RawInflate Exporter); 24@EXPORT_OK = qw( $GunzipError gunzip ); 25%EXPORT_TAGS = %IO::Uncompress::RawInflate::DEFLATE_CONSTANTS ; 26push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; 27Exporter::export_ok_tags('all'); 28 29$GunzipError = ''; 30 31$VERSION = '2.212'; 32 33sub new 34{ 35 my $class = shift ; 36 $GunzipError = ''; 37 my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$GunzipError); 38 39 $obj->_create(undef, 0, @_); 40} 41 42sub gunzip 43{ 44 my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$GunzipError); 45 return $obj->_inf(@_) ; 46} 47 48sub getExtraParams 49{ 50 return ( 'parseextra' => [IO::Compress::Base::Common::Parse_boolean, 0] ) ; 51} 52 53sub ckParams 54{ 55 my $self = shift ; 56 my $got = shift ; 57 58 # gunzip always needs crc32 59 $got->setValue('crc32' => 1); 60 61 return 1; 62} 63 64sub ckMagic 65{ 66 my $self = shift; 67 68 my $magic ; 69 $self->smartReadExact(\$magic, GZIP_ID_SIZE); 70 71 *$self->{HeaderPending} = $magic ; 72 73 return $self->HeaderError("Minimum header size is " . 74 GZIP_MIN_HEADER_SIZE . " bytes") 75 if length $magic != GZIP_ID_SIZE ; 76 77 return $self->HeaderError("Bad Magic") 78 if ! isGzipMagic($magic) ; 79 80 *$self->{Type} = 'rfc1952'; 81 82 return $magic ; 83} 84 85sub readHeader 86{ 87 my $self = shift; 88 my $magic = shift; 89 90 return $self->_readGzipHeader($magic); 91} 92 93sub chkTrailer 94{ 95 my $self = shift; 96 my $trailer = shift; 97 98 # Check CRC & ISIZE 99 my ($CRC32, $ISIZE) = unpack("V V", $trailer) ; 100 *$self->{Info}{CRC32} = $CRC32; 101 *$self->{Info}{ISIZE} = $ISIZE; 102 103 if (*$self->{Strict}) { 104 return $self->TrailerError("CRC mismatch") 105 if $CRC32 != *$self->{Uncomp}->crc32() ; 106 107 my $exp_isize = *$self->{UnCompSize}->get32bit(); 108 return $self->TrailerError("ISIZE mismatch. Got $ISIZE" 109 . ", expected $exp_isize") 110 if $ISIZE != $exp_isize ; 111 } 112 113 return STATUS_OK; 114} 115 116sub isGzipMagic 117{ 118 my $buffer = shift ; 119 return 0 if length $buffer < GZIP_ID_SIZE ; 120 my ($id1, $id2) = unpack("C C", $buffer) ; 121 return $id1 == GZIP_ID1 && $id2 == GZIP_ID2 ; 122} 123 124sub _readFullGzipHeader($) 125{ 126 my ($self) = @_ ; 127 my $magic = '' ; 128 129 $self->smartReadExact(\$magic, GZIP_ID_SIZE); 130 131 *$self->{HeaderPending} = $magic ; 132 133 return $self->HeaderError("Minimum header size is " . 134 GZIP_MIN_HEADER_SIZE . " bytes") 135 if length $magic != GZIP_ID_SIZE ; 136 137 138 return $self->HeaderError("Bad Magic") 139 if ! isGzipMagic($magic) ; 140 141 my $status = $self->_readGzipHeader($magic); 142 delete *$self->{Transparent} if ! defined $status ; 143 return $status ; 144} 145 146sub _readGzipHeader($) 147{ 148 my ($self, $magic) = @_ ; 149 my ($HeaderCRC) ; 150 my ($buffer) = '' ; 151 152 $self->smartReadExact(\$buffer, GZIP_MIN_HEADER_SIZE - GZIP_ID_SIZE) 153 or return $self->HeaderError("Minimum header size is " . 154 GZIP_MIN_HEADER_SIZE . " bytes") ; 155 156 my $keep = $magic . $buffer ; 157 *$self->{HeaderPending} = $keep ; 158 159 # now split out the various parts 160 my ($cm, $flag, $mtime, $xfl, $os) = unpack("C C V C C", $buffer) ; 161 162 $cm == GZIP_CM_DEFLATED 163 or return $self->HeaderError("Not Deflate (CM is $cm)") ; 164 165 # check for use of reserved bits 166 return $self->HeaderError("Use of Reserved Bits in FLG field.") 167 if $flag & GZIP_FLG_RESERVED ; 168 169 my $EXTRA ; 170 my @EXTRA = () ; 171 if ($flag & GZIP_FLG_FEXTRA) { 172 $EXTRA = "" ; 173 $self->smartReadExact(\$buffer, GZIP_FEXTRA_HEADER_SIZE) 174 or return $self->TruncatedHeader("FEXTRA Length") ; 175 176 my ($XLEN) = unpack("v", $buffer) ; 177 $self->smartReadExact(\$EXTRA, $XLEN) 178 or return $self->TruncatedHeader("FEXTRA Body"); 179 $keep .= $buffer . $EXTRA ; 180 181 if ($XLEN && *$self->{'ParseExtra'}) { 182 my $bad = IO::Compress::Zlib::Extra::parseRawExtra($EXTRA, 183 \@EXTRA, 1, 1); 184 return $self->HeaderError($bad) 185 if defined $bad; 186 } 187 } 188 189 my $origname ; 190 if ($flag & GZIP_FLG_FNAME) { 191 $origname = "" ; 192 while (1) { 193 $self->smartReadExact(\$buffer, 1) 194 or return $self->TruncatedHeader("FNAME"); 195 last if $buffer eq GZIP_NULL_BYTE ; 196 $origname .= $buffer 197 } 198 $keep .= $origname . GZIP_NULL_BYTE ; 199 200 return $self->HeaderError("Non ISO 8859-1 Character found in Name") 201 if *$self->{Strict} && $origname =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ; 202 } 203 204 my $comment ; 205 if ($flag & GZIP_FLG_FCOMMENT) { 206 $comment = ""; 207 while (1) { 208 $self->smartReadExact(\$buffer, 1) 209 or return $self->TruncatedHeader("FCOMMENT"); 210 last if $buffer eq GZIP_NULL_BYTE ; 211 $comment .= $buffer 212 } 213 $keep .= $comment . GZIP_NULL_BYTE ; 214 215 return $self->HeaderError("Non ISO 8859-1 Character found in Comment") 216 if *$self->{Strict} && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o ; 217 } 218 219 if ($flag & GZIP_FLG_FHCRC) { 220 $self->smartReadExact(\$buffer, GZIP_FHCRC_SIZE) 221 or return $self->TruncatedHeader("FHCRC"); 222 223 $HeaderCRC = unpack("v", $buffer) ; 224 my $crc16 = Compress::Raw::Zlib::crc32($keep) & 0xFF ; 225 226 return $self->HeaderError("CRC16 mismatch.") 227 if *$self->{Strict} && $crc16 != $HeaderCRC; 228 229 $keep .= $buffer ; 230 } 231 232 # Assume compression method is deflated for xfl tests 233 #if ($xfl) { 234 #} 235 236 *$self->{Type} = 'rfc1952'; 237 238 return { 239 'Type' => 'rfc1952', 240 'FingerprintLength' => 2, 241 'HeaderLength' => length $keep, 242 'TrailerLength' => GZIP_TRAILER_SIZE, 243 'Header' => $keep, 244 'isMinimalHeader' => $keep eq GZIP_MINIMUM_HEADER ? 1 : 0, 245 246 'MethodID' => $cm, 247 'MethodName' => $cm == GZIP_CM_DEFLATED ? "Deflated" : "Unknown" , 248 'TextFlag' => $flag & GZIP_FLG_FTEXT ? 1 : 0, 249 'HeaderCRCFlag' => $flag & GZIP_FLG_FHCRC ? 1 : 0, 250 'NameFlag' => $flag & GZIP_FLG_FNAME ? 1 : 0, 251 'CommentFlag' => $flag & GZIP_FLG_FCOMMENT ? 1 : 0, 252 'ExtraFlag' => $flag & GZIP_FLG_FEXTRA ? 1 : 0, 253 'Name' => $origname, 254 'Comment' => $comment, 255 'Time' => $mtime, 256 'OsID' => $os, 257 'OsName' => defined $GZIP_OS_Names{$os} 258 ? $GZIP_OS_Names{$os} : "Unknown", 259 'HeaderCRC' => $HeaderCRC, 260 'Flags' => $flag, 261 'ExtraFlags' => $xfl, 262 'ExtraFieldRaw' => $EXTRA, 263 'ExtraField' => [ @EXTRA ], 264 265 266 #'CompSize'=> $compsize, 267 #'CRC32'=> $CRC32, 268 #'OrigSize'=> $ISIZE, 269 } 270} 271 272 2731; 274 275__END__ 276 277 278=head1 NAME 279 280IO::Uncompress::Gunzip - Read RFC 1952 files/buffers 281 282=head1 SYNOPSIS 283 284 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 285 286 my $status = gunzip $input => $output [,OPTS] 287 or die "gunzip failed: $GunzipError\n"; 288 289 my $z = IO::Uncompress::Gunzip->new( $input [OPTS] ) 290 or die "gunzip failed: $GunzipError\n"; 291 292 $status = $z->read($buffer) 293 $status = $z->read($buffer, $length) 294 $status = $z->read($buffer, $length, $offset) 295 $line = $z->getline() 296 $char = $z->getc() 297 $char = $z->ungetc() 298 $char = $z->opened() 299 300 $status = $z->inflateSync() 301 302 $data = $z->trailingData() 303 $status = $z->nextStream() 304 $data = $z->getHeaderInfo() 305 $z->tell() 306 $z->seek($position, $whence) 307 $z->binmode() 308 $z->fileno() 309 $z->eof() 310 $z->close() 311 312 $GunzipError ; 313 314 # IO::File mode 315 316 <$z> 317 read($z, $buffer); 318 read($z, $buffer, $length); 319 read($z, $buffer, $length, $offset); 320 tell($z) 321 seek($z, $position, $whence) 322 binmode($z) 323 fileno($z) 324 eof($z) 325 close($z) 326 327=head1 DESCRIPTION 328 329This module provides a Perl interface that allows the reading of 330files/buffers that conform to RFC 1952. 331 332For writing RFC 1952 files/buffers, see the companion module IO::Compress::Gzip. 333 334=head1 Functional Interface 335 336A top-level function, C<gunzip>, is provided to carry out 337"one-shot" uncompression between buffers and/or files. For finer 338control over the uncompression process, see the L</"OO Interface"> 339section. 340 341 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 342 343 gunzip $input_filename_or_reference => $output_filename_or_reference [,OPTS] 344 or die "gunzip failed: $GunzipError\n"; 345 346The functional interface needs Perl5.005 or better. 347 348=head2 gunzip $input_filename_or_reference => $output_filename_or_reference [, OPTS] 349 350C<gunzip> expects at least two parameters, 351C<$input_filename_or_reference> and C<$output_filename_or_reference> 352and zero or more optional parameters (see L</Optional Parameters>) 353 354=head3 The C<$input_filename_or_reference> parameter 355 356The parameter, C<$input_filename_or_reference>, is used to define the 357source of the compressed data. 358 359It can take one of the following forms: 360 361=over 5 362 363=item A filename 364 365If the C<$input_filename_or_reference> parameter is a simple scalar, it is 366assumed to be a filename. This file will be opened for reading and the 367input data will be read from it. 368 369=item A filehandle 370 371If the C<$input_filename_or_reference> parameter is a filehandle, the input 372data will be read from it. The string '-' can be used as an alias for 373standard input. 374 375=item A scalar reference 376 377If C<$input_filename_or_reference> is a scalar reference, the input data 378will be read from C<$$input_filename_or_reference>. 379 380=item An array reference 381 382If C<$input_filename_or_reference> is an array reference, each element in 383the array must be a filename. 384 385The input data will be read from each file in turn. 386 387The complete array will be walked to ensure that it only 388contains valid filenames before any data is uncompressed. 389 390=item An Input FileGlob string 391 392If C<$input_filename_or_reference> is a string that is delimited by the 393characters "<" and ">" C<gunzip> will assume that it is an 394I<input fileglob string>. The input is the list of files that match the 395fileglob. 396 397See L<File::GlobMapper|File::GlobMapper> for more details. 398 399=back 400 401If the C<$input_filename_or_reference> parameter is any other type, 402C<undef> will be returned. 403 404=head3 The C<$output_filename_or_reference> parameter 405 406The parameter C<$output_filename_or_reference> is used to control the 407destination of the uncompressed data. This parameter can take one of 408these forms. 409 410=over 5 411 412=item A filename 413 414If the C<$output_filename_or_reference> parameter is a simple scalar, it is 415assumed to be a filename. This file will be opened for writing and the 416uncompressed data will be written to it. 417 418=item A filehandle 419 420If the C<$output_filename_or_reference> parameter is a filehandle, the 421uncompressed data will be written to it. The string '-' can be used as 422an alias for standard output. 423 424=item A scalar reference 425 426If C<$output_filename_or_reference> is a scalar reference, the 427uncompressed data will be stored in C<$$output_filename_or_reference>. 428 429=item An Array Reference 430 431If C<$output_filename_or_reference> is an array reference, 432the uncompressed data will be pushed onto the array. 433 434=item An Output FileGlob 435 436If C<$output_filename_or_reference> is a string that is delimited by the 437characters "<" and ">" C<gunzip> will assume that it is an 438I<output fileglob string>. The output is the list of files that match the 439fileglob. 440 441When C<$output_filename_or_reference> is an fileglob string, 442C<$input_filename_or_reference> must also be a fileglob string. Anything 443else is an error. 444 445See L<File::GlobMapper|File::GlobMapper> for more details. 446 447=back 448 449If the C<$output_filename_or_reference> parameter is any other type, 450C<undef> will be returned. 451 452=head2 Notes 453 454When C<$input_filename_or_reference> maps to multiple compressed 455files/buffers and C<$output_filename_or_reference> is 456a single file/buffer, after uncompression C<$output_filename_or_reference> will contain a 457concatenation of all the uncompressed data from each of the input 458files/buffers. 459 460=head2 Optional Parameters 461 462The optional parameters for the one-shot function C<gunzip> 463are (for the most part) identical to those used with the OO interface defined in the 464L</"Constructor Options"> section. The exceptions are listed below 465 466=over 5 467 468=item C<< AutoClose => 0|1 >> 469 470This option applies to any input or output data streams to 471C<gunzip> that are filehandles. 472 473If C<AutoClose> is specified, and the value is true, it will result in all 474input and/or output filehandles being closed once C<gunzip> has 475completed. 476 477This parameter defaults to 0. 478 479=item C<< BinModeOut => 0|1 >> 480 481This option is now a no-op. All files will be written in binmode. 482 483=item C<< Append => 0|1 >> 484 485The behaviour of this option is dependent on the type of output data 486stream. 487 488=over 5 489 490=item * A Buffer 491 492If C<Append> is enabled, all uncompressed data will be append to the end of 493the output buffer. Otherwise the output buffer will be cleared before any 494uncompressed data is written to it. 495 496=item * A Filename 497 498If C<Append> is enabled, the file will be opened in append mode. Otherwise 499the contents of the file, if any, will be truncated before any uncompressed 500data is written to it. 501 502=item * A Filehandle 503 504If C<Append> is enabled, the filehandle will be positioned to the end of 505the file via a call to C<seek> before any uncompressed data is 506written to it. Otherwise the file pointer will not be moved. 507 508=back 509 510When C<Append> is specified, and set to true, it will I<append> all uncompressed 511data to the output data stream. 512 513So when the output is a filehandle it will carry out a seek to the eof 514before writing any uncompressed data. If the output is a filename, it will be opened for 515appending. If the output is a buffer, all uncompressed data will be 516appended to the existing buffer. 517 518Conversely when C<Append> is not specified, or it is present and is set to 519false, it will operate as follows. 520 521When the output is a filename, it will truncate the contents of the file 522before writing any uncompressed data. If the output is a filehandle 523its position will not be changed. If the output is a buffer, it will be 524wiped before any uncompressed data is output. 525 526Defaults to 0. 527 528=item C<< MultiStream => 0|1 >> 529 530If the input file/buffer contains multiple compressed data streams, this 531option will uncompress the whole lot as a single data stream. 532 533Defaults to 0. 534 535=item C<< TrailingData => $scalar >> 536 537Returns the data, if any, that is present immediately after the compressed 538data stream once uncompression is complete. 539 540This option can be used when there is useful information immediately 541following the compressed data stream, and you don't know the length of the 542compressed data stream. 543 544If the input is a buffer, C<trailingData> will return everything from the 545end of the compressed data stream to the end of the buffer. 546 547If the input is a filehandle, C<trailingData> will return the data that is 548left in the filehandle input buffer once the end of the compressed data 549stream has been reached. You can then use the filehandle to read the rest 550of the input file. 551 552Don't bother using C<trailingData> if the input is a filename. 553 554If you know the length of the compressed data stream before you start 555uncompressing, you can avoid having to use C<trailingData> by setting the 556C<InputLength> option. 557 558=back 559 560=head2 OneShot Examples 561 562To read the contents of the file C<file1.txt.gz> and write the 563uncompressed data to the file C<file1.txt>. 564 565 use strict ; 566 use warnings ; 567 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 568 569 my $input = "file1.txt.gz"; 570 my $output = "file1.txt"; 571 gunzip $input => $output 572 or die "gunzip failed: $GunzipError\n"; 573 574To read from an existing Perl filehandle, C<$input>, and write the 575uncompressed data to a buffer, C<$buffer>. 576 577 use strict ; 578 use warnings ; 579 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 580 use IO::File ; 581 582 my $input = IO::File->new( "<file1.txt.gz" ) 583 or die "Cannot open 'file1.txt.gz': $!\n" ; 584 my $buffer ; 585 gunzip $input => \$buffer 586 or die "gunzip failed: $GunzipError\n"; 587 588To uncompress all files in the directory "/my/home" that match "*.txt.gz" and store the compressed data in the same directory 589 590 use strict ; 591 use warnings ; 592 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 593 594 gunzip '</my/home/*.txt.gz>' => '</my/home/#1.txt>' 595 or die "gunzip failed: $GunzipError\n"; 596 597and if you want to compress each file one at a time, this will do the trick 598 599 use strict ; 600 use warnings ; 601 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 602 603 for my $input ( glob "/my/home/*.txt.gz" ) 604 { 605 my $output = $input; 606 $output =~ s/.gz// ; 607 gunzip $input => $output 608 or die "Error compressing '$input': $GunzipError\n"; 609 } 610 611=head1 OO Interface 612 613=head2 Constructor 614 615The format of the constructor for IO::Uncompress::Gunzip is shown below 616 617 my $z = IO::Uncompress::Gunzip->new( $input [OPTS] ) 618 or die "IO::Uncompress::Gunzip failed: $GunzipError\n"; 619 620The constructor takes one mandatory parameter, C<$input>, defined below, and 621zero or more C<OPTS>, defined in L<Constructor Options>. 622 623Returns an C<IO::Uncompress::Gunzip> object on success and undef on failure. 624The variable C<$GunzipError> will contain an error message on failure. 625 626If you are running Perl 5.005 or better the object, C<$z>, returned from 627IO::Uncompress::Gunzip can be used exactly like an L<IO::File|IO::File> filehandle. 628This means that all normal input file operations can be carried out with 629C<$z>. For example, to read a line from a compressed file/buffer you can 630use either of these forms 631 632 $line = $z->getline(); 633 $line = <$z>; 634 635Below is a simple exaple of using the OO interface to read the compressed file 636C<myfile.gz> and write its contents to stdout. 637 638 my $filename = "myfile.gz"; 639 my $z = IO::Uncompress::Gunzip->new($filename) 640 or die "IO::Uncompress::Gunzip failed: $GunzipError\n"; 641 642 while (<$z>) { 643 print $_; 644 } 645 $z->close(); 646 647See L</EXAMPLES> for further examples 648 649The mandatory parameter C<$input> is used to determine the source of the 650compressed data. This parameter can take one of three forms. 651 652=over 5 653 654=item A filename 655 656If the C<$input> parameter is a scalar, it is assumed to be a filename. This 657file will be opened for reading and the compressed data will be read from it. 658 659=item A filehandle 660 661If the C<$input> parameter is a filehandle, the compressed data will be 662read from it. 663The string '-' can be used as an alias for standard input. 664 665=item A scalar reference 666 667If C<$input> is a scalar reference, the compressed data will be read from 668C<$$input>. 669 670=back 671 672=head2 Constructor Options 673 674The option names defined below are case insensitive and can be optionally 675prefixed by a '-'. So all of the following are valid 676 677 -AutoClose 678 -autoclose 679 AUTOCLOSE 680 autoclose 681 682OPTS is a combination of the following options: 683 684=over 5 685 686=item C<< AutoClose => 0|1 >> 687 688This option is only valid when the C<$input> parameter is a filehandle. If 689specified, and the value is true, it will result in the file being closed once 690either the C<close> method is called or the IO::Uncompress::Gunzip object is 691destroyed. 692 693This parameter defaults to 0. 694 695=item C<< MultiStream => 0|1 >> 696 697Allows multiple concatenated compressed streams to be treated as a single 698compressed stream. Decompression will stop once either the end of the 699file/buffer is reached, an error is encountered (premature eof, corrupt 700compressed data) or the end of a stream is not immediately followed by the 701start of another stream. 702 703This parameter defaults to 0. 704 705=item C<< Prime => $string >> 706 707This option will uncompress the contents of C<$string> before processing the 708input file/buffer. 709 710This option can be useful when the compressed data is embedded in another 711file/data structure and it is not possible to work out where the compressed 712data begins without having to read the first few bytes. If this is the 713case, the uncompression can be I<primed> with these bytes using this 714option. 715 716=item C<< Transparent => 0|1 >> 717 718If this option is set and the input file/buffer is not compressed data, 719the module will allow reading of it anyway. 720 721In addition, if the input file/buffer does contain compressed data and 722there is non-compressed data immediately following it, setting this option 723will make this module treat the whole file/buffer as a single data stream. 724 725This option defaults to 1. 726 727=item C<< BlockSize => $num >> 728 729When reading the compressed input data, IO::Uncompress::Gunzip will read it in 730blocks of C<$num> bytes. 731 732This option defaults to 4096. 733 734=item C<< InputLength => $size >> 735 736When present this option will limit the number of compressed bytes read 737from the input file/buffer to C<$size>. This option can be used in the 738situation where there is useful data directly after the compressed data 739stream and you know beforehand the exact length of the compressed data 740stream. 741 742This option is mostly used when reading from a filehandle, in which case 743the file pointer will be left pointing to the first byte directly after the 744compressed data stream. 745 746This option defaults to off. 747 748=item C<< Append => 0|1 >> 749 750This option controls what the C<read> method does with uncompressed data. 751 752If set to 1, all uncompressed data will be appended to the output parameter 753of the C<read> method. 754 755If set to 0, the contents of the output parameter of the C<read> method 756will be overwritten by the uncompressed data. 757 758Defaults to 0. 759 760=item C<< Strict => 0|1 >> 761 762This option controls whether the extra checks defined below are used when 763carrying out the decompression. When Strict is on, the extra tests are 764carried out, when Strict is off they are not. 765 766The default for this option is off. 767 768=over 5 769 770=item 1 771 772If the FHCRC bit is set in the gzip FLG header byte, the CRC16 bytes in the 773header must match the crc16 value of the gzip header actually read. 774 775=item 2 776 777If the gzip header contains a name field (FNAME) it consists solely of ISO 7788859-1 characters. 779 780=item 3 781 782If the gzip header contains a comment field (FCOMMENT) it consists solely 783of ISO 8859-1 characters plus line-feed. 784 785=item 4 786 787If the gzip FEXTRA header field is present it must conform to the sub-field 788structure as defined in RFC 1952. 789 790=item 5 791 792The CRC32 and ISIZE trailer fields must be present. 793 794=item 6 795 796The value of the CRC32 field read must match the crc32 value of the 797uncompressed data actually contained in the gzip file. 798 799=item 7 800 801The value of the ISIZE fields read must match the length of the 802uncompressed data actually read from the file. 803 804=back 805 806=item C<< ParseExtra => 0|1 >> 807If the gzip FEXTRA header field is present and this option is set, it will 808force the module to check that it conforms to the sub-field structure as 809defined in RFC 1952. 810 811If the C<Strict> is on it will automatically enable this option. 812 813Defaults to 0. 814 815=back 816 817=head1 Methods 818 819=head2 read 820 821Usage is 822 823 $status = $z->read($buffer) 824 825Reads a block of compressed data (the size of the compressed block is 826determined by the C<Buffer> option in the constructor), uncompresses it and 827writes any uncompressed data into C<$buffer>. If the C<Append> parameter is 828set in the constructor, the uncompressed data will be appended to the 829C<$buffer> parameter. Otherwise C<$buffer> will be overwritten. 830 831Returns the number of uncompressed bytes written to C<$buffer>, zero if eof 832or a negative number on error. 833 834=head2 read 835 836Usage is 837 838 $status = $z->read($buffer, $length) 839 $status = $z->read($buffer, $length, $offset) 840 841 $status = read($z, $buffer, $length) 842 $status = read($z, $buffer, $length, $offset) 843 844Attempt to read C<$length> bytes of uncompressed data into C<$buffer>. 845 846The main difference between this form of the C<read> method and the 847previous one, is that this one will attempt to return I<exactly> C<$length> 848bytes. The only circumstances that this function will not is if end-of-file 849or an IO error is encountered. 850 851Returns the number of uncompressed bytes written to C<$buffer>, zero if eof 852or a negative number on error. 853 854=head2 getline 855 856Usage is 857 858 $line = $z->getline() 859 $line = <$z> 860 861Reads a single line. 862 863This method fully supports the use of the variable C<$/> (or 864C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to 865determine what constitutes an end of line. Paragraph mode, record mode and 866file slurp mode are all supported. 867 868=head2 getc 869 870Usage is 871 872 $char = $z->getc() 873 874Read a single character. 875 876=head2 ungetc 877 878Usage is 879 880 $char = $z->ungetc($string) 881 882=head2 inflateSync 883 884Usage is 885 886 $status = $z->inflateSync() 887 888TODO 889 890=head2 getHeaderInfo 891 892Usage is 893 894 $hdr = $z->getHeaderInfo(); 895 @hdrs = $z->getHeaderInfo(); 896 897This method returns either a hash reference (in scalar context) or a list 898or hash references (in array context) that contains information about each 899of the header fields in the compressed data stream(s). 900 901=over 5 902 903=item Name 904 905The contents of the Name header field, if present. If no name is 906present, the value will be undef. Note this is different from a zero length 907name, which will return an empty string. 908 909=item Comment 910 911The contents of the Comment header field, if present. If no comment is 912present, the value will be undef. Note this is different from a zero length 913comment, which will return an empty string. 914 915=back 916 917=head2 tell 918 919Usage is 920 921 $z->tell() 922 tell $z 923 924Returns the uncompressed file offset. 925 926=head2 eof 927 928Usage is 929 930 $z->eof(); 931 eof($z); 932 933Returns true if the end of the compressed input stream has been reached. 934 935=head2 seek 936 937 $z->seek($position, $whence); 938 seek($z, $position, $whence); 939 940Provides a sub-set of the C<seek> functionality, with the restriction 941that it is only legal to seek forward in the input file/buffer. 942It is a fatal error to attempt to seek backward. 943 944Note that the implementation of C<seek> in this module does not provide 945true random access to a compressed file/buffer. It works by uncompressing 946data from the current offset in the file/buffer until it reaches the 947uncompressed offset specified in the parameters to C<seek>. For very small 948files this may be acceptable behaviour. For large files it may cause an 949unacceptable delay. 950 951The C<$whence> parameter takes one the usual values, namely SEEK_SET, 952SEEK_CUR or SEEK_END. 953 954Returns 1 on success, 0 on failure. 955 956=head2 binmode 957 958Usage is 959 960 $z->binmode 961 binmode $z ; 962 963This is a noop provided for completeness. 964 965=head2 opened 966 967 $z->opened() 968 969Returns true if the object currently refers to a opened file/buffer. 970 971=head2 autoflush 972 973 my $prev = $z->autoflush() 974 my $prev = $z->autoflush(EXPR) 975 976If the C<$z> object is associated with a file or a filehandle, this method 977returns the current autoflush setting for the underlying filehandle. If 978C<EXPR> is present, and is non-zero, it will enable flushing after every 979write/print operation. 980 981If C<$z> is associated with a buffer, this method has no effect and always 982returns C<undef>. 983 984B<Note> that the special variable C<$|> B<cannot> be used to set or 985retrieve the autoflush setting. 986 987=head2 input_line_number 988 989 $z->input_line_number() 990 $z->input_line_number(EXPR) 991 992Returns the current uncompressed line number. If C<EXPR> is present it has 993the effect of setting the line number. Note that setting the line number 994does not change the current position within the file/buffer being read. 995 996The contents of C<$/> are used to determine what constitutes a line 997terminator. 998 999=head2 fileno 1000 1001 $z->fileno() 1002 fileno($z) 1003 1004If the C<$z> object is associated with a file or a filehandle, C<fileno> 1005will return the underlying file descriptor. Once the C<close> method is 1006called C<fileno> will return C<undef>. 1007 1008If the C<$z> object is associated with a buffer, this method will return 1009C<undef>. 1010 1011=head2 close 1012 1013 $z->close() ; 1014 close $z ; 1015 1016Closes the output file/buffer. 1017 1018For most versions of Perl this method will be automatically invoked if 1019the IO::Uncompress::Gunzip object is destroyed (either explicitly or by the 1020variable with the reference to the object going out of scope). The 1021exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In 1022these cases, the C<close> method will be called automatically, but 1023not until global destruction of all live objects when the program is 1024terminating. 1025 1026Therefore, if you want your scripts to be able to run on all versions 1027of Perl, you should call C<close> explicitly and not rely on automatic 1028closing. 1029 1030Returns true on success, otherwise 0. 1031 1032If the C<AutoClose> option has been enabled when the IO::Uncompress::Gunzip 1033object was created, and the object is associated with a file, the 1034underlying file will also be closed. 1035 1036=head2 nextStream 1037 1038Usage is 1039 1040 my $status = $z->nextStream(); 1041 1042Skips to the next compressed data stream in the input file/buffer. If a new 1043compressed data stream is found, the eof marker will be cleared and C<$.> 1044will be reset to 0. 1045 1046Returns 1 if a new stream was found, 0 if none was found, and -1 if an 1047error was encountered. 1048 1049=head2 trailingData 1050 1051Usage is 1052 1053 my $data = $z->trailingData(); 1054 1055Returns the data, if any, that is present immediately after the compressed 1056data stream once uncompression is complete. It only makes sense to call 1057this method once the end of the compressed data stream has been 1058encountered. 1059 1060This option can be used when there is useful information immediately 1061following the compressed data stream, and you don't know the length of the 1062compressed data stream. 1063 1064If the input is a buffer, C<trailingData> will return everything from the 1065end of the compressed data stream to the end of the buffer. 1066 1067If the input is a filehandle, C<trailingData> will return the data that is 1068left in the filehandle input buffer once the end of the compressed data 1069stream has been reached. You can then use the filehandle to read the rest 1070of the input file. 1071 1072Don't bother using C<trailingData> if the input is a filename. 1073 1074If you know the length of the compressed data stream before you start 1075uncompressing, you can avoid having to use C<trailingData> by setting the 1076C<InputLength> option in the constructor. 1077 1078=head1 Importing 1079 1080No symbolic constants are required by IO::Uncompress::Gunzip at present. 1081 1082=over 5 1083 1084=item :all 1085 1086Imports C<gunzip> and C<$GunzipError>. 1087Same as doing this 1088 1089 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 1090 1091=back 1092 1093=head1 EXAMPLES 1094 1095=head2 Working with Net::FTP 1096 1097See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP"> 1098 1099=head1 SUPPORT 1100 1101General feedback/questions/bug reports should be sent to 1102L<https://github.com/pmqs/IO-Compress/issues> (preferred) or 1103L<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>. 1104 1105=head1 SEE ALSO 1106 1107L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzip>, L<IO::Uncompress::UnLzip>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Compress::Zstd>, L<IO::Uncompress::UnZstd>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> 1108 1109L<IO::Compress::FAQ|IO::Compress::FAQ> 1110 1111L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, 1112L<Archive::Tar|Archive::Tar>, 1113L<IO::Zlib|IO::Zlib> 1114 1115For RFC 1950, 1951 and 1952 see 1116L<https://datatracker.ietf.org/doc/html/rfc1950>, 1117L<https://datatracker.ietf.org/doc/html/rfc1951> and 1118L<https://datatracker.ietf.org/doc/html/rfc1952> 1119 1120The I<zlib> compression library was written by Jean-loup Gailly 1121C<gzip@prep.ai.mit.edu> and Mark Adler C<madler@alumni.caltech.edu>. 1122 1123The primary site for the I<zlib> compression library is 1124L<http://www.zlib.org>. 1125 1126The primary site for the I<zlib-ng> compression library is 1127L<https://github.com/zlib-ng/zlib-ng>. 1128 1129The primary site for gzip is L<http://www.gzip.org>. 1130 1131=head1 AUTHOR 1132 1133This module was written by Paul Marquess, C<pmqs@cpan.org>. 1134 1135=head1 MODIFICATION HISTORY 1136 1137See the Changes file. 1138 1139=head1 COPYRIGHT AND LICENSE 1140 1141Copyright (c) 2005-2024 Paul Marquess. All rights reserved. 1142 1143This program is free software; you can redistribute it and/or 1144modify it under the same terms as Perl itself. 1145