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