1 package IO::Compress::RawDeflate ;
10 use IO::Compress::Base 2.023 ;
11 use IO::Compress::Base::Common 2.023 qw(:Status createSelfTiedObject);
12 use IO::Compress::Adapter::Deflate 2.023 ;
17 our ($VERSION, @ISA, @EXPORT_OK, %DEFLATE_CONSTANTS, %EXPORT_TAGS, $RawDeflateError);
20 $RawDeflateError = '';
22 @ISA = qw(Exporter IO::Compress::Base);
23 @EXPORT_OK = qw( $RawDeflateError rawdeflate ) ;
25 %EXPORT_TAGS = ( flush => [qw{
51 foreach (keys %EXPORT_TAGS )
53 push @{$EXPORT_TAGS{constants}},
57 $EXPORT_TAGS{all} = $EXPORT_TAGS{constants} ;
61 %DEFLATE_CONSTANTS = %EXPORT_TAGS;
63 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
65 Exporter::export_ok_tags('all');
73 my $obj = createSelfTiedObject($class, \$RawDeflateError);
75 return $obj->_create(undef, @_);
80 my $obj = createSelfTiedObject(undef, \$RawDeflateError);
81 return $obj->_def(@_);
97 my ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject(
99 $got->value('Adler32'),
100 $got->value('Level'),
101 $got->value('Strategy')
104 return $self->saveErrorString(undef, $errstr, $errno)
138 return $self->getZlibParams();
145 use IO::Compress::Base::Common 2.023 qw(:Parse);
146 use Compress::Raw::Zlib 2.023 qw(Z_DEFLATED Z_DEFAULT_COMPRESSION Z_DEFAULT_STRATEGY);
152 #'Method' => [0, 1, Parse_unsigned, Z_DEFLATED],
153 'Level' => [0, 1, Parse_signed, Z_DEFAULT_COMPRESSION],
154 'Strategy' => [0, 1, Parse_signed, Z_DEFAULT_STRATEGY],
156 'CRC32' => [0, 1, Parse_boolean, 0],
157 'ADLER32' => [0, 1, Parse_boolean, 0],
158 'Merge' => [1, 1, Parse_boolean, 0],
166 return ('IO::Uncompress::RawInflate',
167 \$IO::Uncompress::RawInflate::RawInflateError);
178 use IO::Seekable qw(SEEK_SET);
183 my $outValue = shift ;
184 my $outType = shift ;
186 my ($invClass, $error_ref) = $self->getInverseClass();
187 eval "require $invClass"
190 my $inf = $invClass->new( $outValue,
195 or return $self->saveErrorString(undef, "Cannot create InflateScan object: $$error_ref" ) ;
199 or return $self->saveErrorString(undef, "Error Scanning: $$error_ref", $inf->errorNo) ;
200 $inf->zap($end_offset)
201 or return $self->saveErrorString(undef, "Error Zapping: $$error_ref", $inf->errorNo) ;
203 my $def = *$self->{Compress} = $inf->createDeflate();
205 *$self->{Header} = *$inf->{Info}{Header};
206 *$self->{UnCompSize} = *$inf->{UnCompSize}->clone();
207 *$self->{CompSize} = *$inf->{CompSize}->clone();
209 #*$self->{CompSize} = new U64(0, *$self->{UnCompSize_32bit});
212 if ( $outType eq 'buffer')
213 { substr( ${ *$self->{Buffer} }, $end_offset) = '' }
214 elsif ($outType eq 'handle' || $outType eq 'filename') {
215 *$self->{FH} = *$inf->{FH} ;
217 *$self->{FH}->flush() ;
218 *$self->{Handle} = 1 if $outType eq 'handle';
220 #seek(*$self->{FH}, $end_offset, SEEK_SET)
221 *$self->{FH}->seek($end_offset, SEEK_SET)
222 or return $self->saveErrorString(undef, $!, $!) ;
228 #### zlib specific methods
235 my $strategy = shift ;
237 my $status = *$self->{Compress}->deflateParams(Level => $level, Strategy => $strategy) ;
238 return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo})
239 if $status == STATUS_ERROR;
253 IO::Compress::RawDeflate - Write RFC 1951 files/buffers
259 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
261 my $status = rawdeflate $input => $output [,OPTS]
262 or die "rawdeflate failed: $RawDeflateError\n";
264 my $z = new IO::Compress::RawDeflate $output [,OPTS]
265 or die "rawdeflate failed: $RawDeflateError\n";
268 $z->printf($format, $string);
270 $z->syswrite($string [, $length, $offset]);
274 $z->seek($position, $whence);
279 $z->input_line_number();
280 $z->newStream( [OPTS] );
291 printf $z $format, $string;
294 seek $z, $position, $whence
302 This module provides a Perl interface that allows writing compressed
303 data to files or buffer as defined in RFC 1951.
305 Note that RFC 1951 data is not a good choice of compression format
306 to use in isolation, especially if you want to auto-detect it.
308 For reading RFC 1951 files/buffers, see the companion module
309 L<IO::Uncompress::RawInflate|IO::Uncompress::RawInflate>.
311 =head1 Functional Interface
313 A top-level function, C<rawdeflate>, is provided to carry out
314 "one-shot" compression between buffers and/or files. For finer
315 control over the compression process, see the L</"OO Interface">
318 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
320 rawdeflate $input => $output [,OPTS]
321 or die "rawdeflate failed: $RawDeflateError\n";
323 The functional interface needs Perl5.005 or better.
325 =head2 rawdeflate $input => $output [, OPTS]
327 C<rawdeflate> expects at least two parameters, C<$input> and C<$output>.
329 =head3 The C<$input> parameter
331 The parameter, C<$input>, is used to define the source of
332 the uncompressed data.
334 It can take one of the following forms:
340 If the C<$input> parameter is a simple scalar, it is assumed to be a
341 filename. This file will be opened for reading and the input data
342 will be read from it.
346 If the C<$input> parameter is a filehandle, the input data will be
348 The string '-' can be used as an alias for standard input.
350 =item A scalar reference
352 If C<$input> is a scalar reference, the input data will be read
355 =item An array reference
357 If C<$input> is an array reference, each element in the array must be a
360 The input data will be read from each file in turn.
362 The complete array will be walked to ensure that it only
363 contains valid filenames before any data is compressed.
365 =item An Input FileGlob string
367 If C<$input> is a string that is delimited by the characters "<" and ">"
368 C<rawdeflate> will assume that it is an I<input fileglob string>. The
369 input is the list of files that match the fileglob.
371 If the fileglob does not match any files ...
373 See L<File::GlobMapper|File::GlobMapper> for more details.
377 If the C<$input> parameter is any other type, C<undef> will be returned.
379 =head3 The C<$output> parameter
381 The parameter C<$output> is used to control the destination of the
382 compressed data. This parameter can take one of these forms.
388 If the C<$output> parameter is a simple scalar, it is assumed to be a
389 filename. This file will be opened for writing and the compressed
390 data will be written to it.
394 If the C<$output> parameter is a filehandle, the compressed data
395 will be written to it.
396 The string '-' can be used as an alias for standard output.
398 =item A scalar reference
400 If C<$output> is a scalar reference, the compressed data will be
401 stored in C<$$output>.
403 =item An Array Reference
405 If C<$output> is an array reference, the compressed data will be
406 pushed onto the array.
408 =item An Output FileGlob
410 If C<$output> is a string that is delimited by the characters "<" and ">"
411 C<rawdeflate> will assume that it is an I<output fileglob string>. The
412 output is the list of files that match the fileglob.
414 When C<$output> is an fileglob string, C<$input> must also be a fileglob
415 string. Anything else is an error.
419 If the C<$output> parameter is any other type, C<undef> will be returned.
423 When C<$input> maps to multiple files/buffers and C<$output> is a single
424 file/buffer the input files/buffers will be stored
425 in C<$output> as a concatenated series of compressed data streams.
427 =head2 Optional Parameters
429 Unless specified below, the optional parameters for C<rawdeflate>,
430 C<OPTS>, are the same as those used with the OO interface defined in the
431 L</"Constructor Options"> section below.
435 =item C<< AutoClose => 0|1 >>
437 This option applies to any input or output data streams to
438 C<rawdeflate> that are filehandles.
440 If C<AutoClose> is specified, and the value is true, it will result in all
441 input and/or output filehandles being closed once C<rawdeflate> has
444 This parameter defaults to 0.
446 =item C<< BinModeIn => 0|1 >>
448 When reading from a file or filehandle, set C<binmode> before reading.
452 =item C<< Append => 0|1 >>
454 The behaviour of this option is dependent on the type of output data
461 If C<Append> is enabled, all compressed data will be append to the end of
462 the output buffer. Otherwise the output buffer will be cleared before any
463 compressed data is written to it.
467 If C<Append> is enabled, the file will be opened in append mode. Otherwise
468 the contents of the file, if any, will be truncated before any compressed
469 data is written to it.
473 If C<Append> is enabled, the filehandle will be positioned to the end of
474 the file via a call to C<seek> before any compressed data is
475 written to it. Otherwise the file pointer will not be moved.
479 When C<Append> is specified, and set to true, it will I<append> all compressed
480 data to the output data stream.
482 So when the output is a filehandle it will carry out a seek to the eof
483 before writing any compressed data. If the output is a filename, it will be opened for
484 appending. If the output is a buffer, all compressed data will be appened to
487 Conversely when C<Append> is not specified, or it is present and is set to
488 false, it will operate as follows.
490 When the output is a filename, it will truncate the contents of the file
491 before writing any compressed data. If the output is a filehandle
492 its position will not be changed. If the output is a buffer, it will be
493 wiped before any compressed data is output.
501 To read the contents of the file C<file1.txt> and write the compressed
502 data to the file C<file1.txt.1951>.
506 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
508 my $input = "file1.txt";
509 rawdeflate $input => "$input.1951"
510 or die "rawdeflate failed: $RawDeflateError\n";
512 To read from an existing Perl filehandle, C<$input>, and write the
513 compressed data to a buffer, C<$buffer>.
517 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
520 my $input = new IO::File "<file1.txt"
521 or die "Cannot open 'file1.txt': $!\n" ;
523 rawdeflate $input => \$buffer
524 or die "rawdeflate failed: $RawDeflateError\n";
526 To compress all files in the directory "/my/home" that match "*.txt"
527 and store the compressed data in the same directory
531 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
533 rawdeflate '</my/home/*.txt>' => '<*.1951>'
534 or die "rawdeflate failed: $RawDeflateError\n";
536 and if you want to compress each file one at a time, this will do the trick
540 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
542 for my $input ( glob "/my/home/*.txt" )
544 my $output = "$input.1951" ;
545 rawdeflate $input => $output
546 or die "Error compressing '$input': $RawDeflateError\n";
553 The format of the constructor for C<IO::Compress::RawDeflate> is shown below
555 my $z = new IO::Compress::RawDeflate $output [,OPTS]
556 or die "IO::Compress::RawDeflate failed: $RawDeflateError\n";
558 It returns an C<IO::Compress::RawDeflate> object on success and undef on failure.
559 The variable C<$RawDeflateError> will contain an error message on failure.
561 If you are running Perl 5.005 or better the object, C<$z>, returned from
562 IO::Compress::RawDeflate can be used exactly like an L<IO::File|IO::File> filehandle.
563 This means that all normal output file operations can be carried out
565 For example, to write to a compressed file/buffer you can use either of
568 $z->print("hello world\n");
569 print $z "hello world\n";
571 The mandatory parameter C<$output> is used to control the destination
572 of the compressed data. This parameter can take one of these forms.
578 If the C<$output> parameter is a simple scalar, it is assumed to be a
579 filename. This file will be opened for writing and the compressed data
580 will be written to it.
584 If the C<$output> parameter is a filehandle, the compressed data will be
586 The string '-' can be used as an alias for standard output.
588 =item A scalar reference
590 If C<$output> is a scalar reference, the compressed data will be stored
595 If the C<$output> parameter is any other type, C<IO::Compress::RawDeflate>::new will
598 =head2 Constructor Options
600 C<OPTS> is any combination of the following options:
604 =item C<< AutoClose => 0|1 >>
606 This option is only valid when the C<$output> parameter is a filehandle. If
607 specified, and the value is true, it will result in the C<$output> being
608 closed once either the C<close> method is called or the C<IO::Compress::RawDeflate>
611 This parameter defaults to 0.
613 =item C<< Append => 0|1 >>
615 Opens C<$output> in append mode.
617 The behaviour of this option is dependent on the type of C<$output>.
623 If C<$output> is a buffer and C<Append> is enabled, all compressed data
624 will be append to the end of C<$output>. Otherwise C<$output> will be
625 cleared before any data is written to it.
629 If C<$output> is a filename and C<Append> is enabled, the file will be
630 opened in append mode. Otherwise the contents of the file, if any, will be
631 truncated before any compressed data is written to it.
635 If C<$output> is a filehandle, the file pointer will be positioned to the
636 end of the file via a call to C<seek> before any compressed data is written
637 to it. Otherwise the file pointer will not be moved.
641 This parameter defaults to 0.
643 =item C<< Merge => 0|1 >>
645 This option is used to compress input data and append it to an existing
646 compressed data stream in C<$output>. The end result is a single compressed
647 data stream stored in C<$output>.
649 It is a fatal error to attempt to use this option when C<$output> is not an
650 RFC 1951 data stream.
652 There are a number of other limitations with the C<Merge> option:
658 This module needs to have been built with zlib 1.2.1 or better to work. A
659 fatal error will be thrown if C<Merge> is used with an older version of
664 If C<$output> is a file or a filehandle, it must be seekable.
668 This parameter defaults to 0.
672 Defines the compression level used by zlib. The value should either be
673 a number between 0 and 9 (0 means no compression and 9 is maximum
674 compression), or one of the symbolic constants defined below.
679 Z_DEFAULT_COMPRESSION
681 The default is Z_DEFAULT_COMPRESSION.
683 Note, these constants are not imported by C<IO::Compress::RawDeflate> by default.
685 use IO::Compress::RawDeflate qw(:strategy);
686 use IO::Compress::RawDeflate qw(:constants);
687 use IO::Compress::RawDeflate qw(:all);
691 Defines the strategy used to tune the compression. Use one of the symbolic
692 constants defined below.
700 The default is Z_DEFAULT_STRATEGY.
702 =item C<< Strict => 0|1 >>
704 This is a placeholder option.
721 Compresses and outputs the contents of the C<$data> parameter. This
722 has the same behaviour as the C<print> built-in.
724 Returns true if successful.
730 $z->printf($format, $data)
731 printf $z $format, $data
733 Compresses and outputs the contents of the C<$data> parameter.
735 Returns true if successful.
742 $z->syswrite $data, $length
743 $z->syswrite $data, $length, $offset
745 Compresses and outputs the contents of the C<$data> parameter.
747 Returns the number of uncompressed bytes written, or C<undef> if
755 $z->write $data, $length
756 $z->write $data, $length, $offset
758 Compresses and outputs the contents of the C<$data> parameter.
760 Returns the number of uncompressed bytes written, or C<undef> if
768 $z->flush($flush_type);
770 Flushes any pending compressed data to the output file/buffer.
772 This method takes an optional parameter, C<$flush_type>, that controls
773 how the flushing will be carried out. By default the C<$flush_type>
774 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
775 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
776 strongly recommended that you only set the C<flush_type> parameter if
777 you fully understand the implications of what it does - overuse of C<flush>
778 can seriously degrade the level of compression achieved. See the C<zlib>
779 documentation for details.
781 Returns true on success.
790 Returns the uncompressed file offset.
799 Returns true if the C<close> method has been called.
803 $z->seek($position, $whence);
804 seek($z, $position, $whence);
806 Provides a sub-set of the C<seek> functionality, with the restriction
807 that it is only legal to seek forward in the output file/buffer.
808 It is a fatal error to attempt to seek backward.
810 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
812 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
813 SEEK_CUR or SEEK_END.
815 Returns 1 on success, 0 on failure.
824 This is a noop provided for completeness.
830 Returns true if the object currently refers to a opened file/buffer.
834 my $prev = $z->autoflush()
835 my $prev = $z->autoflush(EXPR)
837 If the C<$z> object is associated with a file or a filehandle, this method
838 returns the current autoflush setting for the underlying filehandle. If
839 C<EXPR> is present, and is non-zero, it will enable flushing after every
840 write/print operation.
842 If C<$z> is associated with a buffer, this method has no effect and always
845 B<Note> that the special variable C<$|> B<cannot> be used to set or
846 retrieve the autoflush setting.
848 =head2 input_line_number
850 $z->input_line_number()
851 $z->input_line_number(EXPR)
853 This method always returns C<undef> when compressing.
860 If the C<$z> object is associated with a file or a filehandle, C<fileno>
861 will return the underlying file descriptor. Once the C<close> method is
862 called C<fileno> will return C<undef>.
864 If the C<$z> object is is associated with a buffer, this method will return
872 Flushes any pending compressed data and then closes the output file/buffer.
874 For most versions of Perl this method will be automatically invoked if
875 the IO::Compress::RawDeflate object is destroyed (either explicitly or by the
876 variable with the reference to the object going out of scope). The
877 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
878 these cases, the C<close> method will be called automatically, but
879 not until global destruction of all live objects when the program is
882 Therefore, if you want your scripts to be able to run on all versions
883 of Perl, you should call C<close> explicitly and not rely on automatic
886 Returns true on success, otherwise 0.
888 If the C<AutoClose> option has been enabled when the IO::Compress::RawDeflate
889 object was created, and the object is associated with a file, the
890 underlying file will also be closed.
892 =head2 newStream([OPTS])
896 $z->newStream( [OPTS] )
898 Closes the current compressed data stream and starts a new one.
900 OPTS consists of any of the the options that are available when creating
903 See the L</"Constructor Options"> section for more details.
915 A number of symbolic constants are required by some methods in
916 C<IO::Compress::RawDeflate>. None are imported by default.
922 Imports C<rawdeflate>, C<$RawDeflateError> and all symbolic
923 constants that can be used by C<IO::Compress::RawDeflate>. Same as doing this
925 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError :constants) ;
929 Import all symbolic constants. Same as doing this
931 use IO::Compress::RawDeflate qw(:flush :level :strategy) ;
935 These symbolic constants are used by the C<flush> method.
946 These symbolic constants are used by the C<Level> option in the constructor.
951 Z_DEFAULT_COMPRESSION
955 These symbolic constants are used by the C<Strategy> option in the constructor.
970 =head2 Apache::GZip Revisited
972 See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited">
976 =head2 Working with Net::FTP
978 See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP">
982 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::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>
984 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
986 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
987 L<Archive::Tar|Archive::Tar>,
990 For RFC 1950, 1951 and 1952 see
991 F<http://www.faqs.org/rfcs/rfc1950.html>,
992 F<http://www.faqs.org/rfcs/rfc1951.html> and
993 F<http://www.faqs.org/rfcs/rfc1952.html>
995 The I<zlib> compression library was written by Jean-loup Gailly
996 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
998 The primary site for the I<zlib> compression library is
999 F<http://www.zlib.org>.
1001 The primary site for gzip is F<http://www.gzip.org>.
1005 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1007 =head1 MODIFICATION HISTORY
1009 See the Changes file.
1011 =head1 COPYRIGHT AND LICENSE
1013 Copyright (c) 2005-2009 Paul Marquess. All rights reserved.
1015 This program is free software; you can redistribute it and/or
1016 modify it under the same terms as Perl itself.