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