1 package IO::Compress::Deflate ;
11 use IO::Compress::RawDeflate 2.052 ();
12 use IO::Compress::Adapter::Deflate 2.052 ;
14 use IO::Compress::Zlib::Constants 2.052 ;
15 use IO::Compress::Base::Common 2.052 qw(createSelfTiedObject);
18 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, %DEFLATE_CONSTANTS, $DeflateError);
23 @ISA = qw(Exporter IO::Compress::RawDeflate);
24 @EXPORT_OK = qw( $DeflateError deflate ) ;
25 %EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ;
27 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
28 Exporter::export_ok_tags('all');
35 my $obj = createSelfTiedObject($class, \$DeflateError);
36 return $obj->_create(undef, @_);
41 my $obj = createSelfTiedObject(undef, \$DeflateError);
42 return $obj->_def(@_);
53 return $into | (($value & $mask) << $offset ) ;
56 sub mkDeflateHdr($$$;$)
61 my $fdict_adler = shift ;
66 $fdict = 1 if defined $fdict_adler;
68 $cmf = bitmask($cmf, $method, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS);
69 $cmf = bitmask($cmf, $cinfo, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS);
71 $flg = bitmask($flg, $fdict, ZLIB_FLG_FDICT_OFFSET, ZLIB_FLG_FDICT_BITS);
72 $flg = bitmask($flg, $level, ZLIB_FLG_LEVEL_OFFSET, ZLIB_FLG_LEVEL_BITS);
74 my $fcheck = 31 - ($cmf * 256 + $flg) % 31 ;
75 $flg = bitmask($flg, $fcheck, ZLIB_FLG_FCHECK_OFFSET, ZLIB_FLG_FCHECK_BITS);
77 my $hdr = pack("CC", $cmf, $flg) ;
78 $hdr .= pack("N", $fdict_adler) if $fdict ;
88 my $level = $param->value('Level');
89 my $strategy = $param->value('Strategy');
93 if $level == Z_DEFAULT_COMPRESSION ;
95 if (ZLIB_VERNUM >= 0x1210)
97 if ($strategy >= Z_HUFFMAN_ONLY || $level < 2)
98 { $lflag = ZLIB_FLG_LEVEL_FASTEST }
100 { $lflag = ZLIB_FLG_LEVEL_FAST }
102 { $lflag = ZLIB_FLG_LEVEL_DEFAULT }
104 { $lflag = ZLIB_FLG_LEVEL_SLOWEST }
108 $lflag = ($level - 1) >> 1 ;
109 $lflag = 3 if $lflag > 3 ;
112 #my $wbits = (MAX_WBITS - 8) << 4 ;
114 mkDeflateHdr(ZLIB_CMF_CM_DEFLATED, $wbits, $lflag);
122 $got->value('ADLER32' => 1);
130 return pack("N", *$self->{Compress}->adler32()) ;
141 # return *$self->{Header};
147 return $self->getZlibParams(),
152 return ('IO::Uncompress::Inflate',
153 \$IO::Uncompress::Inflate::InflateError);
172 IO::Compress::Deflate - Write RFC 1950 files/buffers
178 use IO::Compress::Deflate qw(deflate $DeflateError) ;
180 my $status = deflate $input => $output [,OPTS]
181 or die "deflate failed: $DeflateError\n";
183 my $z = new IO::Compress::Deflate $output [,OPTS]
184 or die "deflate failed: $DeflateError\n";
187 $z->printf($format, $string);
189 $z->syswrite($string [, $length, $offset]);
193 $z->seek($position, $whence);
198 $z->input_line_number();
199 $z->newStream( [OPTS] );
210 printf $z $format, $string;
213 seek $z, $position, $whence
221 This module provides a Perl interface that allows writing compressed
222 data to files or buffer as defined in RFC 1950.
224 For reading RFC 1950 files/buffers, see the companion module
225 L<IO::Uncompress::Inflate|IO::Uncompress::Inflate>.
227 =head1 Functional Interface
229 A top-level function, C<deflate>, is provided to carry out
230 "one-shot" compression between buffers and/or files. For finer
231 control over the compression process, see the L</"OO Interface">
234 use IO::Compress::Deflate qw(deflate $DeflateError) ;
236 deflate $input => $output [,OPTS]
237 or die "deflate failed: $DeflateError\n";
239 The functional interface needs Perl5.005 or better.
241 =head2 deflate $input => $output [, OPTS]
243 C<deflate> expects at least two parameters, C<$input> and C<$output>.
245 =head3 The C<$input> parameter
247 The parameter, C<$input>, is used to define the source of
248 the uncompressed data.
250 It can take one of the following forms:
256 If the C<$input> parameter is a simple scalar, it is assumed to be a
257 filename. This file will be opened for reading and the input data
258 will be read from it.
262 If the C<$input> parameter is a filehandle, the input data will be
264 The string '-' can be used as an alias for standard input.
266 =item A scalar reference
268 If C<$input> is a scalar reference, the input data will be read
271 =item An array reference
273 If C<$input> is an array reference, each element in the array must be a
276 The input data will be read from each file in turn.
278 The complete array will be walked to ensure that it only
279 contains valid filenames before any data is compressed.
281 =item An Input FileGlob string
283 If C<$input> is a string that is delimited by the characters "<" and ">"
284 C<deflate> will assume that it is an I<input fileglob string>. The
285 input is the list of files that match the fileglob.
287 See L<File::GlobMapper|File::GlobMapper> for more details.
291 If the C<$input> parameter is any other type, C<undef> will be returned.
293 =head3 The C<$output> parameter
295 The parameter C<$output> is used to control the destination of the
296 compressed data. This parameter can take one of these forms.
302 If the C<$output> parameter is a simple scalar, it is assumed to be a
303 filename. This file will be opened for writing and the compressed
304 data will be written to it.
308 If the C<$output> parameter is a filehandle, the compressed data
309 will be written to it.
310 The string '-' can be used as an alias for standard output.
312 =item A scalar reference
314 If C<$output> is a scalar reference, the compressed data will be
315 stored in C<$$output>.
317 =item An Array Reference
319 If C<$output> is an array reference, the compressed data will be
320 pushed onto the array.
322 =item An Output FileGlob
324 If C<$output> is a string that is delimited by the characters "<" and ">"
325 C<deflate> will assume that it is an I<output fileglob string>. The
326 output is the list of files that match the fileglob.
328 When C<$output> is an fileglob string, C<$input> must also be a fileglob
329 string. Anything else is an error.
331 See L<File::GlobMapper|File::GlobMapper> for more details.
335 If the C<$output> parameter is any other type, C<undef> will be returned.
339 When C<$input> maps to multiple files/buffers and C<$output> is a single
340 file/buffer the input files/buffers will be stored
341 in C<$output> as a concatenated series of compressed data streams.
343 =head2 Optional Parameters
345 Unless specified below, the optional parameters for C<deflate>,
346 C<OPTS>, are the same as those used with the OO interface defined in the
347 L</"Constructor Options"> section below.
351 =item C<< AutoClose => 0|1 >>
353 This option applies to any input or output data streams to
354 C<deflate> that are filehandles.
356 If C<AutoClose> is specified, and the value is true, it will result in all
357 input and/or output filehandles being closed once C<deflate> has
360 This parameter defaults to 0.
362 =item C<< BinModeIn => 0|1 >>
364 When reading from a file or filehandle, set C<binmode> before reading.
368 =item C<< Append => 0|1 >>
370 The behaviour of this option is dependent on the type of output data
377 If C<Append> is enabled, all compressed data will be append to the end of
378 the output buffer. Otherwise the output buffer will be cleared before any
379 compressed data is written to it.
383 If C<Append> is enabled, the file will be opened in append mode. Otherwise
384 the contents of the file, if any, will be truncated before any compressed
385 data is written to it.
389 If C<Append> is enabled, the filehandle will be positioned to the end of
390 the file via a call to C<seek> before any compressed data is
391 written to it. Otherwise the file pointer will not be moved.
395 When C<Append> is specified, and set to true, it will I<append> all compressed
396 data to the output data stream.
398 So when the output is a filehandle it will carry out a seek to the eof
399 before writing any compressed data. If the output is a filename, it will be opened for
400 appending. If the output is a buffer, all compressed data will be
401 appended to the existing buffer.
403 Conversely when C<Append> is not specified, or it is present and is set to
404 false, it will operate as follows.
406 When the output is a filename, it will truncate the contents of the file
407 before writing any compressed data. If the output is a filehandle
408 its position will not be changed. If the output is a buffer, it will be
409 wiped before any compressed data is output.
417 To read the contents of the file C<file1.txt> and write the compressed
418 data to the file C<file1.txt.1950>.
422 use IO::Compress::Deflate qw(deflate $DeflateError) ;
424 my $input = "file1.txt";
425 deflate $input => "$input.1950"
426 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";
469 The format of the constructor for C<IO::Compress::Deflate> is shown below
471 my $z = new IO::Compress::Deflate $output [,OPTS]
472 or die "IO::Compress::Deflate failed: $DeflateError\n";
474 It returns an C<IO::Compress::Deflate> object on success and undef on failure.
475 The variable C<$DeflateError> will contain an error message on failure.
477 If you are running Perl 5.005 or better the object, C<$z>, returned from
478 IO::Compress::Deflate can be used exactly like an L<IO::File|IO::File> filehandle.
479 This means that all normal output file operations can be carried out
481 For example, to write to a compressed file/buffer you can use either of
484 $z->print("hello world\n");
485 print $z "hello world\n";
487 The mandatory parameter C<$output> is used to control the destination
488 of the compressed data. This parameter can take one of these forms.
494 If the C<$output> parameter is a simple scalar, it is assumed to be a
495 filename. This file will be opened for writing and the compressed data
496 will be written to it.
500 If the C<$output> parameter is a filehandle, the compressed data will be
502 The string '-' can be used as an alias for standard output.
504 =item A scalar reference
506 If C<$output> is a scalar reference, the compressed data will be stored
511 If the C<$output> parameter is any other type, C<IO::Compress::Deflate>::new will
514 =head2 Constructor Options
516 C<OPTS> is any combination of the following options:
520 =item C<< AutoClose => 0|1 >>
522 This option is only valid when the C<$output> parameter is a filehandle. If
523 specified, and the value is true, it will result in the C<$output> being
524 closed once either the C<close> method is called or the C<IO::Compress::Deflate>
527 This parameter defaults to 0.
529 =item C<< Append => 0|1 >>
531 Opens C<$output> in append mode.
533 The behaviour of this option is dependent on the type of C<$output>.
539 If C<$output> is a buffer and C<Append> is enabled, all compressed data
540 will be append to the end of C<$output>. Otherwise C<$output> will be
541 cleared before any data is written to it.
545 If C<$output> is a filename and C<Append> is enabled, the file will be
546 opened in append mode. Otherwise the contents of the file, if any, will be
547 truncated before any compressed data is written to it.
551 If C<$output> is a filehandle, the file pointer will be positioned to the
552 end of the file via a call to C<seek> before any compressed data is written
553 to it. Otherwise the file pointer will not be moved.
557 This parameter defaults to 0.
559 =item C<< Merge => 0|1 >>
561 This option is used to compress input data and append it to an existing
562 compressed data stream in C<$output>. The end result is a single compressed
563 data stream stored in C<$output>.
565 It is a fatal error to attempt to use this option when C<$output> is not an
566 RFC 1950 data stream.
568 There are a number of other limitations with the C<Merge> option:
574 This module needs to have been built with zlib 1.2.1 or better to work. A
575 fatal error will be thrown if C<Merge> is used with an older version of
580 If C<$output> is a file or a filehandle, it must be seekable.
584 This parameter defaults to 0.
588 Defines the compression level used by zlib. The value should either be
589 a number between 0 and 9 (0 means no compression and 9 is maximum
590 compression), or one of the symbolic constants defined below.
595 Z_DEFAULT_COMPRESSION
597 The default is Z_DEFAULT_COMPRESSION.
599 Note, these constants are not imported by C<IO::Compress::Deflate> by default.
601 use IO::Compress::Deflate qw(:strategy);
602 use IO::Compress::Deflate qw(:constants);
603 use IO::Compress::Deflate qw(:all);
607 Defines the strategy used to tune the compression. Use one of the symbolic
608 constants defined below.
616 The default is Z_DEFAULT_STRATEGY.
618 =item C<< Strict => 0|1 >>
620 This is a placeholder option.
637 Compresses and outputs the contents of the C<$data> parameter. This
638 has the same behaviour as the C<print> built-in.
640 Returns true if successful.
646 $z->printf($format, $data)
647 printf $z $format, $data
649 Compresses and outputs the contents of the C<$data> parameter.
651 Returns true if successful.
658 $z->syswrite $data, $length
659 $z->syswrite $data, $length, $offset
661 Compresses and outputs the contents of the C<$data> parameter.
663 Returns the number of uncompressed bytes written, or C<undef> if
671 $z->write $data, $length
672 $z->write $data, $length, $offset
674 Compresses and outputs the contents of the C<$data> parameter.
676 Returns the number of uncompressed bytes written, or C<undef> if
684 $z->flush($flush_type);
686 Flushes any pending compressed data to the output file/buffer.
688 This method takes an optional parameter, C<$flush_type>, that controls
689 how the flushing will be carried out. By default the C<$flush_type>
690 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
691 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
692 strongly recommended that you only set the C<flush_type> parameter if
693 you fully understand the implications of what it does - overuse of C<flush>
694 can seriously degrade the level of compression achieved. See the C<zlib>
695 documentation for details.
697 Returns true on success.
706 Returns the uncompressed file offset.
715 Returns true if the C<close> method has been called.
719 $z->seek($position, $whence);
720 seek($z, $position, $whence);
722 Provides a sub-set of the C<seek> functionality, with the restriction
723 that it is only legal to seek forward in the output file/buffer.
724 It is a fatal error to attempt to seek backward.
726 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
728 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
729 SEEK_CUR or SEEK_END.
731 Returns 1 on success, 0 on failure.
740 This is a noop provided for completeness.
746 Returns true if the object currently refers to a opened file/buffer.
750 my $prev = $z->autoflush()
751 my $prev = $z->autoflush(EXPR)
753 If the C<$z> object is associated with a file or a filehandle, this method
754 returns the current autoflush setting for the underlying filehandle. If
755 C<EXPR> is present, and is non-zero, it will enable flushing after every
756 write/print operation.
758 If C<$z> is associated with a buffer, this method has no effect and always
761 B<Note> that the special variable C<$|> B<cannot> be used to set or
762 retrieve the autoflush setting.
764 =head2 input_line_number
766 $z->input_line_number()
767 $z->input_line_number(EXPR)
769 This method always returns C<undef> when compressing.
776 If the C<$z> object is associated with a file or a filehandle, C<fileno>
777 will return the underlying file descriptor. Once the C<close> method is
778 called C<fileno> will return C<undef>.
780 If the C<$z> object is associated with a buffer, this method will return
788 Flushes any pending compressed data and then closes the output file/buffer.
790 For most versions of Perl this method will be automatically invoked if
791 the IO::Compress::Deflate object is destroyed (either explicitly or by the
792 variable with the reference to the object going out of scope). The
793 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
794 these cases, the C<close> method will be called automatically, but
795 not until global destruction of all live objects when the program is
798 Therefore, if you want your scripts to be able to run on all versions
799 of Perl, you should call C<close> explicitly and not rely on automatic
802 Returns true on success, otherwise 0.
804 If the C<AutoClose> option has been enabled when the IO::Compress::Deflate
805 object was created, and the object is associated with a file, the
806 underlying file will also be closed.
808 =head2 newStream([OPTS])
812 $z->newStream( [OPTS] )
814 Closes the current compressed data stream and starts a new one.
816 OPTS consists of any of the the options that are available when creating
819 See the L</"Constructor Options"> section for more details.
831 A number of symbolic constants are required by some methods in
832 C<IO::Compress::Deflate>. None are imported by default.
838 Imports C<deflate>, C<$DeflateError> and all symbolic
839 constants that can be used by C<IO::Compress::Deflate>. Same as doing this
841 use IO::Compress::Deflate qw(deflate $DeflateError :constants) ;
845 Import all symbolic constants. Same as doing this
847 use IO::Compress::Deflate qw(:flush :level :strategy) ;
851 These symbolic constants are used by the C<flush> method.
862 These symbolic constants are used by the C<Level> option in the constructor.
867 Z_DEFAULT_COMPRESSION
871 These symbolic constants are used by the C<Strategy> option in the constructor.
886 =head2 Apache::GZip Revisited
888 See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited">
890 =head2 Working with Net::FTP
892 See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP">
896 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::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
898 L<IO::Compress::FAQ|IO::Compress::FAQ>
900 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
901 L<Archive::Tar|Archive::Tar>,
904 For RFC 1950, 1951 and 1952 see
905 F<http://www.faqs.org/rfcs/rfc1950.html>,
906 F<http://www.faqs.org/rfcs/rfc1951.html> and
907 F<http://www.faqs.org/rfcs/rfc1952.html>
909 The I<zlib> compression library was written by Jean-loup Gailly
910 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
912 The primary site for the I<zlib> compression library is
913 F<http://www.zlib.org>.
915 The primary site for gzip is F<http://www.gzip.org>.
919 This module was written by Paul Marquess, F<pmqs@cpan.org>.
921 =head1 MODIFICATION HISTORY
923 See the Changes file.
925 =head1 COPYRIGHT AND LICENSE
927 Copyright (c) 2005-2012 Paul Marquess. All rights reserved.
929 This program is free software; you can redistribute it and/or
930 modify it under the same terms as Perl itself.