File Coverage

blib/lib/IO/Compress/Gzip.pm
Criterion Covered Total %
statement 106 107 99.0
branch 59 62 95.1
condition 24 27 88.8
subroutine 19 19 100.0
pod 2 9 22.2
total 210 224 93.7


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