xref: /openbsd-src/gnu/usr.bin/perl/cpan/IO-Compress/lib/IO/Compress/Gzip.pm (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1package IO::Compress::Gzip ;
2
3require 5.006 ;
4
5use strict ;
6use warnings;
7use bytes;
8
9require Exporter ;
10
11use IO::Compress::RawDeflate 2.060 () ;
12use IO::Compress::Adapter::Deflate 2.060 ;
13
14use IO::Compress::Base::Common  2.060 qw(:Status );
15use IO::Compress::Gzip::Constants 2.060 ;
16use IO::Compress::Zlib::Extra 2.060 ;
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.060';
29$GzipError = '' ;
30
31@ISA    = qw(Exporter IO::Compress::RawDeflate);
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    # stort-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 ) if $param->getValue('headercrc') ;
261
262    noUTF8($out);
263
264    return $out ;
265}
266
267sub mkFinalTrailer
268{
269    return '';
270}
271
2721;
273
274__END__
275
276=head1 NAME
277
278IO::Compress::Gzip - Write RFC 1952 files/buffers
279
280
281
282=head1 SYNOPSIS
283
284    use IO::Compress::Gzip qw(gzip $GzipError) ;
285
286    my $status = gzip $input => $output [,OPTS]
287        or die "gzip failed: $GzipError\n";
288
289    my $z = new IO::Compress::Gzip $output [,OPTS]
290        or die "gzip failed: $GzipError\n";
291
292    $z->print($string);
293    $z->printf($format, $string);
294    $z->write($string);
295    $z->syswrite($string [, $length, $offset]);
296    $z->flush();
297    $z->tell();
298    $z->eof();
299    $z->seek($position, $whence);
300    $z->binmode();
301    $z->fileno();
302    $z->opened();
303    $z->autoflush();
304    $z->input_line_number();
305    $z->newStream( [OPTS] );
306
307    $z->deflateParams();
308
309    $z->close() ;
310
311    $GzipError ;
312
313    # IO::File mode
314
315    print $z $string;
316    printf $z $format, $string;
317    tell $z
318    eof $z
319    seek $z, $position, $whence
320    binmode $z
321    fileno $z
322    close $z ;
323
324
325=head1 DESCRIPTION
326
327This module provides a Perl interface that allows writing compressed
328data to files or buffer as defined in RFC 1952.
329
330All the gzip headers defined in RFC 1952 can be created using
331this module.
332
333For reading RFC 1952 files/buffers, see the companion module
334L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>.
335
336=head1 Functional Interface
337
338A top-level function, C<gzip>, is provided to carry out
339"one-shot" compression between buffers and/or files. For finer
340control over the compression process, see the L</"OO Interface">
341section.
342
343    use IO::Compress::Gzip qw(gzip $GzipError) ;
344
345    gzip $input_filename_or_reference => $output_filename_or_reference [,OPTS]
346        or die "gzip failed: $GzipError\n";
347
348The functional interface needs Perl5.005 or better.
349
350=head2 gzip $input => $output [, OPTS]
351
352C<gzip> expects at least two parameters,
353C<$input_filename_or_reference> and C<$output_filename_or_reference>.
354
355=head3 The C<$input_filename_or_reference> parameter
356
357The parameter, C<$input_filename_or_reference>, is used to define the
358source of the uncompressed data.
359
360It can take one of the following forms:
361
362=over 5
363
364=item A filename
365
366If the <$input_filename_or_reference> parameter is a simple scalar, it is
367assumed to be a filename. This file will be opened for reading and the
368input data will be read from it.
369
370=item A filehandle
371
372If the C<$input_filename_or_reference> parameter is a filehandle, the input
373data will be read from it.  The string '-' can be used as an alias for
374standard input.
375
376=item A scalar reference
377
378If C<$input_filename_or_reference> is a scalar reference, the input data
379will be read from C<$$input_filename_or_reference>.
380
381=item An array reference
382
383If C<$input_filename_or_reference> is an array reference, each element in
384the array must be a filename.
385
386The input data will be read from each file in turn.
387
388The complete array will be walked to ensure that it only
389contains valid filenames before any data is compressed.
390
391=item An Input FileGlob string
392
393If C<$input_filename_or_reference> is a string that is delimited by the
394characters "<" and ">" C<gzip> will assume that it is an
395I<input fileglob string>. The input is the list of files that match the
396fileglob.
397
398See L<File::GlobMapper|File::GlobMapper> for more details.
399
400=back
401
402If the C<$input_filename_or_reference> parameter is any other type,
403C<undef> will be returned.
404
405In addition, if C<$input_filename_or_reference> is a simple filename,
406the default values for
407the C<Name> and C<Time> options will be sourced from that file.
408
409If you do not want to use these defaults they can be overridden by
410explicitly setting the C<Name> and C<Time> options or by setting the
411C<Minimal> parameter.
412
413=head3 The C<$output_filename_or_reference> parameter
414
415The parameter C<$output_filename_or_reference> is used to control the
416destination of the compressed data. This parameter can take one of
417these forms.
418
419=over 5
420
421=item A filename
422
423If the C<$output_filename_or_reference> parameter is a simple scalar, it is
424assumed to be a filename.  This file will be opened for writing and the
425compressed data will be written to it.
426
427=item A filehandle
428
429If the C<$output_filename_or_reference> parameter is a filehandle, the
430compressed data will be written to it.  The string '-' can be used as
431an alias for standard output.
432
433=item A scalar reference
434
435If C<$output_filename_or_reference> is a scalar reference, the
436compressed data will be stored in C<$$output_filename_or_reference>.
437
438=item An Array Reference
439
440If C<$output_filename_or_reference> is an array reference,
441the compressed data will be pushed onto the array.
442
443=item An Output FileGlob
444
445If C<$output_filename_or_reference> is a string that is delimited by the
446characters "<" and ">" C<gzip> will assume that it is an
447I<output fileglob string>. The output is the list of files that match the
448fileglob.
449
450When C<$output_filename_or_reference> is an fileglob string,
451C<$input_filename_or_reference> must also be a fileglob string. Anything
452else is an error.
453
454See L<File::GlobMapper|File::GlobMapper> for more details.
455
456=back
457
458If the C<$output_filename_or_reference> parameter is any other type,
459C<undef> will be returned.
460
461=head2 Notes
462
463When C<$input_filename_or_reference> maps to multiple files/buffers and
464C<$output_filename_or_reference> is a single
465file/buffer the input files/buffers will be stored
466in C<$output_filename_or_reference> as a concatenated series of compressed data streams.
467
468=head2 Optional Parameters
469
470Unless specified below, the optional parameters for C<gzip>,
471C<OPTS>, are the same as those used with the OO interface defined in the
472L</"Constructor Options"> section below.
473
474=over 5
475
476=item C<< AutoClose => 0|1 >>
477
478This option applies to any input or output data streams to
479C<gzip> that are filehandles.
480
481If C<AutoClose> is specified, and the value is true, it will result in all
482input and/or output filehandles being closed once C<gzip> has
483completed.
484
485This parameter defaults to 0.
486
487=item C<< BinModeIn => 0|1 >>
488
489When reading from a file or filehandle, set C<binmode> before reading.
490
491Defaults to 0.
492
493=item C<< Append => 0|1 >>
494
495The behaviour of this option is dependent on the type of output data
496stream.
497
498=over 5
499
500=item * A Buffer
501
502If C<Append> is enabled, all compressed data will be append to the end of
503the output buffer. Otherwise the output buffer will be cleared before any
504compressed data is written to it.
505
506=item * A Filename
507
508If C<Append> is enabled, the file will be opened in append mode. Otherwise
509the contents of the file, if any, will be truncated before any compressed
510data is written to it.
511
512=item * A Filehandle
513
514If C<Append> is enabled, the filehandle will be positioned to the end of
515the file via a call to C<seek> before any compressed data is
516written to it.  Otherwise the file pointer will not be moved.
517
518=back
519
520When C<Append> is specified, and set to true, it will I<append> all compressed
521data to the output data stream.
522
523So when the output is a filehandle it will carry out a seek to the eof
524before writing any compressed data. If the output is a filename, it will be opened for
525appending. If the output is a buffer, all compressed data will be
526appended to the existing buffer.
527
528Conversely when C<Append> is not specified, or it is present and is set to
529false, it will operate as follows.
530
531When the output is a filename, it will truncate the contents of the file
532before writing any compressed data. If the output is a filehandle
533its position will not be changed. If the output is a buffer, it will be
534wiped before any compressed data is output.
535
536Defaults to 0.
537
538=back
539
540=head2 Examples
541
542To read the contents of the file C<file1.txt> and write the compressed
543data to the file C<file1.txt.gz>.
544
545    use strict ;
546    use warnings ;
547    use IO::Compress::Gzip qw(gzip $GzipError) ;
548
549    my $input = "file1.txt";
550    gzip $input => "$input.gz"
551        or die "gzip failed: $GzipError\n";
552
553To read from an existing Perl filehandle, C<$input>, and write the
554compressed data to a buffer, C<$buffer>.
555
556    use strict ;
557    use warnings ;
558    use IO::Compress::Gzip qw(gzip $GzipError) ;
559    use IO::File ;
560
561    my $input = new IO::File "<file1.txt"
562        or die "Cannot open 'file1.txt': $!\n" ;
563    my $buffer ;
564    gzip $input => \$buffer
565        or die "gzip failed: $GzipError\n";
566
567To compress all files in the directory "/my/home" that match "*.txt"
568and store the compressed data in the same directory
569
570    use strict ;
571    use warnings ;
572    use IO::Compress::Gzip qw(gzip $GzipError) ;
573
574    gzip '</my/home/*.txt>' => '<*.gz>'
575        or die "gzip failed: $GzipError\n";
576
577and if you want to compress each file one at a time, this will do the trick
578
579    use strict ;
580    use warnings ;
581    use IO::Compress::Gzip qw(gzip $GzipError) ;
582
583    for my $input ( glob "/my/home/*.txt" )
584    {
585        my $output = "$input.gz" ;
586        gzip $input => $output
587            or die "Error compressing '$input': $GzipError\n";
588    }
589
590=head1 OO Interface
591
592=head2 Constructor
593
594The format of the constructor for C<IO::Compress::Gzip> is shown below
595
596    my $z = new IO::Compress::Gzip $output [,OPTS]
597        or die "IO::Compress::Gzip failed: $GzipError\n";
598
599It returns an C<IO::Compress::Gzip> object on success and undef on failure.
600The variable C<$GzipError> will contain an error message on failure.
601
602If you are running Perl 5.005 or better the object, C<$z>, returned from
603IO::Compress::Gzip can be used exactly like an L<IO::File|IO::File> filehandle.
604This means that all normal output file operations can be carried out
605with C<$z>.
606For example, to write to a compressed file/buffer you can use either of
607these forms
608
609    $z->print("hello world\n");
610    print $z "hello world\n";
611
612The mandatory parameter C<$output> is used to control the destination
613of the compressed data. This parameter can take one of these forms.
614
615=over 5
616
617=item A filename
618
619If the C<$output> parameter is a simple scalar, it is assumed to be a
620filename. This file will be opened for writing and the compressed data
621will be written to it.
622
623=item A filehandle
624
625If the C<$output> parameter is a filehandle, the compressed data will be
626written to it.
627The string '-' can be used as an alias for standard output.
628
629=item A scalar reference
630
631If C<$output> is a scalar reference, the compressed data will be stored
632in C<$$output>.
633
634=back
635
636If the C<$output> parameter is any other type, C<IO::Compress::Gzip>::new will
637return undef.
638
639=head2 Constructor Options
640
641C<OPTS> is any combination of the following options:
642
643=over 5
644
645=item C<< AutoClose => 0|1 >>
646
647This option is only valid when the C<$output> parameter is a filehandle. If
648specified, and the value is true, it will result in the C<$output> being
649closed once either the C<close> method is called or the C<IO::Compress::Gzip>
650object is destroyed.
651
652This parameter defaults to 0.
653
654=item C<< Append => 0|1 >>
655
656Opens C<$output> in append mode.
657
658The behaviour of this option is dependent on the type of C<$output>.
659
660=over 5
661
662=item * A Buffer
663
664If C<$output> is a buffer and C<Append> is enabled, all compressed data
665will be append to the end of C<$output>. Otherwise C<$output> will be
666cleared before any data is written to it.
667
668=item * A Filename
669
670If C<$output> is a filename and C<Append> is enabled, the file will be
671opened in append mode. Otherwise the contents of the file, if any, will be
672truncated before any compressed data is written to it.
673
674=item * A Filehandle
675
676If C<$output> is a filehandle, the file pointer will be positioned to the
677end of the file via a call to C<seek> before any compressed data is written
678to it.  Otherwise the file pointer will not be moved.
679
680=back
681
682This parameter defaults to 0.
683
684=item C<< Merge => 0|1 >>
685
686This option is used to compress input data and append it to an existing
687compressed data stream in C<$output>. The end result is a single compressed
688data stream stored in C<$output>.
689
690It is a fatal error to attempt to use this option when C<$output> is not an
691RFC 1952 data stream.
692
693There are a number of other limitations with the C<Merge> option:
694
695=over 5
696
697=item 1
698
699This module needs to have been built with zlib 1.2.1 or better to work. A
700fatal error will be thrown if C<Merge> is used with an older version of
701zlib.
702
703=item 2
704
705If C<$output> is a file or a filehandle, it must be seekable.
706
707=back
708
709This parameter defaults to 0.
710
711=item -Level
712
713Defines the compression level used by zlib. The value should either be
714a number between 0 and 9 (0 means no compression and 9 is maximum
715compression), or one of the symbolic constants defined below.
716
717   Z_NO_COMPRESSION
718   Z_BEST_SPEED
719   Z_BEST_COMPRESSION
720   Z_DEFAULT_COMPRESSION
721
722The default is Z_DEFAULT_COMPRESSION.
723
724Note, these constants are not imported by C<IO::Compress::Gzip> by default.
725
726    use IO::Compress::Gzip qw(:strategy);
727    use IO::Compress::Gzip qw(:constants);
728    use IO::Compress::Gzip qw(:all);
729
730=item -Strategy
731
732Defines the strategy used to tune the compression. Use one of the symbolic
733constants defined below.
734
735   Z_FILTERED
736   Z_HUFFMAN_ONLY
737   Z_RLE
738   Z_FIXED
739   Z_DEFAULT_STRATEGY
740
741The default is Z_DEFAULT_STRATEGY.
742
743=item C<< Minimal => 0|1 >>
744
745If specified, this option will force the creation of the smallest possible
746compliant gzip header (which is exactly 10 bytes long) as defined in
747RFC 1952.
748
749See the section titled "Compliance" in RFC 1952 for a definition
750of the values used for the fields in the gzip header.
751
752All other parameters that control the content of the gzip header will
753be ignored if this parameter is set to 1.
754
755This parameter defaults to 0.
756
757=item C<< Comment => $comment >>
758
759Stores the contents of C<$comment> in the COMMENT field in
760the gzip header.
761By default, no comment field is written to the gzip file.
762
763If the C<-Strict> option is enabled, the comment can only consist of ISO
7648859-1 characters plus line feed.
765
766If the C<-Strict> option is disabled, the comment field can contain any
767character except NULL. If any null characters are present, the field
768will be truncated at the first NULL.
769
770=item C<< Name => $string >>
771
772Stores the contents of C<$string> in the gzip NAME header field. If
773C<Name> is not specified, no gzip NAME field will be created.
774
775If the C<-Strict> option is enabled, C<$string> can only consist of ISO
7768859-1 characters.
777
778If C<-Strict> is disabled, then C<$string> can contain any character
779except NULL. If any null characters are present, the field will be
780truncated at the first NULL.
781
782=item C<< Time => $number >>
783
784Sets the MTIME field in the gzip header to $number.
785
786This field defaults to the time the C<IO::Compress::Gzip> object was created
787if this option is not specified.
788
789=item C<< TextFlag => 0|1 >>
790
791This parameter controls the setting of the FLG.FTEXT bit in the gzip
792header. It is used to signal that the data stored in the gzip file/buffer
793is probably text.
794
795The default is 0.
796
797=item C<< HeaderCRC => 0|1 >>
798
799When true this parameter will set the FLG.FHCRC bit to 1 in the gzip header
800and set the CRC16 header field to the CRC of the complete gzip header
801except the CRC16 field itself.
802
803B<Note> that gzip files created with the C<HeaderCRC> flag set to 1 cannot
804be read by most, if not all, of the the standard gunzip utilities, most
805notably gzip version 1.2.4. You should therefore avoid using this option if
806you want to maximize the portability of your gzip files.
807
808This parameter defaults to 0.
809
810=item C<< OS_Code => $value >>
811
812Stores C<$value> in the gzip OS header field. A number between 0 and 255 is
813valid.
814
815If not specified, this parameter defaults to the OS code of the Operating
816System this module was built on. The value 3 is used as a catch-all for all
817Unix variants and unknown Operating Systems.
818
819=item C<< ExtraField => $data >>
820
821This parameter allows additional metadata to be stored in the ExtraField in
822the gzip header. An RFC 1952 compliant ExtraField consists of zero or more
823subfields. Each subfield consists of a two byte header followed by the
824subfield data.
825
826The list of subfields can be supplied in any of the following formats
827
828    -ExtraField => [$id1, $data1,
829                    $id2, $data2,
830                     ...
831                   ]
832    -ExtraField => [ [$id1 => $data1],
833                     [$id2 => $data2],
834                     ...
835                   ]
836    -ExtraField => { $id1 => $data1,
837                     $id2 => $data2,
838                     ...
839                   }
840
841Where C<$id1>, C<$id2> are two byte subfield ID's. The second byte of
842the ID cannot be 0, unless the C<Strict> option has been disabled.
843
844If you use the hash syntax, you have no control over the order in which
845the ExtraSubFields are stored, plus you cannot have SubFields with
846duplicate ID.
847
848Alternatively the list of subfields can by supplied as a scalar, thus
849
850    -ExtraField => $rawdata
851
852If you use the raw format, and the C<Strict> option is enabled,
853C<IO::Compress::Gzip> will check that C<$rawdata> consists of zero or more
854conformant sub-fields. When C<Strict> is disabled, C<$rawdata> can
855consist of any arbitrary byte stream.
856
857The maximum size of the Extra Field 65535 bytes.
858
859=item C<< ExtraFlags => $value >>
860
861Sets the XFL byte in the gzip header to C<$value>.
862
863If this option is not present, the value stored in XFL field will be
864determined by the setting of the C<Level> option.
865
866If C<< Level => Z_BEST_SPEED >> has been specified then XFL is set to 2.
867If C<< Level => Z_BEST_COMPRESSION >> has been specified then XFL is set to 4.
868Otherwise XFL is set to 0.
869
870=item C<< Strict => 0|1 >>
871
872C<Strict> will optionally police the values supplied with other options
873to ensure they are compliant with RFC1952.
874
875This option is enabled by default.
876
877If C<Strict> is enabled the following behaviour will be policed:
878
879=over 5
880
881=item *
882
883The value supplied with the C<Name> option can only contain ISO 8859-1
884characters.
885
886=item *
887
888The value supplied with the C<Comment> option can only contain ISO 8859-1
889characters plus line-feed.
890
891=item *
892
893The values supplied with the C<-Name> and C<-Comment> options cannot
894contain multiple embedded nulls.
895
896=item *
897
898If an C<ExtraField> option is specified and it is a simple scalar,
899it must conform to the sub-field structure as defined in RFC 1952.
900
901=item *
902
903If an C<ExtraField> option is specified the second byte of the ID will be
904checked in each subfield to ensure that it does not contain the reserved
905value 0x00.
906
907=back
908
909When C<Strict> is disabled the following behaviour will be policed:
910
911=over 5
912
913=item *
914
915The value supplied with C<-Name> option can contain
916any character except NULL.
917
918=item *
919
920The value supplied with C<-Comment> option can contain any character
921except NULL.
922
923=item *
924
925The values supplied with the C<-Name> and C<-Comment> options can contain
926multiple embedded nulls. The string written to the gzip header will
927consist of the characters up to, but not including, the first embedded
928NULL.
929
930=item *
931
932If an C<ExtraField> option is specified and it is a simple scalar, the
933structure will not be checked. The only error is if the length is too big.
934
935=item *
936
937The ID header in an C<ExtraField> sub-field can consist of any two bytes.
938
939=back
940
941=back
942
943=head2 Examples
944
945TODO
946
947=head1 Methods
948
949=head2 print
950
951Usage is
952
953    $z->print($data)
954    print $z $data
955
956Compresses and outputs the contents of the C<$data> parameter. This
957has the same behaviour as the C<print> built-in.
958
959Returns true if successful.
960
961=head2 printf
962
963Usage is
964
965    $z->printf($format, $data)
966    printf $z $format, $data
967
968Compresses and outputs the contents of the C<$data> parameter.
969
970Returns true if successful.
971
972=head2 syswrite
973
974Usage is
975
976    $z->syswrite $data
977    $z->syswrite $data, $length
978    $z->syswrite $data, $length, $offset
979
980Compresses and outputs the contents of the C<$data> parameter.
981
982Returns the number of uncompressed bytes written, or C<undef> if
983unsuccessful.
984
985=head2 write
986
987Usage is
988
989    $z->write $data
990    $z->write $data, $length
991    $z->write $data, $length, $offset
992
993Compresses and outputs the contents of the C<$data> parameter.
994
995Returns the number of uncompressed bytes written, or C<undef> if
996unsuccessful.
997
998=head2 flush
999
1000Usage is
1001
1002    $z->flush;
1003    $z->flush($flush_type);
1004
1005Flushes any pending compressed data to the output file/buffer.
1006
1007This method takes an optional parameter, C<$flush_type>, that controls
1008how the flushing will be carried out. By default the C<$flush_type>
1009used is C<Z_FINISH>. Other valid values for C<$flush_type> are
1010C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
1011strongly recommended that you only set the C<flush_type> parameter if
1012you fully understand the implications of what it does - overuse of C<flush>
1013can seriously degrade the level of compression achieved. See the C<zlib>
1014documentation for details.
1015
1016Returns true on success.
1017
1018=head2 tell
1019
1020Usage is
1021
1022    $z->tell()
1023    tell $z
1024
1025Returns the uncompressed file offset.
1026
1027=head2 eof
1028
1029Usage is
1030
1031    $z->eof();
1032    eof($z);
1033
1034Returns true if the C<close> method has been called.
1035
1036=head2 seek
1037
1038    $z->seek($position, $whence);
1039    seek($z, $position, $whence);
1040
1041Provides a sub-set of the C<seek> functionality, with the restriction
1042that it is only legal to seek forward in the output file/buffer.
1043It is a fatal error to attempt to seek backward.
1044
1045Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
1046
1047The C<$whence> parameter takes one the usual values, namely SEEK_SET,
1048SEEK_CUR or SEEK_END.
1049
1050Returns 1 on success, 0 on failure.
1051
1052=head2 binmode
1053
1054Usage is
1055
1056    $z->binmode
1057    binmode $z ;
1058
1059This is a noop provided for completeness.
1060
1061=head2 opened
1062
1063    $z->opened()
1064
1065Returns true if the object currently refers to a opened file/buffer.
1066
1067=head2 autoflush
1068
1069    my $prev = $z->autoflush()
1070    my $prev = $z->autoflush(EXPR)
1071
1072If the C<$z> object is associated with a file or a filehandle, this method
1073returns the current autoflush setting for the underlying filehandle. If
1074C<EXPR> is present, and is non-zero, it will enable flushing after every
1075write/print operation.
1076
1077If C<$z> is associated with a buffer, this method has no effect and always
1078returns C<undef>.
1079
1080B<Note> that the special variable C<$|> B<cannot> be used to set or
1081retrieve the autoflush setting.
1082
1083=head2 input_line_number
1084
1085    $z->input_line_number()
1086    $z->input_line_number(EXPR)
1087
1088This method always returns C<undef> when compressing.
1089
1090=head2 fileno
1091
1092    $z->fileno()
1093    fileno($z)
1094
1095If the C<$z> object is associated with a file or a filehandle, C<fileno>
1096will return the underlying file descriptor. Once the C<close> method is
1097called C<fileno> will return C<undef>.
1098
1099If the C<$z> object is associated with a buffer, this method will return
1100C<undef>.
1101
1102=head2 close
1103
1104    $z->close() ;
1105    close $z ;
1106
1107Flushes any pending compressed data and then closes the output file/buffer.
1108
1109For most versions of Perl this method will be automatically invoked if
1110the IO::Compress::Gzip object is destroyed (either explicitly or by the
1111variable with the reference to the object going out of scope). The
1112exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
1113these cases, the C<close> method will be called automatically, but
1114not until global destruction of all live objects when the program is
1115terminating.
1116
1117Therefore, if you want your scripts to be able to run on all versions
1118of Perl, you should call C<close> explicitly and not rely on automatic
1119closing.
1120
1121Returns true on success, otherwise 0.
1122
1123If the C<AutoClose> option has been enabled when the IO::Compress::Gzip
1124object was created, and the object is associated with a file, the
1125underlying file will also be closed.
1126
1127=head2 newStream([OPTS])
1128
1129Usage is
1130
1131    $z->newStream( [OPTS] )
1132
1133Closes the current compressed data stream and starts a new one.
1134
1135OPTS consists of any of the the options that are available when creating
1136the C<$z> object.
1137
1138See the L</"Constructor Options"> section for more details.
1139
1140=head2 deflateParams
1141
1142Usage is
1143
1144    $z->deflateParams
1145
1146TODO
1147
1148=head1 Importing
1149
1150A number of symbolic constants are required by some methods in
1151C<IO::Compress::Gzip>. None are imported by default.
1152
1153=over 5
1154
1155=item :all
1156
1157Imports C<gzip>, C<$GzipError> and all symbolic
1158constants that can be used by C<IO::Compress::Gzip>. Same as doing this
1159
1160    use IO::Compress::Gzip qw(gzip $GzipError :constants) ;
1161
1162=item :constants
1163
1164Import all symbolic constants. Same as doing this
1165
1166    use IO::Compress::Gzip qw(:flush :level :strategy) ;
1167
1168=item :flush
1169
1170These symbolic constants are used by the C<flush> method.
1171
1172    Z_NO_FLUSH
1173    Z_PARTIAL_FLUSH
1174    Z_SYNC_FLUSH
1175    Z_FULL_FLUSH
1176    Z_FINISH
1177    Z_BLOCK
1178
1179=item :level
1180
1181These symbolic constants are used by the C<Level> option in the constructor.
1182
1183    Z_NO_COMPRESSION
1184    Z_BEST_SPEED
1185    Z_BEST_COMPRESSION
1186    Z_DEFAULT_COMPRESSION
1187
1188=item :strategy
1189
1190These symbolic constants are used by the C<Strategy> option in the constructor.
1191
1192    Z_FILTERED
1193    Z_HUFFMAN_ONLY
1194    Z_RLE
1195    Z_FIXED
1196    Z_DEFAULT_STRATEGY
1197
1198
1199
1200
1201=back
1202
1203=head1 EXAMPLES
1204
1205=head2 Apache::GZip Revisited
1206
1207See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited">
1208
1209=head2 Working with Net::FTP
1210
1211See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP">
1212
1213=head1 SEE ALSO
1214
1215L<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>
1216
1217L<IO::Compress::FAQ|IO::Compress::FAQ>
1218
1219L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1220L<Archive::Tar|Archive::Tar>,
1221L<IO::Zlib|IO::Zlib>
1222
1223For RFC 1950, 1951 and 1952 see
1224F<http://www.faqs.org/rfcs/rfc1950.html>,
1225F<http://www.faqs.org/rfcs/rfc1951.html> and
1226F<http://www.faqs.org/rfcs/rfc1952.html>
1227
1228The I<zlib> compression library was written by Jean-loup Gailly
1229F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1230
1231The primary site for the I<zlib> compression library is
1232F<http://www.zlib.org>.
1233
1234The primary site for gzip is F<http://www.gzip.org>.
1235
1236=head1 AUTHOR
1237
1238This module was written by Paul Marquess, F<pmqs@cpan.org>.
1239
1240=head1 MODIFICATION HISTORY
1241
1242See the Changes file.
1243
1244=head1 COPYRIGHT AND LICENSE
1245
1246Copyright (c) 2005-2013 Paul Marquess. All rights reserved.
1247
1248This program is free software; you can redistribute it and/or
1249modify it under the same terms as Perl itself.
1250
1251