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.084 ; 13 14use Compress::Raw::Zlib 2.084 () ; 15use IO::Compress::Base::Common 2.084 qw(:Status ); 16use IO::Compress::Gzip::Constants 2.084 ; 17use IO::Compress::Zlib::Extra 2.084 ; 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.084'; 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 = new IO::Uncompress::Gunzip $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>. 352 353=head3 The C<$input_filename_or_reference> parameter 354 355The parameter, C<$input_filename_or_reference>, is used to define the 356source of the compressed data. 357 358It can take one of the following forms: 359 360=over 5 361 362=item A filename 363 364If the <$input_filename_or_reference> parameter is a simple scalar, it is 365assumed to be a filename. This file will be opened for reading and the 366input data will be read from it. 367 368=item A filehandle 369 370If the C<$input_filename_or_reference> parameter is a filehandle, the input 371data will be read from it. The string '-' can be used as an alias for 372standard input. 373 374=item A scalar reference 375 376If C<$input_filename_or_reference> is a scalar reference, the input data 377will be read from C<$$input_filename_or_reference>. 378 379=item An array reference 380 381If C<$input_filename_or_reference> is an array reference, each element in 382the array must be a filename. 383 384The input data will be read from each file in turn. 385 386The complete array will be walked to ensure that it only 387contains valid filenames before any data is uncompressed. 388 389=item An Input FileGlob string 390 391If C<$input_filename_or_reference> is a string that is delimited by the 392characters "<" and ">" C<gunzip> will assume that it is an 393I<input fileglob string>. The input is the list of files that match the 394fileglob. 395 396See L<File::GlobMapper|File::GlobMapper> for more details. 397 398=back 399 400If the C<$input_filename_or_reference> parameter is any other type, 401C<undef> will be returned. 402 403=head3 The C<$output_filename_or_reference> parameter 404 405The parameter C<$output_filename_or_reference> is used to control the 406destination of the uncompressed data. This parameter can take one of 407these forms. 408 409=over 5 410 411=item A filename 412 413If the C<$output_filename_or_reference> parameter is a simple scalar, it is 414assumed to be a filename. This file will be opened for writing and the 415uncompressed data will be written to it. 416 417=item A filehandle 418 419If the C<$output_filename_or_reference> parameter is a filehandle, the 420uncompressed data will be written to it. The string '-' can be used as 421an alias for standard output. 422 423=item A scalar reference 424 425If C<$output_filename_or_reference> is a scalar reference, the 426uncompressed data will be stored in C<$$output_filename_or_reference>. 427 428=item An Array Reference 429 430If C<$output_filename_or_reference> is an array reference, 431the uncompressed data will be pushed onto the array. 432 433=item An Output FileGlob 434 435If C<$output_filename_or_reference> is a string that is delimited by the 436characters "<" and ">" C<gunzip> will assume that it is an 437I<output fileglob string>. The output is the list of files that match the 438fileglob. 439 440When C<$output_filename_or_reference> is an fileglob string, 441C<$input_filename_or_reference> must also be a fileglob string. Anything 442else is an error. 443 444See L<File::GlobMapper|File::GlobMapper> for more details. 445 446=back 447 448If the C<$output_filename_or_reference> parameter is any other type, 449C<undef> will be returned. 450 451=head2 Notes 452 453When C<$input_filename_or_reference> maps to multiple compressed 454files/buffers and C<$output_filename_or_reference> is 455a single file/buffer, after uncompression C<$output_filename_or_reference> will contain a 456concatenation of all the uncompressed data from each of the input 457files/buffers. 458 459=head2 Optional Parameters 460 461Unless specified below, the optional parameters for C<gunzip>, 462C<OPTS>, are the same as those used with the OO interface defined in the 463L</"Constructor Options"> section below. 464 465=over 5 466 467=item C<< AutoClose => 0|1 >> 468 469This option applies to any input or output data streams to 470C<gunzip> that are filehandles. 471 472If C<AutoClose> is specified, and the value is true, it will result in all 473input and/or output filehandles being closed once C<gunzip> has 474completed. 475 476This parameter defaults to 0. 477 478=item C<< BinModeOut => 0|1 >> 479 480This option is now a no-op. All files will be written in binmode. 481 482=item C<< Append => 0|1 >> 483 484The behaviour of this option is dependent on the type of output data 485stream. 486 487=over 5 488 489=item * A Buffer 490 491If C<Append> is enabled, all uncompressed data will be append to the end of 492the output buffer. Otherwise the output buffer will be cleared before any 493uncompressed data is written to it. 494 495=item * A Filename 496 497If C<Append> is enabled, the file will be opened in append mode. Otherwise 498the contents of the file, if any, will be truncated before any uncompressed 499data is written to it. 500 501=item * A Filehandle 502 503If C<Append> is enabled, the filehandle will be positioned to the end of 504the file via a call to C<seek> before any uncompressed data is 505written to it. Otherwise the file pointer will not be moved. 506 507=back 508 509When C<Append> is specified, and set to true, it will I<append> all uncompressed 510data to the output data stream. 511 512So when the output is a filehandle it will carry out a seek to the eof 513before writing any uncompressed data. If the output is a filename, it will be opened for 514appending. If the output is a buffer, all uncompressed data will be 515appended to the existing buffer. 516 517Conversely when C<Append> is not specified, or it is present and is set to 518false, it will operate as follows. 519 520When the output is a filename, it will truncate the contents of the file 521before writing any uncompressed data. If the output is a filehandle 522its position will not be changed. If the output is a buffer, it will be 523wiped before any uncompressed data is output. 524 525Defaults to 0. 526 527=item C<< MultiStream => 0|1 >> 528 529If the input file/buffer contains multiple compressed data streams, this 530option will uncompress the whole lot as a single data stream. 531 532Defaults to 0. 533 534=item C<< TrailingData => $scalar >> 535 536Returns the data, if any, that is present immediately after the compressed 537data stream once uncompression is complete. 538 539This option can be used when there is useful information immediately 540following the compressed data stream, and you don't know the length of the 541compressed data stream. 542 543If the input is a buffer, C<trailingData> will return everything from the 544end of the compressed data stream to the end of the buffer. 545 546If the input is a filehandle, C<trailingData> will return the data that is 547left in the filehandle input buffer once the end of the compressed data 548stream has been reached. You can then use the filehandle to read the rest 549of the input file. 550 551Don't bother using C<trailingData> if the input is a filename. 552 553If you know the length of the compressed data stream before you start 554uncompressing, you can avoid having to use C<trailingData> by setting the 555C<InputLength> option. 556 557=back 558 559=head2 Examples 560 561To read the contents of the file C<file1.txt.gz> and write the 562uncompressed data to the file C<file1.txt>. 563 564 use strict ; 565 use warnings ; 566 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 567 568 my $input = "file1.txt.gz"; 569 my $output = "file1.txt"; 570 gunzip $input => $output 571 or die "gunzip failed: $GunzipError\n"; 572 573To read from an existing Perl filehandle, C<$input>, and write the 574uncompressed data to a buffer, C<$buffer>. 575 576 use strict ; 577 use warnings ; 578 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 579 use IO::File ; 580 581 my $input = new IO::File "<file1.txt.gz" 582 or die "Cannot open 'file1.txt.gz': $!\n" ; 583 my $buffer ; 584 gunzip $input => \$buffer 585 or die "gunzip failed: $GunzipError\n"; 586 587To uncompress all files in the directory "/my/home" that match "*.txt.gz" and store the compressed data in the same directory 588 589 use strict ; 590 use warnings ; 591 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 592 593 gunzip '</my/home/*.txt.gz>' => '</my/home/#1.txt>' 594 or die "gunzip failed: $GunzipError\n"; 595 596and if you want to compress each file one at a time, this will do the trick 597 598 use strict ; 599 use warnings ; 600 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 601 602 for my $input ( glob "/my/home/*.txt.gz" ) 603 { 604 my $output = $input; 605 $output =~ s/.gz// ; 606 gunzip $input => $output 607 or die "Error compressing '$input': $GunzipError\n"; 608 } 609 610=head1 OO Interface 611 612=head2 Constructor 613 614The format of the constructor for IO::Uncompress::Gunzip is shown below 615 616 my $z = new IO::Uncompress::Gunzip $input [OPTS] 617 or die "IO::Uncompress::Gunzip failed: $GunzipError\n"; 618 619Returns an C<IO::Uncompress::Gunzip> object on success and undef on failure. 620The variable C<$GunzipError> will contain an error message on failure. 621 622If you are running Perl 5.005 or better the object, C<$z>, returned from 623IO::Uncompress::Gunzip can be used exactly like an L<IO::File|IO::File> filehandle. 624This means that all normal input file operations can be carried out with 625C<$z>. For example, to read a line from a compressed file/buffer you can 626use either of these forms 627 628 $line = $z->getline(); 629 $line = <$z>; 630 631The mandatory parameter C<$input> is used to determine the source of the 632compressed data. This parameter can take one of three forms. 633 634=over 5 635 636=item A filename 637 638If the C<$input> parameter is a scalar, it is assumed to be a filename. This 639file will be opened for reading and the compressed data will be read from it. 640 641=item A filehandle 642 643If the C<$input> parameter is a filehandle, the compressed data will be 644read from it. 645The string '-' can be used as an alias for standard input. 646 647=item A scalar reference 648 649If C<$input> is a scalar reference, the compressed data will be read from 650C<$$input>. 651 652=back 653 654=head2 Constructor Options 655 656The option names defined below are case insensitive and can be optionally 657prefixed by a '-'. So all of the following are valid 658 659 -AutoClose 660 -autoclose 661 AUTOCLOSE 662 autoclose 663 664OPTS is a combination of the following options: 665 666=over 5 667 668=item C<< AutoClose => 0|1 >> 669 670This option is only valid when the C<$input> parameter is a filehandle. If 671specified, and the value is true, it will result in the file being closed once 672either the C<close> method is called or the IO::Uncompress::Gunzip object is 673destroyed. 674 675This parameter defaults to 0. 676 677=item C<< MultiStream => 0|1 >> 678 679Allows multiple concatenated compressed streams to be treated as a single 680compressed stream. Decompression will stop once either the end of the 681file/buffer is reached, an error is encountered (premature eof, corrupt 682compressed data) or the end of a stream is not immediately followed by the 683start of another stream. 684 685This parameter defaults to 0. 686 687=item C<< Prime => $string >> 688 689This option will uncompress the contents of C<$string> before processing the 690input file/buffer. 691 692This option can be useful when the compressed data is embedded in another 693file/data structure and it is not possible to work out where the compressed 694data begins without having to read the first few bytes. If this is the 695case, the uncompression can be I<primed> with these bytes using this 696option. 697 698=item C<< Transparent => 0|1 >> 699 700If this option is set and the input file/buffer is not compressed data, 701the module will allow reading of it anyway. 702 703In addition, if the input file/buffer does contain compressed data and 704there is non-compressed data immediately following it, setting this option 705will make this module treat the whole file/buffer as a single data stream. 706 707This option defaults to 1. 708 709=item C<< BlockSize => $num >> 710 711When reading the compressed input data, IO::Uncompress::Gunzip will read it in 712blocks of C<$num> bytes. 713 714This option defaults to 4096. 715 716=item C<< InputLength => $size >> 717 718When present this option will limit the number of compressed bytes read 719from the input file/buffer to C<$size>. This option can be used in the 720situation where there is useful data directly after the compressed data 721stream and you know beforehand the exact length of the compressed data 722stream. 723 724This option is mostly used when reading from a filehandle, in which case 725the file pointer will be left pointing to the first byte directly after the 726compressed data stream. 727 728This option defaults to off. 729 730=item C<< Append => 0|1 >> 731 732This option controls what the C<read> method does with uncompressed data. 733 734If set to 1, all uncompressed data will be appended to the output parameter 735of the C<read> method. 736 737If set to 0, the contents of the output parameter of the C<read> method 738will be overwritten by the uncompressed data. 739 740Defaults to 0. 741 742=item C<< Strict => 0|1 >> 743 744This option controls whether the extra checks defined below are used when 745carrying out the decompression. When Strict is on, the extra tests are 746carried out, when Strict is off they are not. 747 748The default for this option is off. 749 750=over 5 751 752=item 1 753 754If the FHCRC bit is set in the gzip FLG header byte, the CRC16 bytes in the 755header must match the crc16 value of the gzip header actually read. 756 757=item 2 758 759If the gzip header contains a name field (FNAME) it consists solely of ISO 7608859-1 characters. 761 762=item 3 763 764If the gzip header contains a comment field (FCOMMENT) it consists solely 765of ISO 8859-1 characters plus line-feed. 766 767=item 4 768 769If the gzip FEXTRA header field is present it must conform to the sub-field 770structure as defined in RFC 1952. 771 772=item 5 773 774The CRC32 and ISIZE trailer fields must be present. 775 776=item 6 777 778The value of the CRC32 field read must match the crc32 value of the 779uncompressed data actually contained in the gzip file. 780 781=item 7 782 783The value of the ISIZE fields read must match the length of the 784uncompressed data actually read from the file. 785 786=back 787 788=item C<< ParseExtra => 0|1 >> 789If the gzip FEXTRA header field is present and this option is set, it will 790force the module to check that it conforms to the sub-field structure as 791defined in RFC 1952. 792 793If the C<Strict> is on it will automatically enable this option. 794 795Defaults to 0. 796 797=back 798 799=head2 Examples 800 801TODO 802 803=head1 Methods 804 805=head2 read 806 807Usage is 808 809 $status = $z->read($buffer) 810 811Reads a block of compressed data (the size of the compressed block is 812determined by the C<Buffer> option in the constructor), uncompresses it and 813writes any uncompressed data into C<$buffer>. If the C<Append> parameter is 814set in the constructor, the uncompressed data will be appended to the 815C<$buffer> parameter. Otherwise C<$buffer> will be overwritten. 816 817Returns the number of uncompressed bytes written to C<$buffer>, zero if eof 818or a negative number on error. 819 820=head2 read 821 822Usage is 823 824 $status = $z->read($buffer, $length) 825 $status = $z->read($buffer, $length, $offset) 826 827 $status = read($z, $buffer, $length) 828 $status = read($z, $buffer, $length, $offset) 829 830Attempt to read C<$length> bytes of uncompressed data into C<$buffer>. 831 832The main difference between this form of the C<read> method and the 833previous one, is that this one will attempt to return I<exactly> C<$length> 834bytes. The only circumstances that this function will not is if end-of-file 835or an IO error is encountered. 836 837Returns the number of uncompressed bytes written to C<$buffer>, zero if eof 838or a negative number on error. 839 840=head2 getline 841 842Usage is 843 844 $line = $z->getline() 845 $line = <$z> 846 847Reads a single line. 848 849This method fully supports the use of the variable C<$/> (or 850C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to 851determine what constitutes an end of line. Paragraph mode, record mode and 852file slurp mode are all supported. 853 854=head2 getc 855 856Usage is 857 858 $char = $z->getc() 859 860Read a single character. 861 862=head2 ungetc 863 864Usage is 865 866 $char = $z->ungetc($string) 867 868=head2 inflateSync 869 870Usage is 871 872 $status = $z->inflateSync() 873 874TODO 875 876=head2 getHeaderInfo 877 878Usage is 879 880 $hdr = $z->getHeaderInfo(); 881 @hdrs = $z->getHeaderInfo(); 882 883This method returns either a hash reference (in scalar context) or a list 884or hash references (in array context) that contains information about each 885of the header fields in the compressed data stream(s). 886 887=over 5 888 889=item Name 890 891The contents of the Name header field, if present. If no name is 892present, the value will be undef. Note this is different from a zero length 893name, which will return an empty string. 894 895=item Comment 896 897The contents of the Comment header field, if present. If no comment is 898present, the value will be undef. Note this is different from a zero length 899comment, which will return an empty string. 900 901=back 902 903=head2 tell 904 905Usage is 906 907 $z->tell() 908 tell $z 909 910Returns the uncompressed file offset. 911 912=head2 eof 913 914Usage is 915 916 $z->eof(); 917 eof($z); 918 919Returns true if the end of the compressed input stream has been reached. 920 921=head2 seek 922 923 $z->seek($position, $whence); 924 seek($z, $position, $whence); 925 926Provides a sub-set of the C<seek> functionality, with the restriction 927that it is only legal to seek forward in the input file/buffer. 928It is a fatal error to attempt to seek backward. 929 930Note that the implementation of C<seek> in this module does not provide 931true random access to a compressed file/buffer. It works by uncompressing 932data from the current offset in the file/buffer until it reaches the 933uncompressed offset specified in the parameters to C<seek>. For very small 934files this may be acceptable behaviour. For large files it may cause an 935unacceptable delay. 936 937The C<$whence> parameter takes one the usual values, namely SEEK_SET, 938SEEK_CUR or SEEK_END. 939 940Returns 1 on success, 0 on failure. 941 942=head2 binmode 943 944Usage is 945 946 $z->binmode 947 binmode $z ; 948 949This is a noop provided for completeness. 950 951=head2 opened 952 953 $z->opened() 954 955Returns true if the object currently refers to a opened file/buffer. 956 957=head2 autoflush 958 959 my $prev = $z->autoflush() 960 my $prev = $z->autoflush(EXPR) 961 962If the C<$z> object is associated with a file or a filehandle, this method 963returns the current autoflush setting for the underlying filehandle. If 964C<EXPR> is present, and is non-zero, it will enable flushing after every 965write/print operation. 966 967If C<$z> is associated with a buffer, this method has no effect and always 968returns C<undef>. 969 970B<Note> that the special variable C<$|> B<cannot> be used to set or 971retrieve the autoflush setting. 972 973=head2 input_line_number 974 975 $z->input_line_number() 976 $z->input_line_number(EXPR) 977 978Returns the current uncompressed line number. If C<EXPR> is present it has 979the effect of setting the line number. Note that setting the line number 980does not change the current position within the file/buffer being read. 981 982The contents of C<$/> are used to determine what constitutes a line 983terminator. 984 985=head2 fileno 986 987 $z->fileno() 988 fileno($z) 989 990If the C<$z> object is associated with a file or a filehandle, C<fileno> 991will return the underlying file descriptor. Once the C<close> method is 992called C<fileno> will return C<undef>. 993 994If the C<$z> object is associated with a buffer, this method will return 995C<undef>. 996 997=head2 close 998 999 $z->close() ; 1000 close $z ; 1001 1002Closes the output file/buffer. 1003 1004For most versions of Perl this method will be automatically invoked if 1005the IO::Uncompress::Gunzip object is destroyed (either explicitly or by the 1006variable with the reference to the object going out of scope). The 1007exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In 1008these cases, the C<close> method will be called automatically, but 1009not until global destruction of all live objects when the program is 1010terminating. 1011 1012Therefore, if you want your scripts to be able to run on all versions 1013of Perl, you should call C<close> explicitly and not rely on automatic 1014closing. 1015 1016Returns true on success, otherwise 0. 1017 1018If the C<AutoClose> option has been enabled when the IO::Uncompress::Gunzip 1019object was created, and the object is associated with a file, the 1020underlying file will also be closed. 1021 1022=head2 nextStream 1023 1024Usage is 1025 1026 my $status = $z->nextStream(); 1027 1028Skips to the next compressed data stream in the input file/buffer. If a new 1029compressed data stream is found, the eof marker will be cleared and C<$.> 1030will be reset to 0. 1031 1032Returns 1 if a new stream was found, 0 if none was found, and -1 if an 1033error was encountered. 1034 1035=head2 trailingData 1036 1037Usage is 1038 1039 my $data = $z->trailingData(); 1040 1041Returns the data, if any, that is present immediately after the compressed 1042data stream once uncompression is complete. It only makes sense to call 1043this method once the end of the compressed data stream has been 1044encountered. 1045 1046This option can be used when there is useful information immediately 1047following the compressed data stream, and you don't know the length of the 1048compressed data stream. 1049 1050If the input is a buffer, C<trailingData> will return everything from the 1051end of the compressed data stream to the end of the buffer. 1052 1053If the input is a filehandle, C<trailingData> will return the data that is 1054left in the filehandle input buffer once the end of the compressed data 1055stream has been reached. You can then use the filehandle to read the rest 1056of the input file. 1057 1058Don't bother using C<trailingData> if the input is a filename. 1059 1060If you know the length of the compressed data stream before you start 1061uncompressing, you can avoid having to use C<trailingData> by setting the 1062C<InputLength> option in the constructor. 1063 1064=head1 Importing 1065 1066No symbolic constants are required by this IO::Uncompress::Gunzip at present. 1067 1068=over 5 1069 1070=item :all 1071 1072Imports C<gunzip> and C<$GunzipError>. 1073Same as doing this 1074 1075 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 1076 1077=back 1078 1079=head1 EXAMPLES 1080 1081=head2 Working with Net::FTP 1082 1083See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP"> 1084 1085=head1 SEE ALSO 1086 1087L<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> 1088 1089L<IO::Compress::FAQ|IO::Compress::FAQ> 1090 1091L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, 1092L<Archive::Tar|Archive::Tar>, 1093L<IO::Zlib|IO::Zlib> 1094 1095For RFC 1950, 1951 and 1952 see 1096L<http://www.faqs.org/rfcs/rfc1950.html>, 1097L<http://www.faqs.org/rfcs/rfc1951.html> and 1098L<http://www.faqs.org/rfcs/rfc1952.html> 1099 1100The I<zlib> compression library was written by Jean-loup Gailly 1101C<gzip@prep.ai.mit.edu> and Mark Adler C<madler@alumni.caltech.edu>. 1102 1103The primary site for the I<zlib> compression library is 1104L<http://www.zlib.org>. 1105 1106The primary site for gzip is L<http://www.gzip.org>. 1107 1108=head1 AUTHOR 1109 1110This module was written by Paul Marquess, C<pmqs@cpan.org>. 1111 1112=head1 MODIFICATION HISTORY 1113 1114See the Changes file. 1115 1116=head1 COPYRIGHT AND LICENSE 1117 1118Copyright (c) 2005-2019 Paul Marquess. All rights reserved. 1119 1120This program is free software; you can redistribute it and/or 1121modify it under the same terms as Perl itself. 1122 1123