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