IO::Compress::Deflate - Write RFC 1950 files/buffers
use IO::Compress::Deflate qw(deflate $DeflateError) ;
my $status = deflate $input => $output [,OPTS]
or die "deflate failed: $DeflateError\n";
my $z = IO::Compress::Deflate->new( $output [,OPTS] )
or die "deflate failed: $DeflateError\n";
$z->print($string);
$z->printf($format, $string);
$z->write($string);
$z->syswrite($string [, $length, $offset]);
$z->flush();
$z->tell();
$z->eof();
$z->seek($position, $whence);
$z->binmode();
$z->fileno();
$z->opened();
$z->autoflush();
$z->input_line_number();
$z->newStream( [OPTS] );
$z->deflateParams();
$z->close() ;
$DeflateError ;
# IO::File mode
print $z $string;
printf $z $format, $string;
tell $z
eof $z
seek $z, $position, $whence
binmode $z
fileno $z
close $z ;
This module provides a Perl interface that allows writing compressed data to files or buffer as defined in RFC 1950.
For reading RFC 1950 files/buffers, see the companion module IO::Uncompress::Inflate.
A top-level function, deflate
, is provided to carry out "one-shot" compression between buffers and/or files. For finer control over the compression process, see the "OO Interface" section.
use IO::Compress::Deflate qw(deflate $DeflateError) ;
deflate $input_filename_or_reference => $output_filename_or_reference [,OPTS]
or die "deflate failed: $DeflateError\n";
The functional interface needs Perl5.005 or better.
deflate
expects at least two parameters, $input_filename_or_reference
and $output_filename_or_reference
and zero or more optional parameters (see "Optional Parameters")
$input_filename_or_reference
parameterThe parameter, $input_filename_or_reference
, is used to define the source of the uncompressed data.
It can take one of the following forms:
If the $input_filename_or_reference
parameter is a simple scalar, it is assumed to be a filename. This file will be opened for reading and the input data will be read from it.
If the $input_filename_or_reference
parameter is a filehandle, the input data will be read from it. The string '-' can be used as an alias for standard input.
If $input_filename_or_reference
is a scalar reference, the input data will be read from $$input_filename_or_reference
.
If $input_filename_or_reference
is an array reference, each element in the array must be a filename.
The input data will be read from each file in turn.
The complete array will be walked to ensure that it only contains valid filenames before any data is compressed.
If $input_filename_or_reference
is a string that is delimited by the characters "<" and ">" deflate
will assume that it is an input fileglob string. The input is the list of files that match the fileglob.
See File::GlobMapper for more details.
If the $input_filename_or_reference
parameter is any other type, undef
will be returned.
$output_filename_or_reference
parameterThe parameter $output_filename_or_reference
is used to control the destination of the compressed data. This parameter can take one of these forms.
If the $output_filename_or_reference
parameter is a simple scalar, it is assumed to be a filename. This file will be opened for writing and the compressed data will be written to it.
If the $output_filename_or_reference
parameter is a filehandle, the compressed data will be written to it. The string '-' can be used as an alias for standard output.
If $output_filename_or_reference
is a scalar reference, the compressed data will be stored in $$output_filename_or_reference
.
If $output_filename_or_reference
is an array reference, the compressed data will be pushed onto the array.
If $output_filename_or_reference
is a string that is delimited by the characters "<" and ">" deflate
will assume that it is an output fileglob string. The output is the list of files that match the fileglob.
When $output_filename_or_reference
is an fileglob string, $input_filename_or_reference
must also be a fileglob string. Anything else is an error.
See File::GlobMapper for more details.
If the $output_filename_or_reference
parameter is any other type, undef
will be returned.
When $input_filename_or_reference
maps to multiple files/buffers and $output_filename_or_reference
is a single file/buffer the input files/buffers will be stored in $output_filename_or_reference
as a concatenated series of compressed data streams.
The optional parameters for the one-shot function deflate
are (for the most part) identical to those used with the OO interface defined in the "Constructor Options" section. The exceptions are listed below
AutoClose => 0|1
This option applies to any input or output data streams to deflate
that are filehandles.
If AutoClose
is specified, and the value is true, it will result in all input and/or output filehandles being closed once deflate
has completed.
This parameter defaults to 0.
BinModeIn => 0|1
This option is now a no-op. All files will be read in binmode.
Append => 0|1
The behaviour of this option is dependent on the type of output data stream.
A Buffer
If Append
is enabled, all compressed data will be append to the end of the output buffer. Otherwise the output buffer will be cleared before any compressed data is written to it.
A Filename
If Append
is enabled, the file will be opened in append mode. Otherwise the contents of the file, if any, will be truncated before any compressed data is written to it.
A Filehandle
If Append
is enabled, the filehandle will be positioned to the end of the file via a call to seek
before any compressed data is written to it. Otherwise the file pointer will not be moved.
When Append
is specified, and set to true, it will append all compressed data to the output data stream.
So when the output is a filehandle it will carry out a seek to the eof before writing any compressed data. If the output is a filename, it will be opened for appending. If the output is a buffer, all compressed data will be appended to the existing buffer.
Conversely when Append
is not specified, or it is present and is set to false, it will operate as follows.
When the output is a filename, it will truncate the contents of the file before writing any compressed data. If the output is a filehandle its position will not be changed. If the output is a buffer, it will be wiped before any compressed data is output.
Defaults to 0.
Here are a few example that show the capabilities of the module.
This very simple command line example demonstrates the streaming capabilities of the module. The code reads data from STDIN, compresses it, and writes the compressed data to STDOUT.
$ echo hello world | perl -MIO::Compress::Deflate=deflate -e 'deflate \*STDIN => \*STDOUT' >output.1950
The special filename "-" can be used as a standin for both \*STDIN
and \*STDOUT
, so the above can be rewritten as
$ echo hello world | perl -MIO::Compress::Deflate=deflate -e 'deflate "-" => "-"' >output.1950