1 package IO::Compress::Deflate ;
8 use IO::Compress::RawDeflate;
10 use Compress::Zlib 2 ;
11 use Compress::Zlib::FileConstants;
12 use Compress::Zlib::Common qw(createSelfTiedObject);
15 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $DeflateError);
17 $VERSION = '2.000_07';
20 @ISA = qw(Exporter IO::Compress::RawDeflate);
21 @EXPORT_OK = qw( $DeflateError deflate ) ;
22 %EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ;
23 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
24 Exporter::export_ok_tags('all');
31 my $obj = createSelfTiedObject($class, \$DeflateError);
32 return $obj->_create(undef, @_);
37 my $obj = createSelfTiedObject(undef, \$DeflateError);
38 return $obj->_def(@_);
49 return $into | (($value & $mask) << $offset ) ;
52 sub mkDeflateHdr($$$;$)
57 my $fdict_adler = shift ;
62 $fdict = 1 if defined $fdict_adler;
64 $cmf = bitmask($cmf, $method, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS);
65 $cmf = bitmask($cmf, $cinfo, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS);
67 $flg = bitmask($flg, $fdict, ZLIB_FLG_FDICT_OFFSET, ZLIB_FLG_FDICT_BITS);
68 $flg = bitmask($flg, $level, ZLIB_FLG_LEVEL_OFFSET, ZLIB_FLG_LEVEL_BITS);
70 my $fcheck = 31 - ($cmf * 256 + $flg) % 31 ;
71 $flg = bitmask($flg, $fcheck, ZLIB_FLG_FCHECK_OFFSET, ZLIB_FLG_FCHECK_BITS);
73 my $hdr = pack("CC", $cmf, $flg) ;
74 $hdr .= pack("N", $fdict_adler) if $fdict ;
84 my $level = $param->value('Level');
85 my $strategy = $param->value('Strategy');
89 if $level == Z_DEFAULT_COMPRESSION ;
91 if (ZLIB_VERNUM >= 0x1210)
93 if ($strategy >= Z_HUFFMAN_ONLY || $level < 2)
94 { $lflag = ZLIB_FLG_LEVEL_FASTEST }
96 { $lflag = ZLIB_FLG_LEVEL_FAST }
98 { $lflag = ZLIB_FLG_LEVEL_DEFAULT }
100 { $lflag = ZLIB_FLG_LEVEL_SLOWEST }
104 $lflag = ($level - 1) >> 1 ;
105 $lflag = 3 if $lflag > 3 ;
108 #my $wbits = (MAX_WBITS - 8) << 4 ;
110 mkDeflateHdr(ZLIB_CMF_CM_DEFLATED, $wbits, $lflag);
118 $got->value('ADLER32' => 1);
126 return pack("N", *$self->{Compress}->adler32()) ;
137 # return *$self->{Header};
143 return $self->getZlibParams(),
148 return ('IO::Uncompress::Inflate',
149 \$IO::Uncompress::Inflate::InflateError);
168 IO::Compress::Deflate - Perl interface to write RFC 1950 files/buffers
172 use IO::Compress::Deflate qw(deflate $DeflateError) ;
175 my $status = deflate $input => $output [,OPTS]
176 or die "deflate failed: $DeflateError\n";
178 my $z = new IO::Compress::Deflate $output [,OPTS]
179 or die "deflate failed: $DeflateError\n";
182 $z->printf($format, $string);
184 $z->syswrite($string [, $length, $offset]);
188 $z->seek($position, $whence);
191 $z->newStream( [OPTS] );
200 printf $z $format, $string;
201 syswrite $z, $string [, $length, $offset];
205 seek $z, $position, $whence
215 B<WARNING -- This is a Beta release>.
219 =item * DO NOT use in production code.
221 =item * The documentation is incomplete in places.
223 =item * Parts of the interface defined here are tentative.
225 =item * Please report any problems you find.
231 This module provides a Perl interface that allows writing compressed
232 data to files or buffer as defined in RFC 1950.
238 For reading RFC 1950 files/buffers, see the companion module
239 L<IO::Uncompress::Inflate|IO::Uncompress::Inflate>.
242 =head1 Functional Interface
244 A top-level function, C<deflate>, is provided to carry out
245 "one-shot" compression between buffers and/or files. For finer
246 control over the compression process, see the L</"OO Interface">
249 use IO::Compress::Deflate qw(deflate $DeflateError) ;
251 deflate $input => $output [,OPTS]
252 or die "deflate failed: $DeflateError\n";
256 The functional interface needs Perl5.005 or better.
259 =head2 deflate $input => $output [, OPTS]
262 C<deflate> expects at least two parameters, C<$input> and C<$output>.
264 =head3 The C<$input> parameter
266 The parameter, C<$input>, is used to define the source of
267 the uncompressed data.
269 It can take one of the following forms:
275 If the C<$input> parameter is a simple scalar, it is assumed to be a
276 filename. This file will be opened for reading and the input data
277 will be read from it.
281 If the C<$input> parameter is a filehandle, the input data will be
283 The string '-' can be used as an alias for standard input.
285 =item A scalar reference
287 If C<$input> is a scalar reference, the input data will be read
290 =item An array reference
292 If C<$input> is an array reference, each element in the array must be a
295 The input data will be read from each file in turn.
297 The complete array will be walked to ensure that it only
298 contains valid filenames before any data is compressed.
302 =item An Input FileGlob string
304 If C<$input> is a string that is delimited by the characters "<" and ">"
305 C<deflate> will assume that it is an I<input fileglob string>. The
306 input is the list of files that match the fileglob.
308 If the fileglob does not match any files ...
310 See L<File::GlobMapper|File::GlobMapper> for more details.
315 If the C<$input> parameter is any other type, C<undef> will be returned.
319 =head3 The C<$output> parameter
321 The parameter C<$output> is used to control the destination of the
322 compressed data. This parameter can take one of these forms.
328 If the C<$output> parameter is a simple scalar, it is assumed to be a
329 filename. This file will be opened for writing and the compressed
330 data will be written to it.
334 If the C<$output> parameter is a filehandle, the compressed data
335 will be written to it.
336 The string '-' can be used as an alias for standard output.
339 =item A scalar reference
341 If C<$output> is a scalar reference, the compressed data will be
342 stored in C<$$output>.
346 =item An Array Reference
348 If C<$output> is an array reference, the compressed data will be
349 pushed onto the array.
351 =item An Output FileGlob
353 If C<$output> is a string that is delimited by the characters "<" and ">"
354 C<deflate> will assume that it is an I<output fileglob string>. The
355 output is the list of files that match the fileglob.
357 When C<$output> is an fileglob string, C<$input> must also be a fileglob
358 string. Anything else is an error.
362 If the C<$output> parameter is any other type, C<undef> will be returned.
368 When C<$input> maps to multiple files/buffers and C<$output> is a single
369 file/buffer the compressed input files/buffers will all be stored
370 in C<$output> as a single compressed stream.
374 =head2 Optional Parameters
376 Unless specified below, the optional parameters for C<deflate>,
377 C<OPTS>, are the same as those used with the OO interface defined in the
378 L</"Constructor Options"> section below.
382 =item AutoClose =E<gt> 0|1
384 This option applies to any input or output data streams to
385 C<deflate> that are filehandles.
387 If C<AutoClose> is specified, and the value is true, it will result in all
388 input and/or output filehandles being closed once C<deflate> has
391 This parameter defaults to 0.
395 =item BinModeIn =E<gt> 0|1
397 When reading from a file or filehandle, set C<binmode> before reading.
405 =item -Append =E<gt> 0|1
416 To read the contents of the file C<file1.txt> and write the compressed
417 data to the file C<file1.txt.1950>.
421 use IO::Compress::Deflate qw(deflate $DeflateError) ;
423 my $input = "file1.txt";
424 deflate $input => "$input.1950"
425 or die "deflate failed: $DeflateError\n";
428 To read from an existing Perl filehandle, C<$input>, and write the
429 compressed data to a buffer, C<$buffer>.
433 use IO::Compress::Deflate qw(deflate $DeflateError) ;
436 my $input = new IO::File "<file1.txt"
437 or die "Cannot open 'file1.txt': $!\n" ;
439 deflate $input => \$buffer
440 or die "deflate failed: $DeflateError\n";
442 To compress all files in the directory "/my/home" that match "*.txt"
443 and store the compressed data in the same directory
447 use IO::Compress::Deflate qw(deflate $DeflateError) ;
449 deflate '</my/home/*.txt>' => '<*.1950>'
450 or die "deflate failed: $DeflateError\n";
452 and if you want to compress each file one at a time, this will do the trick
456 use IO::Compress::Deflate qw(deflate $DeflateError) ;
458 for my $input ( glob "/my/home/*.txt" )
460 my $output = "$input.1950" ;
461 deflate $input => $output
462 or die "Error compressing '$input': $DeflateError\n";
470 The format of the constructor for C<IO::Compress::Deflate> is shown below
472 my $z = new IO::Compress::Deflate $output [,OPTS]
473 or die "IO::Compress::Deflate failed: $DeflateError\n";
475 It returns an C<IO::Compress::Deflate> object on success and undef on failure.
476 The variable C<$DeflateError> will contain an error message on failure.
478 If you are running Perl 5.005 or better the object, C<$z>, returned from
479 IO::Compress::Deflate can be used exactly like an L<IO::File|IO::File> filehandle.
480 This means that all normal output file operations can be carried out
482 For example, to write to a compressed file/buffer you can use either of
485 $z->print("hello world\n");
486 print $z "hello world\n";
488 The mandatory parameter C<$output> is used to control the destination
489 of the compressed data. This parameter can take one of these forms.
495 If the C<$output> parameter is a simple scalar, it is assumed to be a
496 filename. This file will be opened for writing and the compressed data
497 will be written to it.
501 If the C<$output> parameter is a filehandle, the compressed data will be
503 The string '-' can be used as an alias for standard output.
506 =item A scalar reference
508 If C<$output> is a scalar reference, the compressed data will be stored
513 If the C<$output> parameter is any other type, C<IO::Compress::Deflate>::new will
516 =head2 Constructor Options
518 C<OPTS> is any combination of the following options:
522 =item -AutoClose =E<gt> 0|1
524 This option is only valid when the C<$output> parameter is a filehandle. If
525 specified, and the value is true, it will result in the C<$output> being
526 closed once either the C<close> method is called or the C<IO::Compress::Deflate>
529 This parameter defaults to 0.
531 =item -Append =E<gt> 0|1
533 Opens C<$output> in append mode.
535 The behaviour of this option is dependent on the type of C<$output>.
541 If C<$output> is a buffer and C<Append> is enabled, all compressed data
542 will be append to the end if C<$output>. Otherwise C<$output> will be
543 cleared before any data is written to it.
547 If C<$output> is a filename and C<Append> is enabled, the file will be
548 opened in append mode. Otherwise the contents of the file, if any, will be
549 truncated before any compressed data is written to it.
553 If C<$output> is a filehandle, the file pointer will be positioned to the
554 end of the file via a call to C<seek> before any compressed data is written
555 to it. Otherwise the file pointer will not be moved.
559 This parameter defaults to 0.
561 =item -Merge =E<gt> 0|1
563 This option is used to compress input data and append it to an existing
564 compressed data stream in C<$output>. The end result is a single compressed
565 data stream stored in C<$output>.
569 It is a fatal error to attempt to use this option when C<$output> is not an
570 RFC 1950 data stream.
574 There are a number of other limitations with the C<Merge> option:
580 This module needs to have been built with zlib 1.2.1 or better to work. A
581 fatal error will be thrown if C<Merge> is used with an older version of
586 If C<$output> is a file or a filehandle, it must be seekable.
591 This parameter defaults to 0.
595 Defines the compression level used by zlib. The value should either be
596 a number between 0 and 9 (0 means no compression and 9 is maximum
597 compression), or one of the symbolic constants defined below.
602 Z_DEFAULT_COMPRESSION
604 The default is Z_DEFAULT_COMPRESSION.
606 Note, these constants are not imported by C<IO::Compress::Deflate> by default.
608 use IO::Compress::Deflate qw(:strategy);
609 use IO::Compress::Deflate qw(:constants);
610 use IO::Compress::Deflate qw(:all);
614 Defines the strategy used to tune the compression. Use one of the symbolic
615 constants defined below.
623 The default is Z_DEFAULT_STRATEGY.
629 =item -Strict =E<gt> 0|1
633 This is a placeholder option.
652 Compresses and outputs the contents of the C<$data> parameter. This
653 has the same behaviour as the C<print> built-in.
655 Returns true if successful.
661 $z->printf($format, $data)
662 printf $z $format, $data
664 Compresses and outputs the contents of the C<$data> parameter.
666 Returns true if successful.
673 $z->syswrite $data, $length
674 $z->syswrite $data, $length, $offset
677 syswrite $z, $data, $length
678 syswrite $z, $data, $length, $offset
680 Compresses and outputs the contents of the C<$data> parameter.
682 Returns the number of uncompressed bytes written, or C<undef> if
690 $z->write $data, $length
691 $z->write $data, $length, $offset
693 Compresses and outputs the contents of the C<$data> parameter.
695 Returns the number of uncompressed bytes written, or C<undef> if
703 $z->flush($flush_type);
705 flush $z $flush_type;
707 Flushes any pending compressed data to the output file/buffer.
709 This method takes an optional parameter, C<$flush_type>, that controls
710 how the flushing will be carried out. By default the C<$flush_type>
711 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
712 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
713 strongly recommended that you only set the C<flush_type> parameter if
714 you fully understand the implications of what it does - overuse of C<flush>
715 can seriously degrade the level of compression achieved. See the C<zlib>
716 documentation for details.
718 Returns true on success.
728 Returns the uncompressed file offset.
739 Returns true if the C<close> method has been called.
745 $z->seek($position, $whence);
746 seek($z, $position, $whence);
751 Provides a sub-set of the C<seek> functionality, with the restriction
752 that it is only legal to seek forward in the output file/buffer.
753 It is a fatal error to attempt to seek backward.
755 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
759 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
760 SEEK_CUR or SEEK_END.
762 Returns 1 on success, 0 on failure.
771 This is a noop provided for completeness.
778 If the C<$z> object is associated with a file, this method will return
779 the underlying filehandle.
781 If the C<$z> object is is associated with a buffer, this method will
791 Flushes any pending compressed data and then closes the output file/buffer.
795 For most versions of Perl this method will be automatically invoked if
796 the IO::Compress::Deflate object is destroyed (either explicitly or by the
797 variable with the reference to the object going out of scope). The
798 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
799 these cases, the C<close> method will be called automatically, but
800 not until global destruction of all live objects when the program is
803 Therefore, if you want your scripts to be able to run on all versions
804 of Perl, you should call C<close> explicitly and not rely on automatic
807 Returns true on success, otherwise 0.
809 If the C<AutoClose> option has been enabled when the IO::Compress::Deflate
810 object was created, and the object is associated with a file, the
811 underlying file will also be closed.
816 =head2 newStream([OPTS])
820 $z->newStream( [OPTS] )
822 Closes the current compressed data stream and starts a new one.
824 OPTS consists of the following sub-set of the the options that are
825 available when creating the C<$z> object,
845 A number of symbolic constants are required by some methods in
846 C<IO::Compress::Deflate>. None are imported by default.
852 Imports C<deflate>, C<$DeflateError> and all symbolic
853 constants that can be used by C<IO::Compress::Deflate>. Same as doing this
855 use IO::Compress::Deflate qw(deflate $DeflateError :constants) ;
859 Import all symbolic constants. Same as doing this
861 use IO::Compress::Deflate qw(:flush :level :strategy) ;
865 These symbolic constants are used by the C<flush> method.
877 These symbolic constants are used by the C<Level> option in the constructor.
882 Z_DEFAULT_COMPRESSION
887 These symbolic constants are used by the C<Strategy> option in the constructor.
910 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>
912 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
914 L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
917 For RFC 1950, 1951 and 1952 see
918 F<http://www.faqs.org/rfcs/rfc1950.html>,
919 F<http://www.faqs.org/rfcs/rfc1951.html> and
920 F<http://www.faqs.org/rfcs/rfc1952.html>
922 The primary site for the gzip program is F<http://www.gzip.org>.
926 The I<IO::Compress::Deflate> module was written by Paul Marquess,
927 F<pmqs@cpan.org>. The latest copy of the module can be
928 found on CPAN in F<modules/by-module/Compress/Compress-Zlib-x.x.tar.gz>.
930 The I<zlib> compression library was written by Jean-loup Gailly
931 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
933 The primary site for the I<zlib> compression library is
934 F<http://www.zlib.org>.
936 =head1 MODIFICATION HISTORY
938 See the Changes file.
940 =head1 COPYRIGHT AND LICENSE
943 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
944 This program is free software; you can redistribute it and/or
945 modify it under the same terms as Perl itself.