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