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