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