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