Upgrade to Compress::Zlib 2.000_05
[p5sagit/p5-mst-13.2.git] / ext / Compress / Zlib / lib / IO / Compress / Deflate.pm
1 package IO::Compress::Deflate ;
2
3 use strict ;
4 use warnings;
5 require Exporter ;
6
7 use IO::Compress::Gzip ;
8
9
10 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $DeflateError);
11
12 $VERSION = '2.000_05';
13 $DeflateError = '';
14
15 @ISA = qw(Exporter IO::BaseDeflate);
16 @EXPORT_OK = qw( $DeflateError deflate ) ;
17 %EXPORT_TAGS = %IO::BaseDeflate::EXPORT_TAGS ;
18 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
19 Exporter::export_ok_tags('all');
20
21
22
23 sub new
24 {
25     my $pkg = shift ;
26     return IO::BaseDeflate::new($pkg, 'rfc1950', undef, \$DeflateError, @_);
27 }
28
29 sub deflate
30 {
31     return IO::BaseDeflate::_def(__PACKAGE__, 'rfc1950', \$DeflateError, @_);
32 }
33
34
35 1;
36
37 __END__
38
39 =head1 NAME
40
41 IO::Compress::Deflate     - Perl interface to write RFC 1950 files/buffers
42
43 =head1 SYNOPSIS
44
45     use IO::Compress::Deflate qw(deflate $DeflateError) ;
46
47
48     my $status = deflate $input => $output [,OPTS] 
49         or die "deflate failed: $DeflateError\n";
50
51     my $z = new IO::Compress::Deflate $output [,OPTS]
52         or die "deflate failed: $DeflateError\n";
53
54     $z->print($string);
55     $z->printf($format, $string);
56     $z->write($string);
57     $z->syswrite($string [, $length, $offset]);
58     $z->flush();
59     $z->tell();
60     $z->eof();
61     $z->seek($position, $whence);
62     $z->binmode();
63     $z->fileno();
64     $z->newStream();
65     $z->deflateParams();
66     $z->close() ;
67
68     $DeflateError ;
69
70     # IO::File mode
71
72     print $z $string;
73     printf $z $format, $string;
74     syswrite $z, $string [, $length, $offset];
75     flush $z, ;
76     tell $z
77     eof $z
78     seek $z, $position, $whence
79     binmode $z
80     fileno $z
81     close $z ;
82     
83
84 =head1 DESCRIPTION
85
86
87
88 B<WARNING -- This is a Beta release>. 
89
90 =over 5
91
92 =item * DO NOT use in production code.
93
94 =item * The documentation is incomplete in places.
95
96 =item * Parts of the interface defined here are tentative.
97
98 =item * Please report any problems you find.
99
100 =back
101
102
103
104 This module provides a Perl interface that allows writing compressed
105 data to files or buffer as defined in RFC 1950.
106
107
108
109
110
111 For reading RFC 1950 files/buffers, see the companion module 
112 L<IO::Uncompress::Inflate|IO::Uncompress::Inflate>.
113
114
115 =head1 Functional Interface
116
117 A top-level function, C<deflate>, is provided to carry out "one-shot"
118 compression between buffers and/or files. For finer control over the compression process, see the L</"OO Interface"> section.
119
120     use IO::Compress::Deflate qw(deflate $DeflateError) ;
121
122     deflate $input => $output [,OPTS] 
123         or die "deflate failed: $DeflateError\n";
124
125     deflate \%hash [,OPTS] 
126         or die "deflate failed: $DeflateError\n";
127
128 The functional interface needs Perl5.005 or better.
129
130
131 =head2 deflate $input => $output [, OPTS]
132
133 If the first parameter is not a hash reference C<deflate> expects
134 at least two parameters, C<$input> and C<$output>.
135
136 =head3 The C<$input> parameter
137
138 The parameter, C<$input>, is used to define the source of
139 the uncompressed data. 
140
141 It can take one of the following forms:
142
143 =over 5
144
145 =item A filename
146
147 If the C<$input> parameter is a simple scalar, it is assumed to be a
148 filename. This file will be opened for reading and the input data
149 will be read from it.
150
151 =item A filehandle
152
153 If the C<$input> parameter is a filehandle, the input data will be
154 read from it.
155 The string '-' can be used as an alias for standard input.
156
157 =item A scalar reference 
158
159 If C<$input> is a scalar reference, the input data will be read
160 from C<$$input>.
161
162 =item An array reference 
163
164 If C<$input> is an array reference, the input data will be read from each
165 element of the array in turn. The action taken by C<deflate> with
166 each element of the array will depend on the type of data stored
167 in it. You can mix and match any of the types defined in this list,
168 excluding other array or hash references. 
169 The complete array will be walked to ensure that it only
170 contains valid data types before any data is compressed.
171
172 =item An Input FileGlob string
173
174 If C<$input> is a string that is delimited by the characters "<" and ">"
175 C<deflate> will assume that it is an I<input fileglob string>. The
176 input is the list of files that match the fileglob.
177
178 If the fileglob does not match any files ...
179
180 See L<File::GlobMapper|File::GlobMapper> for more details.
181
182
183 =back
184
185 If the C<$input> parameter is any other type, C<undef> will be returned.
186
187
188
189 =head3 The C<$output> parameter
190
191 The parameter C<$output> is used to control the destination of the
192 compressed data. This parameter can take one of these forms.
193
194 =over 5
195
196 =item A filename
197
198 If the C<$output> parameter is a simple scalar, it is assumed to be a filename.
199 This file will be opened for writing and the compressed data will be
200 written to it.
201
202 =item A filehandle
203
204 If the C<$output> parameter is a filehandle, the compressed data will
205 be written to it.  
206 The string '-' can be used as an alias for standard output.
207
208
209 =item A scalar reference 
210
211 If C<$output> is a scalar reference, the compressed data will be stored
212 in C<$$output>.
213
214
215 =item A Hash Reference
216
217 If C<$output> is a hash reference, the compressed data will be written
218 to C<$output{$input}> as a scalar reference.
219
220 When C<$output> is a hash reference, C<$input> must be either a filename or
221 list of filenames. Anything else is an error.
222
223
224 =item An Array Reference
225
226 If C<$output> is an array reference, the compressed data will be pushed
227 onto the array.
228
229 =item An Output FileGlob
230
231 If C<$output> is a string that is delimited by the characters "<" and ">"
232 C<deflate> will assume that it is an I<output fileglob string>. The
233 output is the list of files that match the fileglob.
234
235 When C<$output> is an fileglob string, C<$input> must also be a fileglob
236 string. Anything else is an error.
237
238 =back
239
240 If the C<$output> parameter is any other type, C<undef> will be returned.
241
242 =head2 deflate \%hash [, OPTS]
243
244 If the first parameter is a hash reference, C<\%hash>, this will be used to
245 define both the source of uncompressed data and to control where the
246 compressed data is output. Each key/value pair in the hash defines a
247 mapping between an input filename, stored in the key, and an output
248 file/buffer, stored in the value. Although the input can only be a filename,
249 there is more flexibility to control the destination of the compressed
250 data. This is determined by the type of the value. Valid types are
251
252 =over 5
253
254 =item undef
255
256 If the value is C<undef> the compressed data will be written to the
257 value as a scalar reference.
258
259 =item A filename
260
261 If the value is a simple scalar, it is assumed to be a filename. This file will
262 be opened for writing and the compressed data will be written to it.
263
264 =item A filehandle
265
266 If the value is a filehandle, the compressed data will be
267 written to it. 
268 The string '-' can be used as an alias for standard output.
269
270
271 =item A scalar reference 
272
273 If the value is a scalar reference, the compressed data will be stored
274 in the buffer that is referenced by the scalar.
275
276
277 =item A Hash Reference
278
279 If the value is a hash reference, the compressed data will be written
280 to C<$hash{$input}> as a scalar reference.
281
282 =item An Array Reference
283
284 If C<$output> is an array reference, the compressed data will be pushed
285 onto the array.
286
287 =back
288
289 Any other type is a error.
290
291 =head2 Notes
292
293 When C<$input> maps to multiple files/buffers and C<$output> is a single
294 file/buffer the compressed input files/buffers will all be stored in
295 C<$output> as a single compressed stream.
296
297
298
299 =head2 Optional Parameters
300
301 Unless specified below, the optional parameters for C<deflate>,
302 C<OPTS>, are the same as those used with the OO interface defined in the
303 L</"Constructor Options"> section below.
304
305 =over 5
306
307 =item AutoClose =E<gt> 0|1
308
309 This option applies to any input or output data streams to C<deflate>
310 that are filehandles.
311
312 If C<AutoClose> is specified, and the value is true, it will result in all
313 input and/or output filehandles being closed once C<deflate> has
314 completed.
315
316 This parameter defaults to 0.
317
318
319
320 =item -Append =E<gt> 0|1
321
322 TODO
323
324
325 =back
326
327
328
329 =head2 Examples
330
331 To read the contents of the file C<file1.txt> and write the compressed
332 data to the file C<file1.txt.1950>.
333
334     use strict ;
335     use warnings ;
336     use IO::Compress::Deflate qw(deflate $DeflateError) ;
337
338     my $input = "file1.txt";
339     deflate $input => "$input.1950"
340         or die "deflate failed: $DeflateError\n";
341
342
343 To read from an existing Perl filehandle, C<$input>, and write the
344 compressed data to a buffer, C<$buffer>.
345
346     use strict ;
347     use warnings ;
348     use IO::Compress::Deflate qw(deflate $DeflateError) ;
349     use IO::File ;
350
351     my $input = new IO::File "<file1.txt"
352         or die "Cannot open 'file1.txt': $!\n" ;
353     my $buffer ;
354     deflate $input => \$buffer 
355         or die "deflate failed: $DeflateError\n";
356
357 To compress all files in the directory "/my/home" that match "*.txt"
358 and store the compressed data in the same directory
359
360     use strict ;
361     use warnings ;
362     use IO::Compress::Deflate qw(deflate $DeflateError) ;
363
364     deflate '</my/home/*.txt>' => '<*.1950>'
365         or die "deflate failed: $DeflateError\n";
366
367 and if you want to compress each file one at a time, this will do the trick
368
369     use strict ;
370     use warnings ;
371     use IO::Compress::Deflate qw(deflate $DeflateError) ;
372
373     for my $input ( glob "/my/home/*.txt" )
374     {
375         my $output = "$input.1950" ;
376         deflate $input => $output 
377             or die "Error compressing '$input': $DeflateError\n";
378     }
379
380
381 =head1 OO Interface
382
383 =head2 Constructor
384
385 The format of the constructor for C<IO::Compress::Deflate> is shown below
386
387     my $z = new IO::Compress::Deflate $output [,OPTS]
388         or die "IO::Compress::Deflate failed: $DeflateError\n";
389
390 It returns an C<IO::Compress::Deflate> object on success and undef on failure. 
391 The variable C<$DeflateError> will contain an error message on failure.
392
393 If you are running Perl 5.005 or better the object, C<$z>, returned from 
394 IO::Compress::Deflate can be used exactly like an L<IO::File|IO::File> filehandle. 
395 This means that all normal output file operations can be carried out 
396 with C<$z>. 
397 For example, to write to a compressed file/buffer you can use either of 
398 these forms
399
400     $z->print("hello world\n");
401     print $z "hello world\n";
402
403 The mandatory parameter C<$output> is used to control the destination
404 of the compressed data. This parameter can take one of these forms.
405
406 =over 5
407
408 =item A filename
409
410 If the C<$output> parameter is a simple scalar, it is assumed to be a
411 filename. This file will be opened for writing and the compressed data
412 will be written to it.
413
414 =item A filehandle
415
416 If the C<$output> parameter is a filehandle, the compressed data will be
417 written to it.
418 The string '-' can be used as an alias for standard output.
419
420
421 =item A scalar reference 
422
423 If C<$output> is a scalar reference, the compressed data will be stored
424 in C<$$output>.
425
426 =back
427
428 If the C<$output> parameter is any other type, C<IO::Compress::Deflate>::new will
429 return undef.
430
431 =head2 Constructor Options
432
433 C<OPTS> is any combination of the following options:
434
435 =over 5
436
437 =item -AutoClose =E<gt> 0|1
438
439 This option is only valid when the C<$output> parameter is a filehandle. If
440 specified, and the value is true, it will result in the C<$output> being closed
441 once either the C<close> method is called or the C<IO::Compress::Deflate> object is
442 destroyed.
443
444 This parameter defaults to 0.
445
446 =item -Append =E<gt> 0|1
447
448 Opens C<$output> in append mode. 
449
450 The behaviour of this option is dependant on the type of C<$output>.
451
452 =over 5
453
454 =item * A Buffer
455
456 If C<$output> is a buffer and C<Append> is enabled, all compressed data will be
457 append to the end if C<$output>. Otherwise C<$output> will be cleared before
458 any data is written to it.
459
460 =item * A Filename
461
462 If C<$output> is a filename and C<Append> is enabled, the file will be opened
463 in append mode. Otherwise the contents of the file, if any, will be truncated
464 before any compressed data is written to it.
465
466 =item * A Filehandle
467
468 If C<$output> is a filehandle, the file pointer will be positioned to the end
469 of the file via a call to C<seek> before any compressed data is written to it.
470 Otherwise the file pointer will not be moved.
471
472 =back
473
474 This parameter defaults to 0.
475
476 =item -Merge =E<gt> 0|1
477
478 This option is used to compress input data and append it to an existing
479 compressed data stream in C<$output>. The end result is a single compressed
480 data stream stored in C<$output>. 
481
482
483
484 It is a fatal error to attempt to use this option when C<$output> is not an RFC
485 1950 data stream.
486
487
488
489 There are a number of other limitations with the C<Merge> option:
490
491 =over 5 
492
493 =item 1
494
495 This module needs to have been built with zlib 1.2.1 or better to work. A fatal
496 error will be thrown if C<Merge> is used with an older version of zlib.  
497
498 =item 2
499
500 If C<$output> is a file or a filehandle, it must be seekable.
501
502 =back
503
504
505 This parameter defaults to 0.
506
507 =item -Level 
508
509 Defines the compression level used by zlib. The value should either be
510 a number between 0 and 9 (0 means no compression and 9 is maximum
511 compression), or one of the symbolic constants defined below.
512
513    Z_NO_COMPRESSION
514    Z_BEST_SPEED
515    Z_BEST_COMPRESSION
516    Z_DEFAULT_COMPRESSION
517
518 The default is Z_DEFAULT_COMPRESSION.
519
520 Note, these constants are not imported by C<IO::Compress::Deflate> by default.
521
522     use IO::Compress::Deflate qw(:strategy);
523     use IO::Compress::Deflate qw(:constants);
524     use IO::Compress::Deflate qw(:all);
525
526 =item -Strategy 
527
528 Defines the strategy used to tune the compression. Use one of the symbolic
529 constants defined below.
530
531    Z_FILTERED
532    Z_HUFFMAN_ONLY
533    Z_RLE
534    Z_FIXED
535    Z_DEFAULT_STRATEGY
536
537 The default is Z_DEFAULT_STRATEGY.
538
539
540
541
542
543 =item -Strict =E<gt> 0|1
544
545
546
547 This is a placeholder option.
548
549
550
551 =back
552
553 =head2 Examples
554
555 TODO
556
557 =head1 Methods 
558
559 =head2 print
560
561 Usage is
562
563     $z->print($data)
564     print $z $data
565
566 Compresses and outputs the contents of the C<$data> parameter. This
567 has the same behavior as the C<print> built-in.
568
569 Returns true if successful.
570
571 =head2 printf
572
573 Usage is
574
575     $z->printf($format, $data)
576     printf $z $format, $data
577
578 Compresses and outputs the contents of the C<$data> parameter.
579
580 Returns true if successful.
581
582 =head2 syswrite
583
584 Usage is
585
586     $z->syswrite $data
587     $z->syswrite $data, $length
588     $z->syswrite $data, $length, $offset
589
590     syswrite $z, $data
591     syswrite $z, $data, $length
592     syswrite $z, $data, $length, $offset
593
594 Compresses and outputs the contents of the C<$data> parameter.
595
596 Returns the number of uncompressed bytes written, or C<undef> if
597 unsuccessful.
598
599 =head2 write
600
601 Usage is
602
603     $z->write $data
604     $z->write $data, $length
605     $z->write $data, $length, $offset
606
607 Compresses and outputs the contents of the C<$data> parameter.
608
609 Returns the number of uncompressed bytes written, or C<undef> if
610 unsuccessful.
611
612 =head2 flush
613
614 Usage is
615
616     $z->flush;
617     $z->flush($flush_type);
618     flush $z ;
619     flush $z $flush_type;
620
621 Flushes any pending compressed data to the output file/buffer.
622
623 This method takes an optional parameter, C<$flush_type>, that controls
624 how the flushing will be carried out. By default the C<$flush_type>
625 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
626 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
627 strongly recommended that you only set the C<flush_type> parameter if
628 you fully understand the implications of what it does - overuse of C<flush>
629 can seriously degrade the level of compression achieved. See the C<zlib>
630 documentation for details.
631
632 Returns true on success.
633
634
635 =head2 tell
636
637 Usage is
638
639     $z->tell()
640     tell $z
641
642 Returns the uncompressed file offset.
643
644 =head2 eof
645
646 Usage is
647
648     $z->eof();
649     eof($z);
650
651
652
653 Returns true if the C<close> method has been called.
654
655
656
657 =head2 seek
658
659     $z->seek($position, $whence);
660     seek($z, $position, $whence);
661
662
663
664
665 Provides a sub-set of the C<seek> functionality, with the restriction
666 that it is only legal to seek forward in the output file/buffer.
667 It is a fatal error to attempt to seek backward.
668
669 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
670
671
672
673 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
674 SEEK_CUR or SEEK_END.
675
676 Returns 1 on success, 0 on failure.
677
678 =head2 binmode
679
680 Usage is
681
682     $z->binmode
683     binmode $z ;
684
685 This is a noop provided for completeness.
686
687 =head2 fileno
688
689     $z->fileno()
690     fileno($z)
691
692 If the C<$z> object is associated with a file, this method will return
693 the underlying filehandle.
694
695 If the C<$z> object is is associated with a buffer, this method will
696 return undef.
697
698 =head2 close
699
700     $z->close() ;
701     close $z ;
702
703
704
705 Flushes any pending compressed data and then closes the output file/buffer. 
706
707
708
709 For most versions of Perl this method will be automatically invoked if
710 the IO::Compress::Deflate object is destroyed (either explicitly or by the
711 variable with the reference to the object going out of scope). The
712 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
713 these cases, the C<close> method will be called automatically, but
714 not until global destruction of all live objects when the program is
715 terminating.
716
717 Therefore, if you want your scripts to be able to run on all versions
718 of Perl, you should call C<close> explicitly and not rely on automatic
719 closing.
720
721 Returns true on success, otherwise 0.
722
723 If the C<AutoClose> option has been enabled when the IO::Compress::Deflate
724 object was created, and the object is associated with a file, the
725 underlying file will also be closed.
726
727
728
729
730 =head2 newStream
731
732 Usage is
733
734     $z->newStream
735
736 TODO
737
738 =head2 deflateParams
739
740 Usage is
741
742     $z->deflateParams
743
744 TODO
745
746 =head1 Importing 
747
748 A number of symbolic constants are required by some methods in 
749 C<IO::Compress::Deflate>. None are imported by default.
750
751 =over 5
752
753 =item :all
754
755 Imports C<deflate>, C<$DeflateError> and all symbolic
756 constants that can be used by C<IO::Compress::Deflate>. Same as doing this
757
758     use IO::Compress::Deflate qw(deflate $DeflateError :constants) ;
759
760 =item :constants
761
762 Import all symbolic constants. Same as doing this
763
764     use IO::Compress::Deflate qw(:flush :level :strategy) ;
765
766 =item :flush
767
768 These symbolic constants are used by the C<flush> method.
769
770     Z_NO_FLUSH
771     Z_PARTIAL_FLUSH
772     Z_SYNC_FLUSH
773     Z_FULL_FLUSH
774     Z_FINISH
775     Z_BLOCK
776
777
778 =item :level
779
780 These symbolic constants are used by the C<Level> option in the constructor.
781
782     Z_NO_COMPRESSION
783     Z_BEST_SPEED
784     Z_BEST_COMPRESSION
785     Z_DEFAULT_COMPRESSION
786
787
788 =item :strategy
789
790 These symbolic constants are used by the C<Strategy> option in the constructor.
791
792     Z_FILTERED
793     Z_HUFFMAN_ONLY
794     Z_RLE
795     Z_FIXED
796     Z_DEFAULT_STRATEGY
797
798 =back
799
800 For 
801
802 =head1 EXAMPLES
803
804 TODO
805
806
807
808
809
810
811 =head1 SEE ALSO
812
813 L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Uncompress::AnyInflate>
814
815 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
816
817 L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
818 L<IO::Zlib|IO::Zlib>
819
820 For RFC 1950, 1951 and 1952 see 
821 F<http://www.faqs.org/rfcs/rfc1950.html>,
822 F<http://www.faqs.org/rfcs/rfc1951.html> and
823 F<http://www.faqs.org/rfcs/rfc1952.html>
824
825 The primary site for the gzip program is F<http://www.gzip.org>.
826
827 =head1 AUTHOR
828
829 The I<IO::Compress::Deflate> module was written by Paul Marquess,
830 F<pmqs@cpan.org>. The latest copy of the module can be
831 found on CPAN in F<modules/by-module/Compress/Compress-Zlib-x.x.tar.gz>.
832
833 The I<zlib> compression library was written by Jean-loup Gailly
834 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
835
836 The primary site for the I<zlib> compression library is
837 F<http://www.zlib.org>.
838
839 =head1 MODIFICATION HISTORY
840
841 See the Changes file.
842
843 =head1 COPYRIGHT AND LICENSE
844  
845
846 Copyright (c) 2005 Paul Marquess. All rights reserved.
847 This program is free software; you can redistribute it and/or
848 modify it under the same terms as Perl itself.
849
850
851
852