1 package IO::Compress::RawDeflate ;
10 use IO::Compress::Base 2.006 ;
11 use IO::Compress::Base::Common 2.006 qw(:Status createSelfTiedObject);
12 use IO::Compress::Adapter::Deflate 2.006 ;
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(@_);
98 my ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject(
100 $got->value('Adler32'),
101 $got->value('Level'),
102 $got->value('Strategy')
105 return $self->saveErrorString(undef, $errstr, $errno)
139 return $self->getZlibParams();
146 use IO::Compress::Base::Common 2.006 qw(:Parse);
147 use Compress::Raw::Zlib 2.006 qw(Z_DEFLATED Z_DEFAULT_COMPRESSION Z_DEFAULT_STRATEGY);
153 #'Method' => [0, 1, Parse_unsigned, Z_DEFLATED],
154 'Level' => [0, 1, Parse_signed, Z_DEFAULT_COMPRESSION],
155 'Strategy' => [0, 1, Parse_signed, Z_DEFAULT_STRATEGY],
157 'CRC32' => [0, 1, Parse_boolean, 0],
158 'ADLER32' => [0, 1, Parse_boolean, 0],
159 'Merge' => [1, 1, Parse_boolean, 0],
167 return ('IO::Uncompress::RawInflate',
168 \$IO::Uncompress::RawInflate::RawInflateError);
179 use IO::Seekable qw(SEEK_SET);
184 my $outValue = shift ;
185 my $outType = shift ;
187 my ($invClass, $error_ref) = $self->getInverseClass();
188 eval "require $invClass"
191 my $inf = $invClass->new( $outValue,
196 or return $self->saveErrorString(undef, "Cannot create InflateScan object: $$error_ref" ) ;
200 or return $self->saveErrorString(undef, "Error Scanning: $$error_ref", $inf->errorNo) ;
201 $inf->zap($end_offset)
202 or return $self->saveErrorString(undef, "Error Zapping: $$error_ref", $inf->errorNo) ;
204 my $def = *$self->{Compress} = $inf->createDeflate();
206 *$self->{Header} = *$inf->{Info}{Header};
207 *$self->{UnCompSize} = *$inf->{UnCompSize}->clone();
208 *$self->{CompSize} = *$inf->{CompSize}->clone();
210 #*$self->{CompSize} = new U64(0, *$self->{UnCompSize_32bit});
213 if ( $outType eq 'buffer')
214 { substr( ${ *$self->{Buffer} }, $end_offset) = '' }
215 elsif ($outType eq 'handle' || $outType eq 'filename') {
216 *$self->{FH} = *$inf->{FH} ;
218 *$self->{FH}->flush() ;
219 *$self->{Handle} = 1 if $outType eq 'handle';
221 #seek(*$self->{FH}, $end_offset, SEEK_SET)
222 *$self->{FH}->seek($end_offset, SEEK_SET)
223 or return $self->saveErrorString(undef, $!, $!) ;
229 #### zlib specific methods
236 my $strategy = shift ;
238 my $status = *$self->{Compress}->deflateParams(Level => $level, Strategy => $strategy) ;
239 return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo})
240 if $status == STATUS_ERROR;
256 IO::Compress::RawDeflate - Write RFC 1951 files/buffers
262 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
265 my $status = rawdeflate $input => $output [,OPTS]
266 or die "rawdeflate failed: $RawDeflateError\n";
268 my $z = new IO::Compress::RawDeflate $output [,OPTS]
269 or die "rawdeflate failed: $RawDeflateError\n";
272 $z->printf($format, $string);
274 $z->syswrite($string [, $length, $offset]);
278 $z->seek($position, $whence);
283 $z->input_line_number();
284 $z->newStream( [OPTS] );
295 printf $z $format, $string;
298 seek $z, $position, $whence
307 This module provides a Perl interface that allows writing compressed
308 data to files or buffer as defined in RFC 1951.
316 Note that RFC 1951 data is not a good choice of compression format
317 to use in isolation, especially if you want to auto-detect it.
323 For reading RFC 1951 files/buffers, see the companion module
324 L<IO::Uncompress::RawInflate|IO::Uncompress::RawInflate>.
327 =head1 Functional Interface
329 A top-level function, C<rawdeflate>, is provided to carry out
330 "one-shot" compression between buffers and/or files. For finer
331 control over the compression process, see the L</"OO Interface">
334 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
336 rawdeflate $input => $output [,OPTS]
337 or die "rawdeflate failed: $RawDeflateError\n";
341 The functional interface needs Perl5.005 or better.
344 =head2 rawdeflate $input => $output [, OPTS]
347 C<rawdeflate> expects at least two parameters, C<$input> and C<$output>.
349 =head3 The C<$input> parameter
351 The parameter, C<$input>, is used to define the source of
352 the uncompressed data.
354 It can take one of the following forms:
360 If the C<$input> parameter is a simple scalar, it is assumed to be a
361 filename. This file will be opened for reading and the input data
362 will be read from it.
366 If the C<$input> parameter is a filehandle, the input data will be
368 The string '-' can be used as an alias for standard input.
370 =item A scalar reference
372 If C<$input> is a scalar reference, the input data will be read
375 =item An array reference
377 If C<$input> is an array reference, each element in the array must be a
380 The input data will be read from each file in turn.
382 The complete array will be walked to ensure that it only
383 contains valid filenames before any data is compressed.
387 =item An Input FileGlob string
389 If C<$input> is a string that is delimited by the characters "<" and ">"
390 C<rawdeflate> will assume that it is an I<input fileglob string>. The
391 input is the list of files that match the fileglob.
393 If the fileglob does not match any files ...
395 See L<File::GlobMapper|File::GlobMapper> for more details.
400 If the C<$input> parameter is any other type, C<undef> will be returned.
404 =head3 The C<$output> parameter
406 The parameter C<$output> is used to control the destination of the
407 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
415 data will be written to it.
419 If the C<$output> parameter is a filehandle, the compressed data
420 will be written to it.
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
427 stored in C<$$output>.
431 =item An Array Reference
433 If C<$output> is an array reference, the compressed data will be
434 pushed onto the array.
436 =item An Output FileGlob
438 If C<$output> is a string that is delimited by the characters "<" and ">"
439 C<rawdeflate> will assume that it is an I<output fileglob string>. The
440 output is the list of files that match the fileglob.
442 When C<$output> is an fileglob string, C<$input> must also be a fileglob
443 string. Anything else is an error.
447 If the C<$output> parameter is any other type, C<undef> will be returned.
455 When C<$input> maps to multiple files/buffers and C<$output> is a single
456 file/buffer the input files/buffers will be stored
457 in C<$output> as a concatenated series of compressed data streams.
464 =head2 Optional Parameters
466 Unless specified below, the optional parameters for C<rawdeflate>,
467 C<OPTS>, are the same as those used with the OO interface defined in the
468 L</"Constructor Options"> section below.
472 =item C<< AutoClose => 0|1 >>
474 This option applies to any input or output data streams to
475 C<rawdeflate> that are filehandles.
477 If C<AutoClose> is specified, and the value is true, it will result in all
478 input and/or output filehandles being closed once C<rawdeflate> has
481 This parameter defaults to 0.
484 =item C<< BinModeIn => 0|1 >>
486 When reading from a file or filehandle, set C<binmode> before reading.
494 =item C<< Append => 0|1 >>
506 To read the contents of the file C<file1.txt> and write the compressed
507 data to the file C<file1.txt.1951>.
511 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
513 my $input = "file1.txt";
514 rawdeflate $input => "$input.1951"
515 or die "rawdeflate failed: $RawDeflateError\n";
518 To read from an existing Perl filehandle, C<$input>, and write the
519 compressed data to a buffer, C<$buffer>.
523 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
526 my $input = new IO::File "<file1.txt"
527 or die "Cannot open 'file1.txt': $!\n" ;
529 rawdeflate $input => \$buffer
530 or die "rawdeflate failed: $RawDeflateError\n";
532 To compress all files in the directory "/my/home" that match "*.txt"
533 and store the compressed data in the same directory
537 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
539 rawdeflate '</my/home/*.txt>' => '<*.1951>'
540 or die "rawdeflate failed: $RawDeflateError\n";
542 and if you want to compress each file one at a time, this will do the trick
546 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
548 for my $input ( glob "/my/home/*.txt" )
550 my $output = "$input.1951" ;
551 rawdeflate $input => $output
552 or die "Error compressing '$input': $RawDeflateError\n";
560 The format of the constructor for C<IO::Compress::RawDeflate> is shown below
562 my $z = new IO::Compress::RawDeflate $output [,OPTS]
563 or die "IO::Compress::RawDeflate failed: $RawDeflateError\n";
565 It returns an C<IO::Compress::RawDeflate> object on success and undef on failure.
566 The variable C<$RawDeflateError> will contain an error message on failure.
568 If you are running Perl 5.005 or better the object, C<$z>, returned from
569 IO::Compress::RawDeflate can be used exactly like an L<IO::File|IO::File> filehandle.
570 This means that all normal output file operations can be carried out
572 For example, to write to a compressed file/buffer you can use either of
575 $z->print("hello world\n");
576 print $z "hello world\n";
578 The mandatory parameter C<$output> is used to control the destination
579 of the compressed data. This parameter can take one of these forms.
585 If the C<$output> parameter is a simple scalar, it is assumed to be a
586 filename. This file will be opened for writing and the compressed data
587 will be written to it.
591 If the C<$output> parameter is a filehandle, the compressed data will be
593 The string '-' can be used as an alias for standard output.
596 =item A scalar reference
598 If C<$output> is a scalar reference, the compressed data will be stored
603 If the C<$output> parameter is any other type, C<IO::Compress::RawDeflate>::new will
606 =head2 Constructor Options
608 C<OPTS> is any combination of the following options:
612 =item C<< AutoClose => 0|1 >>
614 This option is only valid when the C<$output> parameter is a filehandle. If
615 specified, and the value is true, it will result in the C<$output> being
616 closed once either the C<close> method is called or the C<IO::Compress::RawDeflate>
619 This parameter defaults to 0.
621 =item C<< Append => 0|1 >>
623 Opens C<$output> in append mode.
625 The behaviour of this option is dependent on the type of C<$output>.
631 If C<$output> is a buffer and C<Append> is enabled, all compressed data
632 will be append to the end if C<$output>. Otherwise C<$output> will be
633 cleared before any data is written to it.
637 If C<$output> is a filename and C<Append> is enabled, the file will be
638 opened in append mode. Otherwise the contents of the file, if any, will be
639 truncated before any compressed data is written to it.
643 If C<$output> is a filehandle, the file pointer will be positioned to the
644 end of the file via a call to C<seek> before any compressed data is written
645 to it. Otherwise the file pointer will not be moved.
649 This parameter defaults to 0.
655 =item C<< Merge => 0|1 >>
657 This option is used to compress input data and append it to an existing
658 compressed data stream in C<$output>. The end result is a single compressed
659 data stream stored in C<$output>.
663 It is a fatal error to attempt to use this option when C<$output> is not an
664 RFC 1951 data stream.
668 There are a number of other limitations with the C<Merge> option:
674 This module needs to have been built with zlib 1.2.1 or better to work. A
675 fatal error will be thrown if C<Merge> is used with an older version of
680 If C<$output> is a file or a filehandle, it must be seekable.
685 This parameter defaults to 0.
691 Defines the compression level used by zlib. The value should either be
692 a number between 0 and 9 (0 means no compression and 9 is maximum
693 compression), or one of the symbolic constants defined below.
698 Z_DEFAULT_COMPRESSION
700 The default is Z_DEFAULT_COMPRESSION.
702 Note, these constants are not imported by C<IO::Compress::RawDeflate> by default.
704 use IO::Compress::RawDeflate qw(:strategy);
705 use IO::Compress::RawDeflate qw(:constants);
706 use IO::Compress::RawDeflate qw(:all);
710 Defines the strategy used to tune the compression. Use one of the symbolic
711 constants defined below.
719 The default is Z_DEFAULT_STRATEGY.
726 =item C<< Strict => 0|1 >>
730 This is a placeholder option.
749 Compresses and outputs the contents of the C<$data> parameter. This
750 has the same behaviour as the C<print> built-in.
752 Returns true if successful.
758 $z->printf($format, $data)
759 printf $z $format, $data
761 Compresses and outputs the contents of the C<$data> parameter.
763 Returns true if successful.
770 $z->syswrite $data, $length
771 $z->syswrite $data, $length, $offset
773 Compresses and outputs the contents of the C<$data> parameter.
775 Returns the number of uncompressed bytes written, or C<undef> if
783 $z->write $data, $length
784 $z->write $data, $length, $offset
786 Compresses and outputs the contents of the C<$data> parameter.
788 Returns the number of uncompressed bytes written, or C<undef> if
797 $z->flush($flush_type);
800 Flushes any pending compressed data to the output file/buffer.
803 This method takes an optional parameter, C<$flush_type>, that controls
804 how the flushing will be carried out. By default the C<$flush_type>
805 used is C<Z_FINISH>. Other valid values for C<$flush_type> are
806 C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
807 strongly recommended that you only set the C<flush_type> parameter if
808 you fully understand the implications of what it does - overuse of C<flush>
809 can seriously degrade the level of compression achieved. See the C<zlib>
810 documentation for details.
813 Returns true on success.
823 Returns the uncompressed file offset.
834 Returns true if the C<close> method has been called.
840 $z->seek($position, $whence);
841 seek($z, $position, $whence);
846 Provides a sub-set of the C<seek> functionality, with the restriction
847 that it is only legal to seek forward in the output file/buffer.
848 It is a fatal error to attempt to seek backward.
850 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
854 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
855 SEEK_CUR or SEEK_END.
857 Returns 1 on success, 0 on failure.
866 This is a noop provided for completeness.
872 Returns true if the object currently refers to a opened file/buffer.
876 my $prev = $z->autoflush()
877 my $prev = $z->autoflush(EXPR)
879 If the C<$z> object is associated with a file or a filehandle, this method
880 returns the current autoflush setting for the underlying filehandle. If
881 C<EXPR> is present, and is non-zero, it will enable flushing after every
882 write/print operation.
884 If C<$z> is associated with a buffer, this method has no effect and always
887 B<Note> that the special variable C<$|> B<cannot> be used to set or
888 retrieve the autoflush setting.
890 =head2 input_line_number
892 $z->input_line_number()
893 $z->input_line_number(EXPR)
896 This method always returns C<undef> when compressing.
905 If the C<$z> object is associated with a file or a filehandle, this method
906 will return the underlying file descriptor.
908 If the C<$z> object is is associated with a buffer, this method will
918 Flushes any pending compressed data and then closes the output file/buffer.
922 For most versions of Perl this method will be automatically invoked if
923 the IO::Compress::RawDeflate object is destroyed (either explicitly or by the
924 variable with the reference to the object going out of scope). The
925 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
926 these cases, the C<close> method will be called automatically, but
927 not until global destruction of all live objects when the program is
930 Therefore, if you want your scripts to be able to run on all versions
931 of Perl, you should call C<close> explicitly and not rely on automatic
934 Returns true on success, otherwise 0.
936 If the C<AutoClose> option has been enabled when the IO::Compress::RawDeflate
937 object was created, and the object is associated with a file, the
938 underlying file will also be closed.
943 =head2 newStream([OPTS])
947 $z->newStream( [OPTS] )
949 Closes the current compressed data stream and starts a new one.
951 OPTS consists of any of the the options that are available when creating
954 See the L</"Constructor Options"> section for more details.
969 A number of symbolic constants are required by some methods in
970 C<IO::Compress::RawDeflate>. None are imported by default.
979 Imports C<rawdeflate>, C<$RawDeflateError> and all symbolic
980 constants that can be used by C<IO::Compress::RawDeflate>. Same as doing this
982 use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError :constants) ;
986 Import all symbolic constants. Same as doing this
989 use IO::Compress::RawDeflate qw(:flush :level :strategy) ;
994 These symbolic constants are used by the C<flush> method.
1005 These symbolic constants are used by the C<Level> option in the constructor.
1010 Z_DEFAULT_COMPRESSION
1015 These symbolic constants are used by the C<Strategy> option in the constructor.
1046 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::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1048 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1050 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1051 L<Archive::Tar|Archive::Tar>,
1052 L<IO::Zlib|IO::Zlib>
1055 For RFC 1950, 1951 and 1952 see
1056 F<http://www.faqs.org/rfcs/rfc1950.html>,
1057 F<http://www.faqs.org/rfcs/rfc1951.html> and
1058 F<http://www.faqs.org/rfcs/rfc1952.html>
1060 The I<zlib> compression library was written by Jean-loup Gailly
1061 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1063 The primary site for the I<zlib> compression library is
1064 F<http://www.zlib.org>.
1066 The primary site for gzip is F<http://www.gzip.org>.
1073 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1077 =head1 MODIFICATION HISTORY
1079 See the Changes file.
1081 =head1 COPYRIGHT AND LICENSE
1083 Copyright (c) 2005-2007 Paul Marquess. All rights reserved.
1085 This program is free software; you can redistribute it and/or
1086 modify it under the same terms as Perl itself.