1 package IO::Compress::Deflate ;
7 use IO::Compress::Gzip ;
10 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $DeflateError);
12 $VERSION = '2.000_05';
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');
26 return IO::BaseDeflate::new($pkg, 'rfc1950', undef, \$DeflateError, @_);
31 return IO::BaseDeflate::_def(__PACKAGE__, 'rfc1950', \$DeflateError, @_);
41 IO::Compress::Deflate - Perl interface to write RFC 1950 files/buffers
45 use IO::Compress::Deflate qw(deflate $DeflateError) ;
48 my $status = deflate $input => $output [,OPTS]
49 or die "deflate failed: $DeflateError\n";
51 my $z = new IO::Compress::Deflate $output [,OPTS]
52 or die "deflate failed: $DeflateError\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 1950.
111 For reading RFC 1950 files/buffers, see the companion module
112 L<IO::Uncompress::Inflate|IO::Uncompress::Inflate>.
115 =head1 Functional Interface
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.
120 use IO::Compress::Deflate qw(deflate $DeflateError) ;
122 deflate $input => $output [,OPTS]
123 or die "deflate failed: $DeflateError\n";
125 deflate \%hash [,OPTS]
126 or die "deflate failed: $DeflateError\n";
128 The functional interface needs Perl5.005 or better.
131 =head2 deflate $input => $output [, OPTS]
133 If the first parameter is not a hash reference C<deflate> expects
134 at least two parameters, C<$input> and C<$output>.
136 =head3 The C<$input> parameter
138 The parameter, C<$input>, is used to define the source of
139 the uncompressed data.
141 It can take one of the following forms:
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.
153 If the C<$input> parameter is a filehandle, the input data will be
155 The string '-' can be used as an alias for standard input.
157 =item A scalar reference
159 If C<$input> is a scalar reference, the input data will be read
162 =item An array reference
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.
172 =item An Input FileGlob string
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.
178 If the fileglob does not match any files ...
180 See L<File::GlobMapper|File::GlobMapper> for more details.
185 If the C<$input> parameter is any other type, C<undef> will be returned.
189 =head3 The C<$output> parameter
191 The parameter C<$output> is used to control the destination of the
192 compressed data. This parameter can take one of these forms.
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
204 If the C<$output> parameter is a filehandle, the compressed data will
206 The string '-' can be used as an alias for standard output.
209 =item A scalar reference
211 If C<$output> is a scalar reference, the compressed data will be stored
215 =item A Hash Reference
217 If C<$output> is a hash reference, the compressed data will be written
218 to C<$output{$input}> as a scalar reference.
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.
224 =item An Array Reference
226 If C<$output> is an array reference, the compressed data will be pushed
229 =item An Output FileGlob
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.
235 When C<$output> is an fileglob string, C<$input> must also be a fileglob
236 string. Anything else is an error.
240 If the C<$output> parameter is any other type, C<undef> will be returned.
242 =head2 deflate \%hash [, OPTS]
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
256 If the value is C<undef> the compressed data will be written to the
257 value as a scalar reference.
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.
266 If the value is a filehandle, the compressed data will be
268 The string '-' can be used as an alias for standard output.
271 =item A scalar reference
273 If the value is a scalar reference, the compressed data will be stored
274 in the buffer that is referenced by the scalar.
277 =item A Hash Reference
279 If the value is a hash reference, the compressed data will be written
280 to C<$hash{$input}> as a scalar reference.
282 =item An Array Reference
284 If C<$output> is an array reference, the compressed data will be pushed
289 Any other type is a error.
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.
299 =head2 Optional Parameters
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.
307 =item AutoClose =E<gt> 0|1
309 This option applies to any input or output data streams to C<deflate>
310 that are filehandles.
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
316 This parameter defaults to 0.
320 =item -Append =E<gt> 0|1
331 To read the contents of the file C<file1.txt> and write the compressed
332 data to the file C<file1.txt.1950>.
336 use IO::Compress::Deflate qw(deflate $DeflateError) ;
338 my $input = "file1.txt";
339 deflate $input => "$input.1950"
340 or die "deflate failed: $DeflateError\n";
343 To read from an existing Perl filehandle, C<$input>, and write the
344 compressed data to a buffer, C<$buffer>.
348 use IO::Compress::Deflate qw(deflate $DeflateError) ;
351 my $input = new IO::File "<file1.txt"
352 or die "Cannot open 'file1.txt': $!\n" ;
354 deflate $input => \$buffer
355 or die "deflate failed: $DeflateError\n";
357 To compress all files in the directory "/my/home" that match "*.txt"
358 and store the compressed data in the same directory
362 use IO::Compress::Deflate qw(deflate $DeflateError) ;
364 deflate '</my/home/*.txt>' => '<*.1950>'
365 or die "deflate failed: $DeflateError\n";
367 and if you want to compress each file one at a time, this will do the trick
371 use IO::Compress::Deflate qw(deflate $DeflateError) ;
373 for my $input ( glob "/my/home/*.txt" )
375 my $output = "$input.1950" ;
376 deflate $input => $output
377 or die "Error compressing '$input': $DeflateError\n";
385 The format of the constructor for C<IO::Compress::Deflate> is shown below
387 my $z = new IO::Compress::Deflate $output [,OPTS]
388 or die "IO::Compress::Deflate failed: $DeflateError\n";
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.
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
397 For example, to write to a compressed file/buffer you can use either of
400 $z->print("hello world\n");
401 print $z "hello world\n";
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.
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.
416 If the C<$output> parameter is a filehandle, the compressed data will be
418 The string '-' can be used as an alias for standard output.
421 =item A scalar reference
423 If C<$output> is a scalar reference, the compressed data will be stored
428 If the C<$output> parameter is any other type, C<IO::Compress::Deflate>::new will
431 =head2 Constructor Options
433 C<OPTS> is any combination of the following options:
437 =item -AutoClose =E<gt> 0|1
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
444 This parameter defaults to 0.
446 =item -Append =E<gt> 0|1
448 Opens C<$output> in append mode.
450 The behaviour of this option is dependant on the type of C<$output>.
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.
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.
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.
474 This parameter defaults to 0.
476 =item -Merge =E<gt> 0|1
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>.
484 It is a fatal error to attempt to use this option when C<$output> is not an RFC
489 There are a number of other limitations with the C<Merge> option:
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.
500 If C<$output> is a file or a filehandle, it must be seekable.
505 This parameter defaults to 0.
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.
516 Z_DEFAULT_COMPRESSION
518 The default is Z_DEFAULT_COMPRESSION.
520 Note, these constants are not imported by C<IO::Compress::Deflate> by default.
522 use IO::Compress::Deflate qw(:strategy);
523 use IO::Compress::Deflate qw(:constants);
524 use IO::Compress::Deflate qw(:all);
528 Defines the strategy used to tune the compression. Use one of the symbolic
529 constants defined below.
537 The default is Z_DEFAULT_STRATEGY.
543 =item -Strict =E<gt> 0|1
547 This is a placeholder option.
566 Compresses and outputs the contents of the C<$data> parameter. This
567 has the same behavior as the C<print> built-in.
569 Returns true if successful.
575 $z->printf($format, $data)
576 printf $z $format, $data
578 Compresses and outputs the contents of the C<$data> parameter.
580 Returns true if successful.
587 $z->syswrite $data, $length
588 $z->syswrite $data, $length, $offset
591 syswrite $z, $data, $length
592 syswrite $z, $data, $length, $offset
594 Compresses and outputs the contents of the C<$data> parameter.
596 Returns the number of uncompressed bytes written, or C<undef> if
604 $z->write $data, $length
605 $z->write $data, $length, $offset
607 Compresses and outputs the contents of the C<$data> parameter.
609 Returns the number of uncompressed bytes written, or C<undef> if
617 $z->flush($flush_type);
619 flush $z $flush_type;
621 Flushes any pending compressed data to the output file/buffer.
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.
632 Returns true on success.
642 Returns the uncompressed file offset.
653 Returns true if the C<close> method has been called.
659 $z->seek($position, $whence);
660 seek($z, $position, $whence);
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.
669 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
673 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
674 SEEK_CUR or SEEK_END.
676 Returns 1 on success, 0 on failure.
685 This is a noop provided for completeness.
692 If the C<$z> object is associated with a file, this method will return
693 the underlying filehandle.
695 If the C<$z> object is is associated with a buffer, this method will
705 Flushes any pending compressed data and then closes the output file/buffer.
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
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
721 Returns true on success, otherwise 0.
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.
748 A number of symbolic constants are required by some methods in
749 C<IO::Compress::Deflate>. None are imported by default.
755 Imports C<deflate>, C<$DeflateError> and all symbolic
756 constants that can be used by C<IO::Compress::Deflate>. Same as doing this
758 use IO::Compress::Deflate qw(deflate $DeflateError :constants) ;
762 Import all symbolic constants. Same as doing this
764 use IO::Compress::Deflate qw(:flush :level :strategy) ;
768 These symbolic constants are used by the C<flush> method.
780 These symbolic constants are used by the C<Level> option in the constructor.
785 Z_DEFAULT_COMPRESSION
790 These symbolic constants are used by the C<Strategy> option in the constructor.
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>
815 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
817 L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
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>
825 The primary site for the gzip program is F<http://www.gzip.org>.
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>.
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>.
836 The primary site for the I<zlib> compression library is
837 F<http://www.zlib.org>.
839 =head1 MODIFICATION HISTORY
841 See the Changes file.
843 =head1 COPYRIGHT AND LICENSE
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.