Commit | Line | Data |
3fea05b9 |
1 | package IO::Compress::RawDeflate ; |
2 | |
3 | # create RFC1951 |
4 | # |
5 | use strict ; |
6 | use warnings; |
7 | use bytes; |
8 | |
9 | |
10 | use IO::Compress::Base 2.023 ; |
11 | use IO::Compress::Base::Common 2.023 qw(:Status createSelfTiedObject); |
12 | use IO::Compress::Adapter::Deflate 2.023 ; |
13 | |
14 | require Exporter ; |
15 | |
16 | |
17 | our ($VERSION, @ISA, @EXPORT_OK, %DEFLATE_CONSTANTS, %EXPORT_TAGS, $RawDeflateError); |
18 | |
19 | $VERSION = '2.023'; |
20 | $RawDeflateError = ''; |
21 | |
22 | @ISA = qw(Exporter IO::Compress::Base); |
23 | @EXPORT_OK = qw( $RawDeflateError rawdeflate ) ; |
24 | |
25 | %EXPORT_TAGS = ( flush => [qw{ |
26 | Z_NO_FLUSH |
27 | Z_PARTIAL_FLUSH |
28 | Z_SYNC_FLUSH |
29 | Z_FULL_FLUSH |
30 | Z_FINISH |
31 | Z_BLOCK |
32 | }], |
33 | level => [qw{ |
34 | Z_NO_COMPRESSION |
35 | Z_BEST_SPEED |
36 | Z_BEST_COMPRESSION |
37 | Z_DEFAULT_COMPRESSION |
38 | }], |
39 | strategy => [qw{ |
40 | Z_FILTERED |
41 | Z_HUFFMAN_ONLY |
42 | Z_RLE |
43 | Z_FIXED |
44 | Z_DEFAULT_STRATEGY |
45 | }], |
46 | |
47 | ); |
48 | |
49 | { |
50 | my %seen; |
51 | foreach (keys %EXPORT_TAGS ) |
52 | { |
53 | push @{$EXPORT_TAGS{constants}}, |
54 | grep { !$seen{$_}++ } |
55 | @{ $EXPORT_TAGS{$_} } |
56 | } |
57 | $EXPORT_TAGS{all} = $EXPORT_TAGS{constants} ; |
58 | } |
59 | |
60 | |
61 | %DEFLATE_CONSTANTS = %EXPORT_TAGS; |
62 | |
63 | push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; |
64 | |
65 | Exporter::export_ok_tags('all'); |
66 | |
67 | |
68 | |
69 | sub new |
70 | { |
71 | my $class = shift ; |
72 | |
73 | my $obj = createSelfTiedObject($class, \$RawDeflateError); |
74 | |
75 | return $obj->_create(undef, @_); |
76 | } |
77 | |
78 | sub rawdeflate |
79 | { |
80 | my $obj = createSelfTiedObject(undef, \$RawDeflateError); |
81 | return $obj->_def(@_); |
82 | } |
83 | |
84 | sub ckParams |
85 | { |
86 | my $self = shift ; |
87 | my $got = shift; |
88 | |
89 | return 1 ; |
90 | } |
91 | |
92 | sub mkComp |
93 | { |
94 | my $self = shift ; |
95 | my $got = shift ; |
96 | |
97 | my ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject( |
98 | $got->value('CRC32'), |
99 | $got->value('Adler32'), |
100 | $got->value('Level'), |
101 | $got->value('Strategy') |
102 | ); |
103 | |
104 | return $self->saveErrorString(undef, $errstr, $errno) |
105 | if ! defined $obj; |
106 | |
107 | return $obj; |
108 | } |
109 | |
110 | |
111 | sub mkHeader |
112 | { |
113 | my $self = shift ; |
114 | return ''; |
115 | } |
116 | |
117 | sub mkTrailer |
118 | { |
119 | my $self = shift ; |
120 | return ''; |
121 | } |
122 | |
123 | sub mkFinalTrailer |
124 | { |
125 | return ''; |
126 | } |
127 | |
128 | |
129 | #sub newHeader |
130 | #{ |
131 | # my $self = shift ; |
132 | # return ''; |
133 | #} |
134 | |
135 | sub getExtraParams |
136 | { |
137 | my $self = shift ; |
138 | return $self->getZlibParams(); |
139 | } |
140 | |
141 | sub getZlibParams |
142 | { |
143 | my $self = shift ; |
144 | |
145 | use IO::Compress::Base::Common 2.023 qw(:Parse); |
146 | use Compress::Raw::Zlib 2.023 qw(Z_DEFLATED Z_DEFAULT_COMPRESSION Z_DEFAULT_STRATEGY); |
147 | |
148 | |
149 | return ( |
150 | |
151 | # zlib behaviour |
152 | #'Method' => [0, 1, Parse_unsigned, Z_DEFLATED], |
153 | 'Level' => [0, 1, Parse_signed, Z_DEFAULT_COMPRESSION], |
154 | 'Strategy' => [0, 1, Parse_signed, Z_DEFAULT_STRATEGY], |
155 | |
156 | 'CRC32' => [0, 1, Parse_boolean, 0], |
157 | 'ADLER32' => [0, 1, Parse_boolean, 0], |
158 | 'Merge' => [1, 1, Parse_boolean, 0], |
159 | ); |
160 | |
161 | |
162 | } |
163 | |
164 | sub getInverseClass |
165 | { |
166 | return ('IO::Uncompress::RawInflate', |
167 | \$IO::Uncompress::RawInflate::RawInflateError); |
168 | } |
169 | |
170 | sub getFileInfo |
171 | { |
172 | my $self = shift ; |
173 | my $params = shift; |
174 | my $file = shift ; |
175 | |
176 | } |
177 | |
178 | use IO::Seekable qw(SEEK_SET); |
179 | |
180 | sub createMerge |
181 | { |
182 | my $self = shift ; |
183 | my $outValue = shift ; |
184 | my $outType = shift ; |
185 | |
186 | my ($invClass, $error_ref) = $self->getInverseClass(); |
187 | eval "require $invClass" |
188 | or die "aaaahhhh" ; |
189 | |
190 | my $inf = $invClass->new( $outValue, |
191 | Transparent => 0, |
192 | #Strict => 1, |
193 | AutoClose => 0, |
194 | Scan => 1) |
195 | or return $self->saveErrorString(undef, "Cannot create InflateScan object: $$error_ref" ) ; |
196 | |
197 | my $end_offset = 0; |
198 | $inf->scan() |
199 | or return $self->saveErrorString(undef, "Error Scanning: $$error_ref", $inf->errorNo) ; |
200 | $inf->zap($end_offset) |
201 | or return $self->saveErrorString(undef, "Error Zapping: $$error_ref", $inf->errorNo) ; |
202 | |
203 | my $def = *$self->{Compress} = $inf->createDeflate(); |
204 | |
205 | *$self->{Header} = *$inf->{Info}{Header}; |
206 | *$self->{UnCompSize} = *$inf->{UnCompSize}->clone(); |
207 | *$self->{CompSize} = *$inf->{CompSize}->clone(); |
208 | # TODO -- fix this |
209 | #*$self->{CompSize} = new U64(0, *$self->{UnCompSize_32bit}); |
210 | |
211 | |
212 | if ( $outType eq 'buffer') |
213 | { substr( ${ *$self->{Buffer} }, $end_offset) = '' } |
214 | elsif ($outType eq 'handle' || $outType eq 'filename') { |
215 | *$self->{FH} = *$inf->{FH} ; |
216 | delete *$inf->{FH}; |
217 | *$self->{FH}->flush() ; |
218 | *$self->{Handle} = 1 if $outType eq 'handle'; |
219 | |
220 | #seek(*$self->{FH}, $end_offset, SEEK_SET) |
221 | *$self->{FH}->seek($end_offset, SEEK_SET) |
222 | or return $self->saveErrorString(undef, $!, $!) ; |
223 | } |
224 | |
225 | return $def ; |
226 | } |
227 | |
228 | #### zlib specific methods |
229 | |
230 | sub deflateParams |
231 | { |
232 | my $self = shift ; |
233 | |
234 | my $level = shift ; |
235 | my $strategy = shift ; |
236 | |
237 | my $status = *$self->{Compress}->deflateParams(Level => $level, Strategy => $strategy) ; |
238 | return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo}) |
239 | if $status == STATUS_ERROR; |
240 | |
241 | return 1; |
242 | } |
243 | |
244 | |
245 | |
246 | |
247 | 1; |
248 | |
249 | __END__ |
250 | |
251 | =head1 NAME |
252 | |
253 | IO::Compress::RawDeflate - Write RFC 1951 files/buffers |
254 | |
255 | |
256 | |
257 | =head1 SYNOPSIS |
258 | |
259 | use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ; |
260 | |
261 | my $status = rawdeflate $input => $output [,OPTS] |
262 | or die "rawdeflate failed: $RawDeflateError\n"; |
263 | |
264 | my $z = new IO::Compress::RawDeflate $output [,OPTS] |
265 | or die "rawdeflate failed: $RawDeflateError\n"; |
266 | |
267 | $z->print($string); |
268 | $z->printf($format, $string); |
269 | $z->write($string); |
270 | $z->syswrite($string [, $length, $offset]); |
271 | $z->flush(); |
272 | $z->tell(); |
273 | $z->eof(); |
274 | $z->seek($position, $whence); |
275 | $z->binmode(); |
276 | $z->fileno(); |
277 | $z->opened(); |
278 | $z->autoflush(); |
279 | $z->input_line_number(); |
280 | $z->newStream( [OPTS] ); |
281 | |
282 | $z->deflateParams(); |
283 | |
284 | $z->close() ; |
285 | |
286 | $RawDeflateError ; |
287 | |
288 | # IO::File mode |
289 | |
290 | print $z $string; |
291 | printf $z $format, $string; |
292 | tell $z |
293 | eof $z |
294 | seek $z, $position, $whence |
295 | binmode $z |
296 | fileno $z |
297 | close $z ; |
298 | |
299 | |
300 | =head1 DESCRIPTION |
301 | |
302 | This module provides a Perl interface that allows writing compressed |
303 | data to files or buffer as defined in RFC 1951. |
304 | |
305 | Note that RFC 1951 data is not a good choice of compression format |
306 | to use in isolation, especially if you want to auto-detect it. |
307 | |
308 | For reading RFC 1951 files/buffers, see the companion module |
309 | L<IO::Uncompress::RawInflate|IO::Uncompress::RawInflate>. |
310 | |
311 | =head1 Functional Interface |
312 | |
313 | A top-level function, C<rawdeflate>, is provided to carry out |
314 | "one-shot" compression between buffers and/or files. For finer |
315 | control over the compression process, see the L</"OO Interface"> |
316 | section. |
317 | |
318 | use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ; |
319 | |
320 | rawdeflate $input => $output [,OPTS] |
321 | or die "rawdeflate failed: $RawDeflateError\n"; |
322 | |
323 | The functional interface needs Perl5.005 or better. |
324 | |
325 | =head2 rawdeflate $input => $output [, OPTS] |
326 | |
327 | C<rawdeflate> expects at least two parameters, C<$input> and C<$output>. |
328 | |
329 | =head3 The C<$input> parameter |
330 | |
331 | The parameter, C<$input>, is used to define the source of |
332 | the uncompressed data. |
333 | |
334 | It can take one of the following forms: |
335 | |
336 | =over 5 |
337 | |
338 | =item A filename |
339 | |
340 | If the C<$input> parameter is a simple scalar, it is assumed to be a |
341 | filename. This file will be opened for reading and the input data |
342 | will be read from it. |
343 | |
344 | =item A filehandle |
345 | |
346 | If the C<$input> parameter is a filehandle, the input data will be |
347 | read from it. |
348 | The string '-' can be used as an alias for standard input. |
349 | |
350 | =item A scalar reference |
351 | |
352 | If C<$input> is a scalar reference, the input data will be read |
353 | from C<$$input>. |
354 | |
355 | =item An array reference |
356 | |
357 | If C<$input> is an array reference, each element in the array must be a |
358 | filename. |
359 | |
360 | The input data will be read from each file in turn. |
361 | |
362 | The complete array will be walked to ensure that it only |
363 | contains valid filenames before any data is compressed. |
364 | |
365 | =item An Input FileGlob string |
366 | |
367 | If C<$input> is a string that is delimited by the characters "<" and ">" |
368 | C<rawdeflate> will assume that it is an I<input fileglob string>. The |
369 | input is the list of files that match the fileglob. |
370 | |
371 | If the fileglob does not match any files ... |
372 | |
373 | See L<File::GlobMapper|File::GlobMapper> for more details. |
374 | |
375 | =back |
376 | |
377 | If the C<$input> parameter is any other type, C<undef> will be returned. |
378 | |
379 | =head3 The C<$output> parameter |
380 | |
381 | The parameter C<$output> is used to control the destination of the |
382 | compressed data. This parameter can take one of these forms. |
383 | |
384 | =over 5 |
385 | |
386 | =item A filename |
387 | |
388 | If the C<$output> parameter is a simple scalar, it is assumed to be a |
389 | filename. This file will be opened for writing and the compressed |
390 | data will be written to it. |
391 | |
392 | =item A filehandle |
393 | |
394 | If the C<$output> parameter is a filehandle, the compressed data |
395 | will be written to it. |
396 | The string '-' can be used as an alias for standard output. |
397 | |
398 | =item A scalar reference |
399 | |
400 | If C<$output> is a scalar reference, the compressed data will be |
401 | stored in C<$$output>. |
402 | |
403 | =item An Array Reference |
404 | |
405 | If C<$output> is an array reference, the compressed data will be |
406 | pushed onto the array. |
407 | |
408 | =item An Output FileGlob |
409 | |
410 | If C<$output> is a string that is delimited by the characters "<" and ">" |
411 | C<rawdeflate> will assume that it is an I<output fileglob string>. The |
412 | output is the list of files that match the fileglob. |
413 | |
414 | When C<$output> is an fileglob string, C<$input> must also be a fileglob |
415 | string. Anything else is an error. |
416 | |
417 | =back |
418 | |
419 | If the C<$output> parameter is any other type, C<undef> will be returned. |
420 | |
421 | =head2 Notes |
422 | |
423 | When C<$input> maps to multiple files/buffers and C<$output> is a single |
424 | file/buffer the input files/buffers will be stored |
425 | in C<$output> as a concatenated series of compressed data streams. |
426 | |
427 | =head2 Optional Parameters |
428 | |
429 | Unless specified below, the optional parameters for C<rawdeflate>, |
430 | C<OPTS>, are the same as those used with the OO interface defined in the |
431 | L</"Constructor Options"> section below. |
432 | |
433 | =over 5 |
434 | |
435 | =item C<< AutoClose => 0|1 >> |
436 | |
437 | This option applies to any input or output data streams to |
438 | C<rawdeflate> that are filehandles. |
439 | |
440 | If C<AutoClose> is specified, and the value is true, it will result in all |
441 | input and/or output filehandles being closed once C<rawdeflate> has |
442 | completed. |
443 | |
444 | This parameter defaults to 0. |
445 | |
446 | =item C<< BinModeIn => 0|1 >> |
447 | |
448 | When reading from a file or filehandle, set C<binmode> before reading. |
449 | |
450 | Defaults to 0. |
451 | |
452 | =item C<< Append => 0|1 >> |
453 | |
454 | The behaviour of this option is dependent on the type of output data |
455 | stream. |
456 | |
457 | =over 5 |
458 | |
459 | =item * A Buffer |
460 | |
461 | If C<Append> is enabled, all compressed data will be append to the end of |
462 | the output buffer. Otherwise the output buffer will be cleared before any |
463 | compressed data is written to it. |
464 | |
465 | =item * A Filename |
466 | |
467 | If C<Append> is enabled, the file will be opened in append mode. Otherwise |
468 | the contents of the file, if any, will be truncated before any compressed |
469 | data is written to it. |
470 | |
471 | =item * A Filehandle |
472 | |
473 | If C<Append> is enabled, the filehandle will be positioned to the end of |
474 | the file via a call to C<seek> before any compressed data is |
475 | written to it. Otherwise the file pointer will not be moved. |
476 | |
477 | =back |
478 | |
479 | When C<Append> is specified, and set to true, it will I<append> all compressed |
480 | data to the output data stream. |
481 | |
482 | So when the output is a filehandle it will carry out a seek to the eof |
483 | before writing any compressed data. If the output is a filename, it will be opened for |
484 | appending. If the output is a buffer, all compressed data will be appened to |
485 | the existing buffer. |
486 | |
487 | Conversely when C<Append> is not specified, or it is present and is set to |
488 | false, it will operate as follows. |
489 | |
490 | When the output is a filename, it will truncate the contents of the file |
491 | before writing any compressed data. If the output is a filehandle |
492 | its position will not be changed. If the output is a buffer, it will be |
493 | wiped before any compressed data is output. |
494 | |
495 | Defaults to 0. |
496 | |
497 | =back |
498 | |
499 | =head2 Examples |
500 | |
501 | To read the contents of the file C<file1.txt> and write the compressed |
502 | data to the file C<file1.txt.1951>. |
503 | |
504 | use strict ; |
505 | use warnings ; |
506 | use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ; |
507 | |
508 | my $input = "file1.txt"; |
509 | rawdeflate $input => "$input.1951" |
510 | or die "rawdeflate failed: $RawDeflateError\n"; |
511 | |
512 | To read from an existing Perl filehandle, C<$input>, and write the |
513 | compressed data to a buffer, C<$buffer>. |
514 | |
515 | use strict ; |
516 | use warnings ; |
517 | use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ; |
518 | use IO::File ; |
519 | |
520 | my $input = new IO::File "<file1.txt" |
521 | or die "Cannot open 'file1.txt': $!\n" ; |
522 | my $buffer ; |
523 | rawdeflate $input => \$buffer |
524 | or die "rawdeflate failed: $RawDeflateError\n"; |
525 | |
526 | To compress all files in the directory "/my/home" that match "*.txt" |
527 | and store the compressed data in the same directory |
528 | |
529 | use strict ; |
530 | use warnings ; |
531 | use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ; |
532 | |
533 | rawdeflate '</my/home/*.txt>' => '<*.1951>' |
534 | or die "rawdeflate failed: $RawDeflateError\n"; |
535 | |
536 | and if you want to compress each file one at a time, this will do the trick |
537 | |
538 | use strict ; |
539 | use warnings ; |
540 | use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ; |
541 | |
542 | for my $input ( glob "/my/home/*.txt" ) |
543 | { |
544 | my $output = "$input.1951" ; |
545 | rawdeflate $input => $output |
546 | or die "Error compressing '$input': $RawDeflateError\n"; |
547 | } |
548 | |
549 | =head1 OO Interface |
550 | |
551 | =head2 Constructor |
552 | |
553 | The format of the constructor for C<IO::Compress::RawDeflate> is shown below |
554 | |
555 | my $z = new IO::Compress::RawDeflate $output [,OPTS] |
556 | or die "IO::Compress::RawDeflate failed: $RawDeflateError\n"; |
557 | |
558 | It returns an C<IO::Compress::RawDeflate> object on success and undef on failure. |
559 | The variable C<$RawDeflateError> will contain an error message on failure. |
560 | |
561 | If you are running Perl 5.005 or better the object, C<$z>, returned from |
562 | IO::Compress::RawDeflate can be used exactly like an L<IO::File|IO::File> filehandle. |
563 | This means that all normal output file operations can be carried out |
564 | with C<$z>. |
565 | For example, to write to a compressed file/buffer you can use either of |
566 | these forms |
567 | |
568 | $z->print("hello world\n"); |
569 | print $z "hello world\n"; |
570 | |
571 | The mandatory parameter C<$output> is used to control the destination |
572 | of the compressed data. This parameter can take one of these forms. |
573 | |
574 | =over 5 |
575 | |
576 | =item A filename |
577 | |
578 | If the C<$output> parameter is a simple scalar, it is assumed to be a |
579 | filename. This file will be opened for writing and the compressed data |
580 | will be written to it. |
581 | |
582 | =item A filehandle |
583 | |
584 | If the C<$output> parameter is a filehandle, the compressed data will be |
585 | written to it. |
586 | The string '-' can be used as an alias for standard output. |
587 | |
588 | =item A scalar reference |
589 | |
590 | If C<$output> is a scalar reference, the compressed data will be stored |
591 | in C<$$output>. |
592 | |
593 | =back |
594 | |
595 | If the C<$output> parameter is any other type, C<IO::Compress::RawDeflate>::new will |
596 | return undef. |
597 | |
598 | =head2 Constructor Options |
599 | |
600 | C<OPTS> is any combination of the following options: |
601 | |
602 | =over 5 |
603 | |
604 | =item C<< AutoClose => 0|1 >> |
605 | |
606 | This option is only valid when the C<$output> parameter is a filehandle. If |
607 | specified, and the value is true, it will result in the C<$output> being |
608 | closed once either the C<close> method is called or the C<IO::Compress::RawDeflate> |
609 | object is destroyed. |
610 | |
611 | This parameter defaults to 0. |
612 | |
613 | =item C<< Append => 0|1 >> |
614 | |
615 | Opens C<$output> in append mode. |
616 | |
617 | The behaviour of this option is dependent on the type of C<$output>. |
618 | |
619 | =over 5 |
620 | |
621 | =item * A Buffer |
622 | |
623 | If C<$output> is a buffer and C<Append> is enabled, all compressed data |
624 | will be append to the end of C<$output>. Otherwise C<$output> will be |
625 | cleared before any data is written to it. |
626 | |
627 | =item * A Filename |
628 | |
629 | If C<$output> is a filename and C<Append> is enabled, the file will be |
630 | opened in append mode. Otherwise the contents of the file, if any, will be |
631 | truncated before any compressed data is written to it. |
632 | |
633 | =item * A Filehandle |
634 | |
635 | If C<$output> is a filehandle, the file pointer will be positioned to the |
636 | end of the file via a call to C<seek> before any compressed data is written |
637 | to it. Otherwise the file pointer will not be moved. |
638 | |
639 | =back |
640 | |
641 | This parameter defaults to 0. |
642 | |
643 | =item C<< Merge => 0|1 >> |
644 | |
645 | This option is used to compress input data and append it to an existing |
646 | compressed data stream in C<$output>. The end result is a single compressed |
647 | data stream stored in C<$output>. |
648 | |
649 | It is a fatal error to attempt to use this option when C<$output> is not an |
650 | RFC 1951 data stream. |
651 | |
652 | There are a number of other limitations with the C<Merge> option: |
653 | |
654 | =over 5 |
655 | |
656 | =item 1 |
657 | |
658 | This module needs to have been built with zlib 1.2.1 or better to work. A |
659 | fatal error will be thrown if C<Merge> is used with an older version of |
660 | zlib. |
661 | |
662 | =item 2 |
663 | |
664 | If C<$output> is a file or a filehandle, it must be seekable. |
665 | |
666 | =back |
667 | |
668 | This parameter defaults to 0. |
669 | |
670 | =item -Level |
671 | |
672 | Defines the compression level used by zlib. The value should either be |
673 | a number between 0 and 9 (0 means no compression and 9 is maximum |
674 | compression), or one of the symbolic constants defined below. |
675 | |
676 | Z_NO_COMPRESSION |
677 | Z_BEST_SPEED |
678 | Z_BEST_COMPRESSION |
679 | Z_DEFAULT_COMPRESSION |
680 | |
681 | The default is Z_DEFAULT_COMPRESSION. |
682 | |
683 | Note, these constants are not imported by C<IO::Compress::RawDeflate> by default. |
684 | |
685 | use IO::Compress::RawDeflate qw(:strategy); |
686 | use IO::Compress::RawDeflate qw(:constants); |
687 | use IO::Compress::RawDeflate qw(:all); |
688 | |
689 | =item -Strategy |
690 | |
691 | Defines the strategy used to tune the compression. Use one of the symbolic |
692 | constants defined below. |
693 | |
694 | Z_FILTERED |
695 | Z_HUFFMAN_ONLY |
696 | Z_RLE |
697 | Z_FIXED |
698 | Z_DEFAULT_STRATEGY |
699 | |
700 | The default is Z_DEFAULT_STRATEGY. |
701 | |
702 | =item C<< Strict => 0|1 >> |
703 | |
704 | This is a placeholder option. |
705 | |
706 | =back |
707 | |
708 | =head2 Examples |
709 | |
710 | TODO |
711 | |
712 | =head1 Methods |
713 | |
714 | =head2 print |
715 | |
716 | Usage is |
717 | |
718 | $z->print($data) |
719 | print $z $data |
720 | |
721 | Compresses and outputs the contents of the C<$data> parameter. This |
722 | has the same behaviour as the C<print> built-in. |
723 | |
724 | Returns true if successful. |
725 | |
726 | =head2 printf |
727 | |
728 | Usage is |
729 | |
730 | $z->printf($format, $data) |
731 | printf $z $format, $data |
732 | |
733 | Compresses and outputs the contents of the C<$data> parameter. |
734 | |
735 | Returns true if successful. |
736 | |
737 | =head2 syswrite |
738 | |
739 | Usage is |
740 | |
741 | $z->syswrite $data |
742 | $z->syswrite $data, $length |
743 | $z->syswrite $data, $length, $offset |
744 | |
745 | Compresses and outputs the contents of the C<$data> parameter. |
746 | |
747 | Returns the number of uncompressed bytes written, or C<undef> if |
748 | unsuccessful. |
749 | |
750 | =head2 write |
751 | |
752 | Usage is |
753 | |
754 | $z->write $data |
755 | $z->write $data, $length |
756 | $z->write $data, $length, $offset |
757 | |
758 | Compresses and outputs the contents of the C<$data> parameter. |
759 | |
760 | Returns the number of uncompressed bytes written, or C<undef> if |
761 | unsuccessful. |
762 | |
763 | =head2 flush |
764 | |
765 | Usage is |
766 | |
767 | $z->flush; |
768 | $z->flush($flush_type); |
769 | |
770 | Flushes any pending compressed data to the output file/buffer. |
771 | |
772 | This method takes an optional parameter, C<$flush_type>, that controls |
773 | how the flushing will be carried out. By default the C<$flush_type> |
774 | used is C<Z_FINISH>. Other valid values for C<$flush_type> are |
775 | C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is |
776 | strongly recommended that you only set the C<flush_type> parameter if |
777 | you fully understand the implications of what it does - overuse of C<flush> |
778 | can seriously degrade the level of compression achieved. See the C<zlib> |
779 | documentation for details. |
780 | |
781 | Returns true on success. |
782 | |
783 | =head2 tell |
784 | |
785 | Usage is |
786 | |
787 | $z->tell() |
788 | tell $z |
789 | |
790 | Returns the uncompressed file offset. |
791 | |
792 | =head2 eof |
793 | |
794 | Usage is |
795 | |
796 | $z->eof(); |
797 | eof($z); |
798 | |
799 | Returns true if the C<close> method has been called. |
800 | |
801 | =head2 seek |
802 | |
803 | $z->seek($position, $whence); |
804 | seek($z, $position, $whence); |
805 | |
806 | Provides a sub-set of the C<seek> functionality, with the restriction |
807 | that it is only legal to seek forward in the output file/buffer. |
808 | It is a fatal error to attempt to seek backward. |
809 | |
810 | Empty parts of the file/buffer will have NULL (0x00) bytes written to them. |
811 | |
812 | The C<$whence> parameter takes one the usual values, namely SEEK_SET, |
813 | SEEK_CUR or SEEK_END. |
814 | |
815 | Returns 1 on success, 0 on failure. |
816 | |
817 | =head2 binmode |
818 | |
819 | Usage is |
820 | |
821 | $z->binmode |
822 | binmode $z ; |
823 | |
824 | This is a noop provided for completeness. |
825 | |
826 | =head2 opened |
827 | |
828 | $z->opened() |
829 | |
830 | Returns true if the object currently refers to a opened file/buffer. |
831 | |
832 | =head2 autoflush |
833 | |
834 | my $prev = $z->autoflush() |
835 | my $prev = $z->autoflush(EXPR) |
836 | |
837 | If the C<$z> object is associated with a file or a filehandle, this method |
838 | returns the current autoflush setting for the underlying filehandle. If |
839 | C<EXPR> is present, and is non-zero, it will enable flushing after every |
840 | write/print operation. |
841 | |
842 | If C<$z> is associated with a buffer, this method has no effect and always |
843 | returns C<undef>. |
844 | |
845 | B<Note> that the special variable C<$|> B<cannot> be used to set or |
846 | retrieve the autoflush setting. |
847 | |
848 | =head2 input_line_number |
849 | |
850 | $z->input_line_number() |
851 | $z->input_line_number(EXPR) |
852 | |
853 | This method always returns C<undef> when compressing. |
854 | |
855 | =head2 fileno |
856 | |
857 | $z->fileno() |
858 | fileno($z) |
859 | |
860 | If the C<$z> object is associated with a file or a filehandle, C<fileno> |
861 | will return the underlying file descriptor. Once the C<close> method is |
862 | called C<fileno> will return C<undef>. |
863 | |
864 | If the C<$z> object is is associated with a buffer, this method will return |
865 | C<undef>. |
866 | |
867 | =head2 close |
868 | |
869 | $z->close() ; |
870 | close $z ; |
871 | |
872 | Flushes any pending compressed data and then closes the output file/buffer. |
873 | |
874 | For most versions of Perl this method will be automatically invoked if |
875 | the IO::Compress::RawDeflate object is destroyed (either explicitly or by the |
876 | variable with the reference to the object going out of scope). The |
877 | exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In |
878 | these cases, the C<close> method will be called automatically, but |
879 | not until global destruction of all live objects when the program is |
880 | terminating. |
881 | |
882 | Therefore, if you want your scripts to be able to run on all versions |
883 | of Perl, you should call C<close> explicitly and not rely on automatic |
884 | closing. |
885 | |
886 | Returns true on success, otherwise 0. |
887 | |
888 | If the C<AutoClose> option has been enabled when the IO::Compress::RawDeflate |
889 | object was created, and the object is associated with a file, the |
890 | underlying file will also be closed. |
891 | |
892 | =head2 newStream([OPTS]) |
893 | |
894 | Usage is |
895 | |
896 | $z->newStream( [OPTS] ) |
897 | |
898 | Closes the current compressed data stream and starts a new one. |
899 | |
900 | OPTS consists of any of the the options that are available when creating |
901 | the C<$z> object. |
902 | |
903 | See the L</"Constructor Options"> section for more details. |
904 | |
905 | =head2 deflateParams |
906 | |
907 | Usage is |
908 | |
909 | $z->deflateParams |
910 | |
911 | TODO |
912 | |
913 | =head1 Importing |
914 | |
915 | A number of symbolic constants are required by some methods in |
916 | C<IO::Compress::RawDeflate>. None are imported by default. |
917 | |
918 | =over 5 |
919 | |
920 | =item :all |
921 | |
922 | Imports C<rawdeflate>, C<$RawDeflateError> and all symbolic |
923 | constants that can be used by C<IO::Compress::RawDeflate>. Same as doing this |
924 | |
925 | use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError :constants) ; |
926 | |
927 | =item :constants |
928 | |
929 | Import all symbolic constants. Same as doing this |
930 | |
931 | use IO::Compress::RawDeflate qw(:flush :level :strategy) ; |
932 | |
933 | =item :flush |
934 | |
935 | These symbolic constants are used by the C<flush> method. |
936 | |
937 | Z_NO_FLUSH |
938 | Z_PARTIAL_FLUSH |
939 | Z_SYNC_FLUSH |
940 | Z_FULL_FLUSH |
941 | Z_FINISH |
942 | Z_BLOCK |
943 | |
944 | =item :level |
945 | |
946 | These symbolic constants are used by the C<Level> option in the constructor. |
947 | |
948 | Z_NO_COMPRESSION |
949 | Z_BEST_SPEED |
950 | Z_BEST_COMPRESSION |
951 | Z_DEFAULT_COMPRESSION |
952 | |
953 | =item :strategy |
954 | |
955 | These symbolic constants are used by the C<Strategy> option in the constructor. |
956 | |
957 | Z_FILTERED |
958 | Z_HUFFMAN_ONLY |
959 | Z_RLE |
960 | Z_FIXED |
961 | Z_DEFAULT_STRATEGY |
962 | |
963 | |
964 | |
965 | |
966 | =back |
967 | |
968 | =head1 EXAMPLES |
969 | |
970 | =head2 Apache::GZip Revisited |
971 | |
972 | See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited"> |
973 | |
974 | |
975 | |
976 | =head2 Working with Net::FTP |
977 | |
978 | See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP"> |
979 | |
980 | =head1 SEE ALSO |
981 | |
982 | L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> |
983 | |
984 | L<Compress::Zlib::FAQ|Compress::Zlib::FAQ> |
985 | |
986 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, |
987 | L<Archive::Tar|Archive::Tar>, |
988 | L<IO::Zlib|IO::Zlib> |
989 | |
990 | For RFC 1950, 1951 and 1952 see |
991 | F<http://www.faqs.org/rfcs/rfc1950.html>, |
992 | F<http://www.faqs.org/rfcs/rfc1951.html> and |
993 | F<http://www.faqs.org/rfcs/rfc1952.html> |
994 | |
995 | The I<zlib> compression library was written by Jean-loup Gailly |
996 | F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>. |
997 | |
998 | The primary site for the I<zlib> compression library is |
999 | F<http://www.zlib.org>. |
1000 | |
1001 | The primary site for gzip is F<http://www.gzip.org>. |
1002 | |
1003 | =head1 AUTHOR |
1004 | |
1005 | This module was written by Paul Marquess, F<pmqs@cpan.org>. |
1006 | |
1007 | =head1 MODIFICATION HISTORY |
1008 | |
1009 | See the Changes file. |
1010 | |
1011 | =head1 COPYRIGHT AND LICENSE |
1012 | |
1013 | Copyright (c) 2005-2009 Paul Marquess. All rights reserved. |
1014 | |
1015 | This program is free software; you can redistribute it and/or |
1016 | modify it under the same terms as Perl itself. |
1017 | |