1 package IO::Compress::RawDeflate ;
7 use IO::Uncompress::RawInflate;
11 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $RawDeflateError);
13 $VERSION = '2.000_05';
14 $RawDeflateError = '';
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');
27 return IO::BaseDeflate::new($pkg, 'rfc1951', undef, \$RawDeflateError, @_);
32 return IO::BaseDeflate::_def(__PACKAGE__, 'rfc1951', \$RawDeflateError, @_);
41 IO::Compress::RawDeflate - Perl interface to write RFC 1951 files/buffers
45 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
48 my $status = rawdeflate $input => $output [,OPTS]
49 or die "rawdeflate failed: $RawDeflateError\n";
51 my $z = new IO::Compress::RawDeflate $output [,OPTS]
52 or die "rawdeflate failed: $RawDeflateError\n";
55 $z->printf($format, $string);
57 $z->syswrite($string [, $length, $offset]);
61 $z->seek($position, $whence);
73 printf $z $format, $string;
74 syswrite $z, $string [, $length, $offset];
78 seek $z, $position, $whence
88 B<WARNING -- This is a Beta release>.
92 =item * DO NOT use in production code.
94 =item * The documentation is incomplete in places.
96 =item * Parts of the interface defined here are tentative.
98 =item * Please report any problems you find.
104 This module provides a Perl interface that allows writing compressed
105 data to files or buffer as defined in RFC 1951.
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.
114 For reading RFC 1951 files/buffers, see the companion module
115 L<IO::Uncompress::RawInflate|IO::Uncompress::RawInflate>.
118 =head1 Functional Interface
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.
123 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
125 rawdeflate $input => $output [,OPTS]
126 or die "rawdeflate failed: $RawDeflateError\n";
128 rawdeflate \%hash [,OPTS]
129 or die "rawdeflate failed: $RawDeflateError\n";
131 The functional interface needs Perl5.005 or better.
134 =head2 rawdeflate $input => $output [, OPTS]
136 If the first parameter is not a hash reference C<rawdeflate> expects
137 at least two parameters, C<$input> and C<$output>.
139 =head3 The C<$input> parameter
141 The parameter, C<$input>, is used to define the source of
142 the uncompressed data.
144 It can take one of the following forms:
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.
156 If the C<$input> parameter is a filehandle, the input data will be
158 The string '-' can be used as an alias for standard input.
160 =item A scalar reference
162 If C<$input> is a scalar reference, the input data will be read
165 =item An array reference
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.
175 =item An Input FileGlob string
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.
181 If the fileglob does not match any files ...
183 See L<File::GlobMapper|File::GlobMapper> for more details.
188 If the C<$input> parameter is any other type, C<undef> will be returned.
192 =head3 The C<$output> parameter
194 The parameter C<$output> is used to control the destination of the
195 compressed data. This parameter can take one of these forms.
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
207 If the C<$output> parameter is a filehandle, the compressed data will
209 The string '-' can be used as an alias for standard output.
212 =item A scalar reference
214 If C<$output> is a scalar reference, the compressed data will be stored
218 =item A Hash Reference
220 If C<$output> is a hash reference, the compressed data will be written
221 to C<$output{$input}> as a scalar reference.
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.
227 =item An Array Reference
229 If C<$output> is an array reference, the compressed data will be pushed
232 =item An Output FileGlob
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.
238 When C<$output> is an fileglob string, C<$input> must also be a fileglob
239 string. Anything else is an error.
243 If the C<$output> parameter is any other type, C<undef> will be returned.
245 =head2 rawdeflate \%hash [, OPTS]
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
259 If the value is C<undef> the compressed data will be written to the
260 value as a scalar reference.
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.
269 If the value is a filehandle, the compressed data will be
271 The string '-' can be used as an alias for standard output.
274 =item A scalar reference
276 If the value is a scalar reference, the compressed data will be stored
277 in the buffer that is referenced by the scalar.
280 =item A Hash Reference
282 If the value is a hash reference, the compressed data will be written
283 to C<$hash{$input}> as a scalar reference.
285 =item An Array Reference
287 If C<$output> is an array reference, the compressed data will be pushed
292 Any other type is a error.
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.
302 =head2 Optional Parameters
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.
310 =item AutoClose =E<gt> 0|1
312 This option applies to any input or output data streams to C<rawdeflate>
313 that are filehandles.
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
319 This parameter defaults to 0.
323 =item -Append =E<gt> 0|1
334 To read the contents of the file C<file1.txt> and write the compressed
335 data to the file C<file1.txt.1951>.
339 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
341 my $input = "file1.txt";
342 rawdeflate $input => "$input.1951"
343 or die "rawdeflate failed: $RawDeflateError\n";
346 To read from an existing Perl filehandle, C<$input>, and write the
347 compressed data to a buffer, C<$buffer>.
351 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
354 my $input = new IO::File "<file1.txt"
355 or die "Cannot open 'file1.txt': $!\n" ;
357 rawdeflate $input => \$buffer
358 or die "rawdeflate failed: $RawDeflateError\n";
360 To compress all files in the directory "/my/home" that match "*.txt"
361 and store the compressed data in the same directory
365 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
367 rawdeflate '</my/home/*.txt>' => '<*.1951>'
368 or die "rawdeflate failed: $RawDeflateError\n";
370 and if you want to compress each file one at a time, this will do the trick
374 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
376 for my $input ( glob "/my/home/*.txt" )
378 my $output = "$input.1951" ;
379 rawdeflate $input => $output
380 or die "Error compressing '$input': $RawDeflateError\n";
388 The format of the constructor for C<IO::Compress::RawDeflate> is shown below
390 my $z = new IO::Compress::RawDeflate $output [,OPTS]
391 or die "IO::Compress::RawDeflate failed: $RawDeflateError\n";
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.
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
400 For example, to write to a compressed file/buffer you can use either of
403 $z->print("hello world\n");
404 print $z "hello world\n";
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.
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.
419 If the C<$output> parameter is a filehandle, the compressed data will be
421 The string '-' can be used as an alias for standard output.
424 =item A scalar reference
426 If C<$output> is a scalar reference, the compressed data will be stored
431 If the C<$output> parameter is any other type, C<IO::Compress::RawDeflate>::new will
434 =head2 Constructor Options
436 C<OPTS> is any combination of the following options:
440 =item -AutoClose =E<gt> 0|1
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
447 This parameter defaults to 0.
449 =item -Append =E<gt> 0|1
451 Opens C<$output> in append mode.
453 The behaviour of this option is dependant on the type of C<$output>.
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.
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.
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.
477 This parameter defaults to 0.
479 =item -Merge =E<gt> 0|1
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>.
487 It is a fatal error to attempt to use this option when C<$output> is not an RFC
492 There are a number of other limitations with the C<Merge> option:
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.
503 If C<$output> is a file or a filehandle, it must be seekable.
508 This parameter defaults to 0.
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.
519 Z_DEFAULT_COMPRESSION
521 The default is Z_DEFAULT_COMPRESSION.
523 Note, these constants are not imported by C<IO::Compress::RawDeflate> by default.
525 use IO::Compress::RawDeflate qw(:strategy);
526 use IO::Compress::RawDeflate qw(:constants);
527 use IO::Compress::RawDeflate qw(:all);
531 Defines the strategy used to tune the compression. Use one of the symbolic
532 constants defined below.
540 The default is Z_DEFAULT_STRATEGY.
546 =item -Strict =E<gt> 0|1
550 This is a placeholder option.
569 Compresses and outputs the contents of the C<$data> parameter. This
570 has the same behavior as the C<print> built-in.
572 Returns true if successful.
578 $z->printf($format, $data)
579 printf $z $format, $data
581 Compresses and outputs the contents of the C<$data> parameter.
583 Returns true if successful.
590 $z->syswrite $data, $length
591 $z->syswrite $data, $length, $offset
594 syswrite $z, $data, $length
595 syswrite $z, $data, $length, $offset
597 Compresses and outputs the contents of the C<$data> parameter.
599 Returns the number of uncompressed bytes written, or C<undef> if
607 $z->write $data, $length
608 $z->write $data, $length, $offset
610 Compresses and outputs the contents of the C<$data> parameter.
612 Returns the number of uncompressed bytes written, or C<undef> if
620 $z->flush($flush_type);
622 flush $z $flush_type;
624 Flushes any pending compressed data to the output file/buffer.
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.
635 Returns true on success.
645 Returns the uncompressed file offset.
656 Returns true if the C<close> method has been called.
662 $z->seek($position, $whence);
663 seek($z, $position, $whence);
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.
672 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
676 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
677 SEEK_CUR or SEEK_END.
679 Returns 1 on success, 0 on failure.
688 This is a noop provided for completeness.
695 If the C<$z> object is associated with a file, this method will return
696 the underlying filehandle.
698 If the C<$z> object is is associated with a buffer, this method will
708 Flushes any pending compressed data and then closes the output file/buffer.
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
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
724 Returns true on success, otherwise 0.
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.
751 A number of symbolic constants are required by some methods in
752 C<IO::Compress::RawDeflate>. None are imported by default.
758 Imports C<rawdeflate>, C<$RawDeflateError> and all symbolic
759 constants that can be used by C<IO::Compress::RawDeflate>. Same as doing this
761 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError :constants) ;
765 Import all symbolic constants. Same as doing this
767 use IO::Compress::RawDeflate qw(:flush :level :strategy) ;
771 These symbolic constants are used by the C<flush> method.
783 These symbolic constants are used by the C<Level> option in the constructor.
788 Z_DEFAULT_COMPRESSION
793 These symbolic constants are used by the C<Strategy> option in the constructor.
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>
818 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
820 L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
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>
828 The primary site for the gzip program is F<http://www.gzip.org>.
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>.
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>.
839 The primary site for the I<zlib> compression library is
840 F<http://www.zlib.org>.
842 =head1 MODIFICATION HISTORY
844 See the Changes file.
846 =head1 COPYRIGHT AND LICENSE
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.