1package IO::Compress::Gzip ; 2 3require 5.006 ; 4 5use strict ; 6use warnings; 7use bytes; 8 9require Exporter ; 10 11use IO::Compress::RawDeflate 2.084 () ; 12use IO::Compress::Adapter::Deflate 2.084 ; 13 14use IO::Compress::Base::Common 2.084 qw(:Status ); 15use IO::Compress::Gzip::Constants 2.084 ; 16use IO::Compress::Zlib::Extra 2.084 ; 17 18BEGIN 19{ 20 if (defined &utf8::downgrade ) 21 { *noUTF8 = \&utf8::downgrade } 22 else 23 { *noUTF8 = sub {} } 24} 25 26our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, %DEFLATE_CONSTANTS, $GzipError); 27 28$VERSION = '2.084'; 29$GzipError = '' ; 30 31@ISA = qw(IO::Compress::RawDeflate Exporter); 32@EXPORT_OK = qw( $GzipError gzip ) ; 33%EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ; 34 35push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; 36Exporter::export_ok_tags('all'); 37 38sub new 39{ 40 my $class = shift ; 41 42 my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$GzipError); 43 44 $obj->_create(undef, @_); 45} 46 47 48sub gzip 49{ 50 my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$GzipError); 51 return $obj->_def(@_); 52} 53 54#sub newHeader 55#{ 56# my $self = shift ; 57# #return GZIP_MINIMUM_HEADER ; 58# return $self->mkHeader(*$self->{Got}); 59#} 60 61sub getExtraParams 62{ 63 my $self = shift ; 64 65 return ( 66 # zlib behaviour 67 $self->getZlibParams(), 68 69 # Gzip header fields 70 'minimal' => [IO::Compress::Base::Common::Parse_boolean, 0], 71 'comment' => [IO::Compress::Base::Common::Parse_any, undef], 72 'name' => [IO::Compress::Base::Common::Parse_any, undef], 73 'time' => [IO::Compress::Base::Common::Parse_any, undef], 74 'textflag' => [IO::Compress::Base::Common::Parse_boolean, 0], 75 'headercrc' => [IO::Compress::Base::Common::Parse_boolean, 0], 76 'os_code' => [IO::Compress::Base::Common::Parse_unsigned, $Compress::Raw::Zlib::gzip_os_code], 77 'extrafield'=> [IO::Compress::Base::Common::Parse_any, undef], 78 'extraflags'=> [IO::Compress::Base::Common::Parse_any, undef], 79 80 ); 81} 82 83 84sub ckParams 85{ 86 my $self = shift ; 87 my $got = shift ; 88 89 # gzip always needs crc32 90 $got->setValue('crc32' => 1); 91 92 return 1 93 if $got->getValue('merge') ; 94 95 my $strict = $got->getValue('strict') ; 96 97 98 { 99 if (! $got->parsed('time') ) { 100 # Modification time defaults to now. 101 $got->setValue(time => time) ; 102 } 103 104 # Check that the Name & Comment don't have embedded NULLs 105 # Also check that they only contain ISO 8859-1 chars. 106 if ($got->parsed('name') && defined $got->getValue('name')) { 107 my $name = $got->getValue('name'); 108 109 return $self->saveErrorString(undef, "Null Character found in Name", 110 Z_DATA_ERROR) 111 if $strict && $name =~ /\x00/ ; 112 113 return $self->saveErrorString(undef, "Non ISO 8859-1 Character found in Name", 114 Z_DATA_ERROR) 115 if $strict && $name =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ; 116 } 117 118 if ($got->parsed('comment') && defined $got->getValue('comment')) { 119 my $comment = $got->getValue('comment'); 120 121 return $self->saveErrorString(undef, "Null Character found in Comment", 122 Z_DATA_ERROR) 123 if $strict && $comment =~ /\x00/ ; 124 125 return $self->saveErrorString(undef, "Non ISO 8859-1 Character found in Comment", 126 Z_DATA_ERROR) 127 if $strict && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o; 128 } 129 130 if ($got->parsed('os_code') ) { 131 my $value = $got->getValue('os_code'); 132 133 return $self->saveErrorString(undef, "OS_Code must be between 0 and 255, got '$value'") 134 if $value < 0 || $value > 255 ; 135 136 } 137 138 # gzip only supports Deflate at present 139 $got->setValue('method' => Z_DEFLATED) ; 140 141 if ( ! $got->parsed('extraflags')) { 142 $got->setValue('extraflags' => 2) 143 if $got->getValue('level') == Z_BEST_COMPRESSION ; 144 $got->setValue('extraflags' => 4) 145 if $got->getValue('level') == Z_BEST_SPEED ; 146 } 147 148 my $data = $got->getValue('extrafield') ; 149 if (defined $data) { 150 my $bad = IO::Compress::Zlib::Extra::parseExtraField($data, $strict, 1) ; 151 return $self->saveErrorString(undef, "Error with ExtraField Parameter: $bad", Z_DATA_ERROR) 152 if $bad ; 153 154 $got->setValue('extrafield' => $data) ; 155 } 156 } 157 158 return 1; 159} 160 161sub mkTrailer 162{ 163 my $self = shift ; 164 return pack("V V", *$self->{Compress}->crc32(), 165 *$self->{UnCompSize}->get32bit()); 166} 167 168sub getInverseClass 169{ 170 return ('IO::Uncompress::Gunzip', 171 \$IO::Uncompress::Gunzip::GunzipError); 172} 173 174sub getFileInfo 175{ 176 my $self = shift ; 177 my $params = shift; 178 my $filename = shift ; 179 180 return if IO::Compress::Base::Common::isaScalar($filename); 181 182 my $defaultTime = (stat($filename))[9] ; 183 184 $params->setValue('name' => $filename) 185 if ! $params->parsed('name') ; 186 187 $params->setValue('time' => $defaultTime) 188 if ! $params->parsed('time') ; 189} 190 191 192sub mkHeader 193{ 194 my $self = shift ; 195 my $param = shift ; 196 197 # short-circuit if a minimal header is requested. 198 return GZIP_MINIMUM_HEADER if $param->getValue('minimal') ; 199 200 # METHOD 201 my $method = $param->valueOrDefault('method', GZIP_CM_DEFLATED) ; 202 203 # FLAGS 204 my $flags = GZIP_FLG_DEFAULT ; 205 $flags |= GZIP_FLG_FTEXT if $param->getValue('textflag') ; 206 $flags |= GZIP_FLG_FHCRC if $param->getValue('headercrc') ; 207 $flags |= GZIP_FLG_FEXTRA if $param->wantValue('extrafield') ; 208 $flags |= GZIP_FLG_FNAME if $param->wantValue('name') ; 209 $flags |= GZIP_FLG_FCOMMENT if $param->wantValue('comment') ; 210 211 # MTIME 212 my $time = $param->valueOrDefault('time', GZIP_MTIME_DEFAULT) ; 213 214 # EXTRA FLAGS 215 my $extra_flags = $param->valueOrDefault('extraflags', GZIP_XFL_DEFAULT); 216 217 # OS CODE 218 my $os_code = $param->valueOrDefault('os_code', GZIP_OS_DEFAULT) ; 219 220 221 my $out = pack("C4 V C C", 222 GZIP_ID1, # ID1 223 GZIP_ID2, # ID2 224 $method, # Compression Method 225 $flags, # Flags 226 $time, # Modification Time 227 $extra_flags, # Extra Flags 228 $os_code, # Operating System Code 229 ) ; 230 231 # EXTRA 232 if ($flags & GZIP_FLG_FEXTRA) { 233 my $extra = $param->getValue('extrafield') ; 234 $out .= pack("v", length $extra) . $extra ; 235 } 236 237 # NAME 238 if ($flags & GZIP_FLG_FNAME) { 239 my $name .= $param->getValue('name') ; 240 $name =~ s/\x00.*$//; 241 $out .= $name ; 242 # Terminate the filename with NULL unless it already is 243 $out .= GZIP_NULL_BYTE 244 if !length $name or 245 substr($name, 1, -1) ne GZIP_NULL_BYTE ; 246 } 247 248 # COMMENT 249 if ($flags & GZIP_FLG_FCOMMENT) { 250 my $comment .= $param->getValue('comment') ; 251 $comment =~ s/\x00.*$//; 252 $out .= $comment ; 253 # Terminate the comment with NULL unless it already is 254 $out .= GZIP_NULL_BYTE 255 if ! length $comment or 256 substr($comment, 1, -1) ne GZIP_NULL_BYTE; 257 } 258 259 # HEADER CRC 260 $out .= pack("v", Compress::Raw::Zlib::crc32($out) & 0x00FF ) 261 if $param->getValue('headercrc') ; 262 263 noUTF8($out); 264 265 return $out ; 266} 267 268sub mkFinalTrailer 269{ 270 return ''; 271} 272 2731; 274 275__END__ 276 277=head1 NAME 278 279IO::Compress::Gzip - Write RFC 1952 files/buffers 280 281=head1 SYNOPSIS 282 283 use IO::Compress::Gzip qw(gzip $GzipError) ; 284 285 my $status = gzip $input => $output [,OPTS] 286 or die "gzip failed: $GzipError\n"; 287 288 my $z = new IO::Compress::Gzip $output [,OPTS] 289 or die "gzip failed: $GzipError\n"; 290 291 $z->print($string); 292 $z->printf($format, $string); 293 $z->write($string); 294 $z->syswrite($string [, $length, $offset]); 295 $z->flush(); 296 $z->tell(); 297 $z->eof(); 298 $z->seek($position, $whence); 299 $z->binmode(); 300 $z->fileno(); 301 $z->opened(); 302 $z->autoflush(); 303 $z->input_line_number(); 304 $z->newStream( [OPTS] ); 305 306 $z->deflateParams(); 307 308 $z->close() ; 309 310 $GzipError ; 311 312 # IO::File mode 313 314 print $z $string; 315 printf $z $format, $string; 316 tell $z 317 eof $z 318 seek $z, $position, $whence 319 binmode $z 320 fileno $z 321 close $z ; 322 323 324=head1 DESCRIPTION 325 326This module provides a Perl interface that allows writing compressed 327data to files or buffer as defined in RFC 1952. 328 329All the gzip headers defined in RFC 1952 can be created using 330this module. 331 332For reading RFC 1952 files/buffers, see the companion module 333L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>. 334 335=head1 Functional Interface 336 337A top-level function, C<gzip>, is provided to carry out 338"one-shot" compression between buffers and/or files. For finer 339control over the compression process, see the L</"OO Interface"> 340section. 341 342 use IO::Compress::Gzip qw(gzip $GzipError) ; 343 344 gzip $input_filename_or_reference => $output_filename_or_reference [,OPTS] 345 or die "gzip failed: $GzipError\n"; 346 347The functional interface needs Perl5.005 or better. 348 349=head2 gzip $input_filename_or_reference => $output_filename_or_reference [, OPTS] 350 351C<gzip> expects at least two parameters, 352C<$input_filename_or_reference> and C<$output_filename_or_reference>. 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 uncompressed data. 358 359It can take one of the following forms: 360 361=over 5 362 363=item A filename 364 365If the <$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 compressed. 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<gzip> 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 404In addition, if C<$input_filename_or_reference> is a simple filename, 405the default values for 406the C<Name> and C<Time> options will be sourced from that file. 407 408If you do not want to use these defaults they can be overridden by 409explicitly setting the C<Name> and C<Time> options or by setting the 410C<Minimal> parameter. 411 412=head3 The C<$output_filename_or_reference> parameter 413 414The parameter C<$output_filename_or_reference> is used to control the 415destination of the compressed data. This parameter can take one of 416these forms. 417 418=over 5 419 420=item A filename 421 422If the C<$output_filename_or_reference> parameter is a simple scalar, it is 423assumed to be a filename. This file will be opened for writing and the 424compressed data will be written to it. 425 426=item A filehandle 427 428If the C<$output_filename_or_reference> parameter is a filehandle, the 429compressed data will be written to it. The string '-' can be used as 430an alias for standard output. 431 432=item A scalar reference 433 434If C<$output_filename_or_reference> is a scalar reference, the 435compressed data will be stored in C<$$output_filename_or_reference>. 436 437=item An Array Reference 438 439If C<$output_filename_or_reference> is an array reference, 440the compressed data will be pushed onto the array. 441 442=item An Output FileGlob 443 444If C<$output_filename_or_reference> is a string that is delimited by the 445characters "<" and ">" C<gzip> will assume that it is an 446I<output fileglob string>. The output is the list of files that match the 447fileglob. 448 449When C<$output_filename_or_reference> is an fileglob string, 450C<$input_filename_or_reference> must also be a fileglob string. Anything 451else is an error. 452 453See L<File::GlobMapper|File::GlobMapper> for more details. 454 455=back 456 457If the C<$output_filename_or_reference> parameter is any other type, 458C<undef> will be returned. 459 460=head2 Notes 461 462When C<$input_filename_or_reference> maps to multiple files/buffers and 463C<$output_filename_or_reference> is a single 464file/buffer the input files/buffers will be stored 465in C<$output_filename_or_reference> as a concatenated series of compressed data streams. 466 467=head2 Optional Parameters 468 469Unless specified below, the optional parameters for C<gzip>, 470C<OPTS>, are the same as those used with the OO interface defined in the 471L</"Constructor Options"> section below. 472 473=over 5 474 475=item C<< AutoClose => 0|1 >> 476 477This option applies to any input or output data streams to 478C<gzip> that are filehandles. 479 480If C<AutoClose> is specified, and the value is true, it will result in all 481input and/or output filehandles being closed once C<gzip> has 482completed. 483 484This parameter defaults to 0. 485 486=item C<< BinModeIn => 0|1 >> 487 488This option is now a no-op. All files will be read in binmode. 489 490=item C<< Append => 0|1 >> 491 492The behaviour of this option is dependent on the type of output data 493stream. 494 495=over 5 496 497=item * A Buffer 498 499If C<Append> is enabled, all compressed data will be append to the end of 500the output buffer. Otherwise the output buffer will be cleared before any 501compressed data is written to it. 502 503=item * A Filename 504 505If C<Append> is enabled, the file will be opened in append mode. Otherwise 506the contents of the file, if any, will be truncated before any compressed 507data is written to it. 508 509=item * A Filehandle 510 511If C<Append> is enabled, the filehandle will be positioned to the end of 512the file via a call to C<seek> before any compressed data is 513written to it. Otherwise the file pointer will not be moved. 514 515=back 516 517When C<Append> is specified, and set to true, it will I<append> all compressed 518data to the output data stream. 519 520So when the output is a filehandle it will carry out a seek to the eof 521before writing any compressed data. If the output is a filename, it will be opened for 522appending. If the output is a buffer, all compressed data will be 523appended to the existing buffer. 524 525Conversely when C<Append> is not specified, or it is present and is set to 526false, it will operate as follows. 527 528When the output is a filename, it will truncate the contents of the file 529before writing any compressed data. If the output is a filehandle 530its position will not be changed. If the output is a buffer, it will be 531wiped before any compressed data is output. 532 533Defaults to 0. 534 535=back 536 537=head2 Examples 538 539To read the contents of the file C<file1.txt> and write the compressed 540data to the file C<file1.txt.gz>. 541 542 use strict ; 543 use warnings ; 544 use IO::Compress::Gzip qw(gzip $GzipError) ; 545 546 my $input = "file1.txt"; 547 gzip $input => "$input.gz" 548 or die "gzip failed: $GzipError\n"; 549 550To read from an existing Perl filehandle, C<$input>, and write the 551compressed data to a buffer, C<$buffer>. 552 553 use strict ; 554 use warnings ; 555 use IO::Compress::Gzip qw(gzip $GzipError) ; 556 use IO::File ; 557 558 my $input = new IO::File "<file1.txt" 559 or die "Cannot open 'file1.txt': $!\n" ; 560 my $buffer ; 561 gzip $input => \$buffer 562 or die "gzip failed: $GzipError\n"; 563 564To compress all files in the directory "/my/home" that match "*.txt" 565and store the compressed data in the same directory 566 567 use strict ; 568 use warnings ; 569 use IO::Compress::Gzip qw(gzip $GzipError) ; 570 571 gzip '</my/home/*.txt>' => '<*.gz>' 572 or die "gzip failed: $GzipError\n"; 573 574and if you want to compress each file one at a time, this will do the trick 575 576 use strict ; 577 use warnings ; 578 use IO::Compress::Gzip qw(gzip $GzipError) ; 579 580 for my $input ( glob "/my/home/*.txt" ) 581 { 582 my $output = "$input.gz" ; 583 gzip $input => $output 584 or die "Error compressing '$input': $GzipError\n"; 585 } 586 587=head1 OO Interface 588 589=head2 Constructor 590 591The format of the constructor for C<IO::Compress::Gzip> is shown below 592 593 my $z = new IO::Compress::Gzip $output [,OPTS] 594 or die "IO::Compress::Gzip failed: $GzipError\n"; 595 596It returns an C<IO::Compress::Gzip> object on success and undef on failure. 597The variable C<$GzipError> will contain an error message on failure. 598 599If you are running Perl 5.005 or better the object, C<$z>, returned from 600IO::Compress::Gzip can be used exactly like an L<IO::File|IO::File> filehandle. 601This means that all normal output file operations can be carried out 602with C<$z>. 603For example, to write to a compressed file/buffer you can use either of 604these forms 605 606 $z->print("hello world\n"); 607 print $z "hello world\n"; 608 609The mandatory parameter C<$output> is used to control the destination 610of the compressed data. This parameter can take one of these forms. 611 612=over 5 613 614=item A filename 615 616If the C<$output> parameter is a simple scalar, it is assumed to be a 617filename. This file will be opened for writing and the compressed data 618will be written to it. 619 620=item A filehandle 621 622If the C<$output> parameter is a filehandle, the compressed data will be 623written to it. 624The string '-' can be used as an alias for standard output. 625 626=item A scalar reference 627 628If C<$output> is a scalar reference, the compressed data will be stored 629in C<$$output>. 630 631=back 632 633If the C<$output> parameter is any other type, C<IO::Compress::Gzip>::new will 634return undef. 635 636=head2 Constructor Options 637 638C<OPTS> is any combination of the following options: 639 640=over 5 641 642=item C<< AutoClose => 0|1 >> 643 644This option is only valid when the C<$output> parameter is a filehandle. If 645specified, and the value is true, it will result in the C<$output> being 646closed once either the C<close> method is called or the C<IO::Compress::Gzip> 647object is destroyed. 648 649This parameter defaults to 0. 650 651=item C<< Append => 0|1 >> 652 653Opens C<$output> in append mode. 654 655The behaviour of this option is dependent on the type of C<$output>. 656 657=over 5 658 659=item * A Buffer 660 661If C<$output> is a buffer and C<Append> is enabled, all compressed data 662will be append to the end of C<$output>. Otherwise C<$output> will be 663cleared before any data is written to it. 664 665=item * A Filename 666 667If C<$output> is a filename and C<Append> is enabled, the file will be 668opened in append mode. Otherwise the contents of the file, if any, will be 669truncated before any compressed data is written to it. 670 671=item * A Filehandle 672 673If C<$output> is a filehandle, the file pointer will be positioned to the 674end of the file via a call to C<seek> before any compressed data is written 675to it. Otherwise the file pointer will not be moved. 676 677=back 678 679This parameter defaults to 0. 680 681=item C<< Merge => 0|1 >> 682 683This option is used to compress input data and append it to an existing 684compressed data stream in C<$output>. The end result is a single compressed 685data stream stored in C<$output>. 686 687It is a fatal error to attempt to use this option when C<$output> is not an 688RFC 1952 data stream. 689 690There are a number of other limitations with the C<Merge> option: 691 692=over 5 693 694=item 1 695 696This module needs to have been built with zlib 1.2.1 or better to work. A 697fatal error will be thrown if C<Merge> is used with an older version of 698zlib. 699 700=item 2 701 702If C<$output> is a file or a filehandle, it must be seekable. 703 704=back 705 706This parameter defaults to 0. 707 708=item -Level 709 710Defines the compression level used by zlib. The value should either be 711a number between 0 and 9 (0 means no compression and 9 is maximum 712compression), or one of the symbolic constants defined below. 713 714 Z_NO_COMPRESSION 715 Z_BEST_SPEED 716 Z_BEST_COMPRESSION 717 Z_DEFAULT_COMPRESSION 718 719The default is Z_DEFAULT_COMPRESSION. 720 721Note, these constants are not imported by C<IO::Compress::Gzip> by default. 722 723 use IO::Compress::Gzip qw(:strategy); 724 use IO::Compress::Gzip qw(:constants); 725 use IO::Compress::Gzip qw(:all); 726 727=item -Strategy 728 729Defines the strategy used to tune the compression. Use one of the symbolic 730constants defined below. 731 732 Z_FILTERED 733 Z_HUFFMAN_ONLY 734 Z_RLE 735 Z_FIXED 736 Z_DEFAULT_STRATEGY 737 738The default is Z_DEFAULT_STRATEGY. 739 740=item C<< Minimal => 0|1 >> 741 742If specified, this option will force the creation of the smallest possible 743compliant gzip header (which is exactly 10 bytes long) as defined in 744RFC 1952. 745 746See the section titled "Compliance" in RFC 1952 for a definition 747of the values used for the fields in the gzip header. 748 749All other parameters that control the content of the gzip header will 750be ignored if this parameter is set to 1. 751 752This parameter defaults to 0. 753 754=item C<< Comment => $comment >> 755 756Stores the contents of C<$comment> in the COMMENT field in 757the gzip header. 758By default, no comment field is written to the gzip file. 759 760If the C<-Strict> option is enabled, the comment can only consist of ISO 7618859-1 characters plus line feed. 762 763If the C<-Strict> option is disabled, the comment field can contain any 764character except NULL. If any null characters are present, the field 765will be truncated at the first NULL. 766 767=item C<< Name => $string >> 768 769Stores the contents of C<$string> in the gzip NAME header field. If 770C<Name> is not specified, no gzip NAME field will be created. 771 772If the C<-Strict> option is enabled, C<$string> can only consist of ISO 7738859-1 characters. 774 775If C<-Strict> is disabled, then C<$string> can contain any character 776except NULL. If any null characters are present, the field will be 777truncated at the first NULL. 778 779=item C<< Time => $number >> 780 781Sets the MTIME field in the gzip header to $number. 782 783This field defaults to the time the C<IO::Compress::Gzip> object was created 784if this option is not specified. 785 786=item C<< TextFlag => 0|1 >> 787 788This parameter controls the setting of the FLG.FTEXT bit in the gzip 789header. It is used to signal that the data stored in the gzip file/buffer 790is probably text. 791 792The default is 0. 793 794=item C<< HeaderCRC => 0|1 >> 795 796When true this parameter will set the FLG.FHCRC bit to 1 in the gzip header 797and set the CRC16 header field to the CRC of the complete gzip header 798except the CRC16 field itself. 799 800B<Note> that gzip files created with the C<HeaderCRC> flag set to 1 cannot 801be read by most, if not all, of the standard gunzip utilities, most 802notably gzip version 1.2.4. You should therefore avoid using this option if 803you want to maximize the portability of your gzip files. 804 805This parameter defaults to 0. 806 807=item C<< OS_Code => $value >> 808 809Stores C<$value> in the gzip OS header field. A number between 0 and 255 is 810valid. 811 812If not specified, this parameter defaults to the OS code of the Operating 813System this module was built on. The value 3 is used as a catch-all for all 814Unix variants and unknown Operating Systems. 815 816=item C<< ExtraField => $data >> 817 818This parameter allows additional metadata to be stored in the ExtraField in 819the gzip header. An RFC 1952 compliant ExtraField consists of zero or more 820subfields. Each subfield consists of a two byte header followed by the 821subfield data. 822 823The list of subfields can be supplied in any of the following formats 824 825 -ExtraField => [$id1, $data1, 826 $id2, $data2, 827 ... 828 ] 829 -ExtraField => [ [$id1 => $data1], 830 [$id2 => $data2], 831 ... 832 ] 833 -ExtraField => { $id1 => $data1, 834 $id2 => $data2, 835 ... 836 } 837 838Where C<$id1>, C<$id2> are two byte subfield ID's. The second byte of 839the ID cannot be 0, unless the C<Strict> option has been disabled. 840 841If you use the hash syntax, you have no control over the order in which 842the ExtraSubFields are stored, plus you cannot have SubFields with 843duplicate ID. 844 845Alternatively the list of subfields can by supplied as a scalar, thus 846 847 -ExtraField => $rawdata 848 849If you use the raw format, and the C<Strict> option is enabled, 850C<IO::Compress::Gzip> will check that C<$rawdata> consists of zero or more 851conformant sub-fields. When C<Strict> is disabled, C<$rawdata> can 852consist of any arbitrary byte stream. 853 854The maximum size of the Extra Field 65535 bytes. 855 856=item C<< ExtraFlags => $value >> 857 858Sets the XFL byte in the gzip header to C<$value>. 859 860If this option is not present, the value stored in XFL field will be 861determined by the setting of the C<Level> option. 862 863If C<< Level => Z_BEST_SPEED >> has been specified then XFL is set to 2. 864If C<< Level => Z_BEST_COMPRESSION >> has been specified then XFL is set to 4. 865Otherwise XFL is set to 0. 866 867=item C<< Strict => 0|1 >> 868 869C<Strict> will optionally police the values supplied with other options 870to ensure they are compliant with RFC1952. 871 872This option is enabled by default. 873 874If C<Strict> is enabled the following behaviour will be policed: 875 876=over 5 877 878=item * 879 880The value supplied with the C<Name> option can only contain ISO 8859-1 881characters. 882 883=item * 884 885The value supplied with the C<Comment> option can only contain ISO 8859-1 886characters plus line-feed. 887 888=item * 889 890The values supplied with the C<-Name> and C<-Comment> options cannot 891contain multiple embedded nulls. 892 893=item * 894 895If an C<ExtraField> option is specified and it is a simple scalar, 896it must conform to the sub-field structure as defined in RFC 1952. 897 898=item * 899 900If an C<ExtraField> option is specified the second byte of the ID will be 901checked in each subfield to ensure that it does not contain the reserved 902value 0x00. 903 904=back 905 906When C<Strict> is disabled the following behaviour will be policed: 907 908=over 5 909 910=item * 911 912The value supplied with C<-Name> option can contain 913any character except NULL. 914 915=item * 916 917The value supplied with C<-Comment> option can contain any character 918except NULL. 919 920=item * 921 922The values supplied with the C<-Name> and C<-Comment> options can contain 923multiple embedded nulls. The string written to the gzip header will 924consist of the characters up to, but not including, the first embedded 925NULL. 926 927=item * 928 929If an C<ExtraField> option is specified and it is a simple scalar, the 930structure will not be checked. The only error is if the length is too big. 931 932=item * 933 934The ID header in an C<ExtraField> sub-field can consist of any two bytes. 935 936=back 937 938=back 939 940=head2 Examples 941 942TODO 943 944=head1 Methods 945 946=head2 print 947 948Usage is 949 950 $z->print($data) 951 print $z $data 952 953Compresses and outputs the contents of the C<$data> parameter. This 954has the same behaviour as the C<print> built-in. 955 956Returns true if successful. 957 958=head2 printf 959 960Usage is 961 962 $z->printf($format, $data) 963 printf $z $format, $data 964 965Compresses and outputs the contents of the C<$data> parameter. 966 967Returns true if successful. 968 969=head2 syswrite 970 971Usage is 972 973 $z->syswrite $data 974 $z->syswrite $data, $length 975 $z->syswrite $data, $length, $offset 976 977Compresses and outputs the contents of the C<$data> parameter. 978 979Returns the number of uncompressed bytes written, or C<undef> if 980unsuccessful. 981 982=head2 write 983 984Usage is 985 986 $z->write $data 987 $z->write $data, $length 988 $z->write $data, $length, $offset 989 990Compresses and outputs the contents of the C<$data> parameter. 991 992Returns the number of uncompressed bytes written, or C<undef> if 993unsuccessful. 994 995=head2 flush 996 997Usage is 998 999 $z->flush; 1000 $z->flush($flush_type); 1001 1002Flushes any pending compressed data to the output file/buffer. 1003 1004This method takes an optional parameter, C<$flush_type>, that controls 1005how the flushing will be carried out. By default the C<$flush_type> 1006used is C<Z_FINISH>. Other valid values for C<$flush_type> are 1007C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is 1008strongly recommended that you only set the C<flush_type> parameter if 1009you fully understand the implications of what it does - overuse of C<flush> 1010can seriously degrade the level of compression achieved. See the C<zlib> 1011documentation for details. 1012 1013Returns true on success. 1014 1015=head2 tell 1016 1017Usage is 1018 1019 $z->tell() 1020 tell $z 1021 1022Returns the uncompressed file offset. 1023 1024=head2 eof 1025 1026Usage is 1027 1028 $z->eof(); 1029 eof($z); 1030 1031Returns true if the C<close> method has been called. 1032 1033=head2 seek 1034 1035 $z->seek($position, $whence); 1036 seek($z, $position, $whence); 1037 1038Provides a sub-set of the C<seek> functionality, with the restriction 1039that it is only legal to seek forward in the output file/buffer. 1040It is a fatal error to attempt to seek backward. 1041 1042Empty parts of the file/buffer will have NULL (0x00) bytes written to them. 1043 1044The C<$whence> parameter takes one the usual values, namely SEEK_SET, 1045SEEK_CUR or SEEK_END. 1046 1047Returns 1 on success, 0 on failure. 1048 1049=head2 binmode 1050 1051Usage is 1052 1053 $z->binmode 1054 binmode $z ; 1055 1056This is a noop provided for completeness. 1057 1058=head2 opened 1059 1060 $z->opened() 1061 1062Returns true if the object currently refers to a opened file/buffer. 1063 1064=head2 autoflush 1065 1066 my $prev = $z->autoflush() 1067 my $prev = $z->autoflush(EXPR) 1068 1069If the C<$z> object is associated with a file or a filehandle, this method 1070returns the current autoflush setting for the underlying filehandle. If 1071C<EXPR> is present, and is non-zero, it will enable flushing after every 1072write/print operation. 1073 1074If C<$z> is associated with a buffer, this method has no effect and always 1075returns C<undef>. 1076 1077B<Note> that the special variable C<$|> B<cannot> be used to set or 1078retrieve the autoflush setting. 1079 1080=head2 input_line_number 1081 1082 $z->input_line_number() 1083 $z->input_line_number(EXPR) 1084 1085This method always returns C<undef> when compressing. 1086 1087=head2 fileno 1088 1089 $z->fileno() 1090 fileno($z) 1091 1092If the C<$z> object is associated with a file or a filehandle, C<fileno> 1093will return the underlying file descriptor. Once the C<close> method is 1094called C<fileno> will return C<undef>. 1095 1096If the C<$z> object is associated with a buffer, this method will return 1097C<undef>. 1098 1099=head2 close 1100 1101 $z->close() ; 1102 close $z ; 1103 1104Flushes any pending compressed data and then closes the output file/buffer. 1105 1106For most versions of Perl this method will be automatically invoked if 1107the IO::Compress::Gzip object is destroyed (either explicitly or by the 1108variable with the reference to the object going out of scope). The 1109exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In 1110these cases, the C<close> method will be called automatically, but 1111not until global destruction of all live objects when the program is 1112terminating. 1113 1114Therefore, if you want your scripts to be able to run on all versions 1115of Perl, you should call C<close> explicitly and not rely on automatic 1116closing. 1117 1118Returns true on success, otherwise 0. 1119 1120If the C<AutoClose> option has been enabled when the IO::Compress::Gzip 1121object was created, and the object is associated with a file, the 1122underlying file will also be closed. 1123 1124=head2 newStream([OPTS]) 1125 1126Usage is 1127 1128 $z->newStream( [OPTS] ) 1129 1130Closes the current compressed data stream and starts a new one. 1131 1132OPTS consists of any of the options that are available when creating 1133the C<$z> object. 1134 1135See the L</"Constructor Options"> section for more details. 1136 1137=head2 deflateParams 1138 1139Usage is 1140 1141 $z->deflateParams 1142 1143TODO 1144 1145=head1 Importing 1146 1147A number of symbolic constants are required by some methods in 1148C<IO::Compress::Gzip>. None are imported by default. 1149 1150=over 5 1151 1152=item :all 1153 1154Imports C<gzip>, C<$GzipError> and all symbolic 1155constants that can be used by C<IO::Compress::Gzip>. Same as doing this 1156 1157 use IO::Compress::Gzip qw(gzip $GzipError :constants) ; 1158 1159=item :constants 1160 1161Import all symbolic constants. Same as doing this 1162 1163 use IO::Compress::Gzip qw(:flush :level :strategy) ; 1164 1165=item :flush 1166 1167These symbolic constants are used by the C<flush> method. 1168 1169 Z_NO_FLUSH 1170 Z_PARTIAL_FLUSH 1171 Z_SYNC_FLUSH 1172 Z_FULL_FLUSH 1173 Z_FINISH 1174 Z_BLOCK 1175 1176=item :level 1177 1178These symbolic constants are used by the C<Level> option in the constructor. 1179 1180 Z_NO_COMPRESSION 1181 Z_BEST_SPEED 1182 Z_BEST_COMPRESSION 1183 Z_DEFAULT_COMPRESSION 1184 1185=item :strategy 1186 1187These symbolic constants are used by the C<Strategy> option in the constructor. 1188 1189 Z_FILTERED 1190 Z_HUFFMAN_ONLY 1191 Z_RLE 1192 Z_FIXED 1193 Z_DEFAULT_STRATEGY 1194 1195=back 1196 1197=head1 EXAMPLES 1198 1199=head2 Apache::GZip Revisited 1200 1201See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited"> 1202 1203=head2 Working with Net::FTP 1204 1205See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP"> 1206 1207=head1 SEE ALSO 1208 1209L<Compress::Zlib>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::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> 1210 1211L<IO::Compress::FAQ|IO::Compress::FAQ> 1212 1213L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, 1214L<Archive::Tar|Archive::Tar>, 1215L<IO::Zlib|IO::Zlib> 1216 1217For RFC 1950, 1951 and 1952 see 1218L<http://www.faqs.org/rfcs/rfc1950.html>, 1219L<http://www.faqs.org/rfcs/rfc1951.html> and 1220L<http://www.faqs.org/rfcs/rfc1952.html> 1221 1222The I<zlib> compression library was written by Jean-loup Gailly 1223C<gzip@prep.ai.mit.edu> and Mark Adler C<madler@alumni.caltech.edu>. 1224 1225The primary site for the I<zlib> compression library is 1226L<http://www.zlib.org>. 1227 1228The primary site for gzip is L<http://www.gzip.org>. 1229 1230=head1 AUTHOR 1231 1232This module was written by Paul Marquess, C<pmqs@cpan.org>. 1233 1234=head1 MODIFICATION HISTORY 1235 1236See the Changes file. 1237 1238=head1 COPYRIGHT AND LICENSE 1239 1240Copyright (c) 2005-2019 Paul Marquess. All rights reserved. 1241 1242This program is free software; you can redistribute it and/or 1243modify it under the same terms as Perl itself. 1244 1245