File Coverage

blib/lib/IO/Compress/RawDeflate.pm
Criterion Covered Total %
statement 80 80 100.0
branch 13 20 65.0
condition 2 3 66.6
subroutine 22 22 100.0
pod 3 13 23.0
total 120 138 86.9


line stmt bran cond sub pod time code
1             package IO::Compress::RawDeflate ;
2              
3             # create RFC1951
4             #
5 103     103   123062 use strict ;
  103         222  
  103         8745  
6 103     103   563 use warnings;
  103         259  
  103         12435  
7 103     103   6131 use bytes;
  103         6086  
  103         633  
8              
9 103     103   79554 use IO::Compress::Base 2.219 ;
  103         2655  
  103         7327  
10 103     103   721 use IO::Compress::Base::Common 2.219 qw(:Status :Parse);
  103         1525  
  103         23514  
11 103     103   63184 use IO::Compress::Adapter::Deflate 2.219 ;
  103         2424  
  103         25641  
12 103     103   817 use Compress::Raw::Zlib 2.218 qw(Z_DEFLATED Z_DEFAULT_COMPRESSION Z_DEFAULT_STRATEGY);
  103         1972  
  103         78071  
13              
14             require Exporter ;
15              
16             our ($VERSION, @ISA, @EXPORT_OK, %DEFLATE_CONSTANTS, %EXPORT_TAGS, $RawDeflateError);
17              
18             $VERSION = '2.219';
19             $RawDeflateError = '';
20              
21             @ISA = qw(IO::Compress::Base Exporter);
22             @EXPORT_OK = qw( $RawDeflateError rawdeflate ) ;
23             push @EXPORT_OK, @IO::Compress::Adapter::Deflate::EXPORT_OK ;
24              
25             %EXPORT_TAGS = %IO::Compress::Adapter::Deflate::DEFLATE_CONSTANTS;
26              
27              
28             {
29             my %seen;
30             foreach (keys %EXPORT_TAGS )
31             {
32             push @{$EXPORT_TAGS{constants}},
33             grep { !$seen{$_}++ }
34             @{ $EXPORT_TAGS{$_} }
35             }
36             $EXPORT_TAGS{all} = $EXPORT_TAGS{constants} ;
37             }
38              
39              
40             %DEFLATE_CONSTANTS = %EXPORT_TAGS;
41              
42             #push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
43              
44             Exporter::export_ok_tags('all');
45              
46              
47              
48             sub new
49             {
50 168     168 1 363502 my $class = shift ;
51              
52 168         876 my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$RawDeflateError);
53              
54 168         821 return $obj->_create(undef, @_);
55             }
56              
57             sub rawdeflate
58             {
59 152     152 1 190617 my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$RawDeflateError);
60 152         751 return $obj->_def(@_);
61             }
62              
63             sub ckParams
64             {
65 642     642 0 1340 my $self = shift ;
66 642         1087 my $got = shift;
67              
68 642         2120 return 1 ;
69             }
70              
71             sub mkComp
72             {
73 716     716 0 2547 my $self = shift ;
74 716         1239 my $got = shift ;
75              
76 716         2261 my ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject(
77             $got->getValue('crc32'),
78             $got->getValue('adler32'),
79             $got->getValue('level'),
80             $got->getValue('strategy')
81             );
82              
83 716 50       2491 return $self->saveErrorString(undef, $errstr, $errno)
84             if ! defined $obj;
85              
86 716         5743 return $obj;
87             }
88              
89              
90             sub mkHeader
91             {
92 294     294 0 568 my $self = shift ;
93 294         1190 return '';
94             }
95              
96             sub mkTrailer
97             {
98 314     314 0 626 my $self = shift ;
99 314         853 return '';
100             }
101              
102             sub mkFinalTrailer
103             {
104 287     287 0 767 return '';
105             }
106              
107              
108             #sub newHeader
109             #{
110             # my $self = shift ;
111             # return '';
112             #}
113              
114             sub getExtraParams
115             {
116 319     319 0 971 my $self = shift ;
117 319         909 return getZlibParams();
118             }
119              
120             our %PARAMS = (
121             #'method' => [IO::Compress::Base::Common::Parse_unsigned, Z_DEFLATED],
122             'level' => [IO::Compress::Base::Common::Parse_signed, Z_DEFAULT_COMPRESSION],
123             'strategy' => [IO::Compress::Base::Common::Parse_signed, Z_DEFAULT_STRATEGY],
124              
125             'crc32' => [IO::Compress::Base::Common::Parse_boolean, 0],
126             'adler32' => [IO::Compress::Base::Common::Parse_boolean, 0],
127             'merge' => [IO::Compress::Base::Common::Parse_boolean, 0],
128             );
129              
130             sub getZlibParams
131             {
132 1165     1165 0 19439 return %PARAMS;
133             }
134              
135             sub getInverseClass
136             {
137 103     103   947 no warnings 'once';
  103         217  
  103         13794  
138 23     23 0 68 return ('IO::Uncompress::RawInflate',
139             \$IO::Uncompress::RawInflate::RawInflateError);
140             }
141              
142             sub getFileInfo
143             {
144 102     102 0 195 my $self = shift ;
145 102         220 my $params = shift;
146 102         233 my $file = shift ;
147              
148             }
149              
150 103     103   761 use Fcntl qw(SEEK_SET);
  103         280  
  103         65391  
151              
152             sub createMerge
153             {
154 69     69 0 122 my $self = shift ;
155 69         143 my $outValue = shift ;
156 69         121 my $outType = shift ;
157              
158 69         242 my ($invClass, $error_ref) = $self->getInverseClass();
159 69 50       5536 eval "require $invClass"
160             or die "aaaahhhh" ;
161              
162 69 100       549 my $inf = $invClass->new( $outValue,
163             Transparent => 0,
164             #Strict => 1,
165             AutoClose => 0,
166             Scan => 1)
167             or return $self->saveErrorString(undef, "Cannot create InflateScan object: $$error_ref" ) ;
168              
169 60         109 my $end_offset = 0;
170 60 50       288 $inf->scan()
171             or return $self->saveErrorString(undef, "Error Scanning: $$error_ref", $inf->errorNo) ;
172 60 50       205 $inf->zap($end_offset)
173             or return $self->saveErrorString(undef, "Error Zapping: $$error_ref", $inf->errorNo) ;
174              
175 60         211 my $def = *$self->{Compress} = $inf->createDeflate();
176              
177 60         252 *$self->{Header} = *$inf->{Info}{Header};
178 60         344 *$self->{UnCompSize} = *$inf->{UnCompSize}->clone();
179 60         178 *$self->{CompSize} = *$inf->{CompSize}->clone();
180             # TODO -- fix this
181             #*$self->{CompSize} = U64->new(0, *$self->{UnCompSize_32bit});
182              
183              
184 60 100 66     373 if ( $outType eq 'buffer')
    50          
185 24         42 { substr( ${ *$self->{Buffer} }, $end_offset) = '' }
  24         85  
186             elsif ($outType eq 'handle' || $outType eq 'filename') {
187 36         185 *$self->{FH} = *$inf->{FH} ;
188 36         603 delete *$inf->{FH};
189 36         198 *$self->{FH}->flush() ;
190 36 100       114 *$self->{Handle} = 1 if $outType eq 'handle';
191              
192             #seek(*$self->{FH}, $end_offset, SEEK_SET)
193 36 50       159 *$self->{FH}->seek($end_offset, SEEK_SET)
194             or return $self->saveErrorString(undef, $!, $!) ;
195             }
196              
197 60         776 return $def ;
198             }
199              
200             #### zlib specific methods
201              
202             sub deflateParams
203             {
204 3     3 1 44 my $self = shift ;
205              
206 3         8 my $level = shift ;
207 3         8 my $strategy = shift ;
208              
209 3         21 my $status = *$self->{Compress}->deflateParams(Level => $level, Strategy => $strategy) ;
210             return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo})
211 3 50       13 if $status == STATUS_ERROR;
212              
213 3         17 return 1;
214             }
215              
216              
217              
218              
219             1;
220              
221             __END__
222              
223             =head1 NAME
224              
225             IO::Compress::RawDeflate - Write RFC 1951 files/buffers
226              
227             =head1 SYNOPSIS
228              
229             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
230              
231             my $status = rawdeflate $input => $output [,OPTS]
232             or die "rawdeflate failed: $RawDeflateError\n";
233              
234             my $z = IO::Compress::RawDeflate->new( $output [,OPTS] )
235             or die "rawdeflate failed: $RawDeflateError\n";
236              
237             $z->print($string);
238             $z->printf($format, $string);
239             $z->write($string);
240             $z->syswrite($string [, $length, $offset]);
241             $z->flush();
242             $z->tell();
243             $z->eof();
244             $z->seek($position, $whence);
245             $z->binmode();
246             $z->fileno();
247             $z->opened();
248             $z->autoflush();
249             $z->input_line_number();
250             $z->newStream( [OPTS] );
251              
252             $z->deflateParams();
253              
254             $z->close() ;
255              
256             $RawDeflateError ;
257              
258             # IO::File mode
259              
260             print $z $string;
261             printf $z $format, $string;
262             tell $z
263             eof $z
264             seek $z, $position, $whence
265             binmode $z
266             fileno $z
267             close $z ;
268              
269             =head1 DESCRIPTION
270              
271             This module provides a Perl interface that allows writing compressed
272             data to files or buffer as defined in RFC 1951.
273              
274             Note that RFC 1951 data is not a good choice of compression format
275             to use in isolation, especially if you want to auto-detect it.
276              
277             For reading RFC 1951 files/buffers, see the companion module
278             L<IO::Uncompress::RawInflate|IO::Uncompress::RawInflate>.
279              
280             =head1 Functional Interface
281              
282             A top-level function, C<rawdeflate>, is provided to carry out
283             "one-shot" compression between buffers and/or files. For finer
284             control over the compression process, see the L</"OO Interface">
285             section.
286              
287             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
288              
289             rawdeflate $input_filename_or_reference => $output_filename_or_reference [,OPTS]
290             or die "rawdeflate failed: $RawDeflateError\n";
291              
292             The functional interface needs Perl5.005 or better.
293              
294             =head2 rawdeflate $input_filename_or_reference => $output_filename_or_reference [, OPTS]
295              
296             C<rawdeflate> expects at least two parameters,
297             C<$input_filename_or_reference> and C<$output_filename_or_reference>
298             and zero or more optional parameters (see L</Optional Parameters>)
299              
300             =head3 The C<$input_filename_or_reference> parameter
301              
302             The parameter, C<$input_filename_or_reference>, is used to define the
303             source of the uncompressed data.
304              
305             It can take one of the following forms:
306              
307             =over 5
308              
309             =item A filename
310              
311             If the C<$input_filename_or_reference> parameter is a simple scalar, it is
312             assumed to be a filename. This file will be opened for reading and the
313             input data will be read from it.
314              
315             =item A filehandle
316              
317             If the C<$input_filename_or_reference> parameter is a filehandle, the input
318             data will be read from it. The string '-' can be used as an alias for
319             standard input.
320              
321             =item A scalar reference
322              
323             If C<$input_filename_or_reference> is a scalar reference, the input data
324             will be read from C<$$input_filename_or_reference>.
325              
326             =item An array reference
327              
328             If C<$input_filename_or_reference> is an array reference, each element in
329             the array must be a filename.
330              
331             The input data will be read from each file in turn.
332              
333             The complete array will be walked to ensure that it only
334             contains valid filenames before any data is compressed.
335              
336             =item An Input FileGlob string
337              
338             If C<$input_filename_or_reference> is a string that is delimited by the
339             characters "<" and ">" C<rawdeflate> will assume that it is an
340             I<input fileglob string>. The input is the list of files that match the
341             fileglob.
342              
343             See L<File::GlobMapper|File::GlobMapper> for more details.
344              
345             =back
346              
347             If the C<$input_filename_or_reference> parameter is any other type,
348             C<undef> will be returned.
349              
350             =head3 The C<$output_filename_or_reference> parameter
351              
352             The parameter C<$output_filename_or_reference> is used to control the
353             destination of the compressed data. This parameter can take one of
354             these forms.
355              
356             =over 5
357              
358             =item A filename
359              
360             If the C<$output_filename_or_reference> parameter is a simple scalar, it is
361             assumed to be a filename. This file will be opened for writing and the
362             compressed data will be written to it.
363              
364             =item A filehandle
365              
366             If the C<$output_filename_or_reference> parameter is a filehandle, the
367             compressed data will be written to it. The string '-' can be used as
368             an alias for standard output.
369              
370             =item A scalar reference
371              
372             If C<$output_filename_or_reference> is a scalar reference, the
373             compressed data will be stored in C<$$output_filename_or_reference>.
374              
375             =item An Array Reference
376              
377             If C<$output_filename_or_reference> is an array reference,
378             the compressed data will be pushed onto the array.
379              
380             =item An Output FileGlob
381              
382             If C<$output_filename_or_reference> is a string that is delimited by the
383             characters "<" and ">" C<rawdeflate> will assume that it is an
384             I<output fileglob string>. The output is the list of files that match the
385             fileglob.
386              
387             When C<$output_filename_or_reference> is an fileglob string,
388             C<$input_filename_or_reference> must also be a fileglob string. Anything
389             else is an error.
390              
391             See L<File::GlobMapper|File::GlobMapper> for more details.
392              
393             =back
394              
395             If the C<$output_filename_or_reference> parameter is any other type,
396             C<undef> will be returned.
397              
398             =head2 Notes
399              
400             When C<$input_filename_or_reference> maps to multiple files/buffers and
401             C<$output_filename_or_reference> is a single
402             file/buffer the input files/buffers will be stored
403             in C<$output_filename_or_reference> as a concatenated series of compressed data streams.
404              
405             =head2 Optional Parameters
406              
407             The optional parameters for the one-shot function C<rawdeflate>
408             are (for the most part) identical to those used with the OO interface defined in the
409             L</"Constructor Options"> section. The exceptions are listed below
410              
411             =over 5
412              
413             =item C<< AutoClose => 0|1 >>
414              
415             This option applies to any input or output data streams to
416             C<rawdeflate> that are filehandles.
417              
418             If C<AutoClose> is specified, and the value is true, it will result in all
419             input and/or output filehandles being closed once C<rawdeflate> has
420             completed.
421              
422             This parameter defaults to 0.
423              
424             =item C<< BinModeIn => 0|1 >>
425              
426             This option is now a no-op. All files will be read in binmode.
427              
428             =item C<< Append => 0|1 >>
429              
430             The behaviour of this option is dependent on the type of output data
431             stream.
432              
433             =over 5
434              
435             =item * A Buffer
436              
437             If C<Append> is enabled, all compressed data will be append to the end of
438             the output buffer. Otherwise the output buffer will be cleared before any
439             compressed data is written to it.
440              
441             =item * A Filename
442              
443             If C<Append> is enabled, the file will be opened in append mode. Otherwise
444             the contents of the file, if any, will be truncated before any compressed
445             data is written to it.
446              
447             =item * A Filehandle
448              
449             If C<Append> is enabled, the filehandle will be positioned to the end of
450             the file via a call to C<seek> before any compressed data is
451             written to it. Otherwise the file pointer will not be moved.
452              
453             =back
454              
455             When C<Append> is specified, and set to true, it will I<append> all compressed
456             data to the output data stream.
457              
458             So when the output is a filehandle it will carry out a seek to the eof
459             before writing any compressed data. If the output is a filename, it will be opened for
460             appending. If the output is a buffer, all compressed data will be
461             appended to the existing buffer.
462              
463             Conversely when C<Append> is not specified, or it is present and is set to
464             false, it will operate as follows.
465              
466             When the output is a filename, it will truncate the contents of the file
467             before writing any compressed data. If the output is a filehandle
468             its position will not be changed. If the output is a buffer, it will be
469             wiped before any compressed data is output.
470              
471             Defaults to 0.
472              
473             =back
474              
475             =head2 Oneshot Examples
476              
477             Here are a few example that show the capabilities of the module.
478              
479             =head3 Streaming
480              
481             This very simple command line example demonstrates the streaming capabilities of the module.
482             The code reads data from STDIN, compresses it, and writes the compressed data to STDOUT.
483              
484             $ echo hello world | perl -MIO::Compress::RawDeflate=rawdeflate -e 'rawdeflate \*STDIN => \*STDOUT' >output.1951
485              
486             The special filename "-" can be used as a standin for both C<\*STDIN> and C<\*STDOUT>,
487             so the above can be rewritten as
488              
489             $ echo hello world | perl -MIO::Compress::RawDeflate=rawdeflate -e 'rawdeflate "-" => "-"' >output.1951
490              
491             =head3 Compressing a file from the filesystem
492              
493             To read the contents of the file C<file1.txt> and write the compressed
494             data to the file C<file1.txt.1951>.
495              
496             use strict ;
497             use warnings ;
498             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
499              
500             my $input = "file1.txt";
501             rawdeflate $input => "$input.1951"
502             or die "rawdeflate failed: $RawDeflateError\n";
503              
504             =head3 Reading from a Filehandle and writing to an in-memory buffer
505              
506             To read from an existing Perl filehandle, C<$input>, and write the
507             compressed data to a buffer, C<$buffer>.
508              
509             use strict ;
510             use warnings ;
511             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
512             use IO::File ;
513              
514             my $input = IO::File->new( "<file1.txt" )
515             or die "Cannot open 'file1.txt': $!\n" ;
516             my $buffer ;
517             rawdeflate $input => \$buffer
518             or die "rawdeflate failed: $RawDeflateError\n";
519              
520             =head3 Compressing multiple files
521              
522             To compress all files in the directory "/my/home" that match "*.txt"
523             and store the compressed data in the same directory
524              
525             use strict ;
526             use warnings ;
527             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
528              
529             rawdeflate '</my/home/*.txt>' => '<*.1951>'
530             or die "rawdeflate failed: $RawDeflateError\n";
531              
532             and if you want to compress each file one at a time, this will do the trick
533              
534             use strict ;
535             use warnings ;
536             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
537              
538             for my $input ( glob "/my/home/*.txt" )
539             {
540             my $output = "$input.1951" ;
541             rawdeflate $input => $output
542             or die "Error compressing '$input': $RawDeflateError\n";
543             }
544              
545             =head1 OO Interface
546              
547             =head2 Constructor
548              
549             The format of the constructor for C<IO::Compress::RawDeflate> is shown below
550              
551             my $z = IO::Compress::RawDeflate->new( $output [,OPTS] )
552             or die "IO::Compress::RawDeflate failed: $RawDeflateError\n";
553              
554             The constructor takes one mandatory parameter, C<$output>, defined below and
555             zero or more C<OPTS>, defined in L<Constructor Options>.
556              
557             It returns an C<IO::Compress::RawDeflate> object on success and C<undef> on failure.
558             The variable C<$RawDeflateError> will contain an error message on failure.
559              
560             If you are running Perl 5.005 or better the object, C<$z>, returned from
561             IO::Compress::RawDeflate can be used exactly like an L<IO::File|IO::File> filehandle.
562             This means that all normal output file operations can be carried out
563             with C<$z>.
564             For example, to write to a compressed file/buffer you can use either of
565             these forms
566              
567             $z->print("hello world\n");
568             print $z "hello world\n";
569              
570             Below is a simple exaple of using the OO interface to create an output file
571             C<myfile.1951> and write some data to it.
572              
573             my $filename = "myfile.1951";
574             my $z = IO::Compress::RawDeflate->new($filename)
575             or die "IO::Compress::RawDeflate failed: $RawDeflateError\n";
576              
577             $z->print("abcde");
578             $z->close();
579              
580             See the L</Examples> for more.
581              
582             The mandatory parameter C<$output> is used to control the destination
583             of the compressed data. This parameter can take one of these forms.
584              
585             =over 5
586              
587             =item A filename
588              
589             If the C<$output> parameter is a simple scalar, it is assumed to be a
590             filename. This file will be opened for writing and the compressed data
591             will be written to it.
592              
593             =item A filehandle
594              
595             If the C<$output> parameter is a filehandle, the compressed data will be
596             written to it.
597             The string '-' can be used as an alias for standard output.
598              
599             =item A scalar reference
600              
601             If C<$output> is a scalar reference, the compressed data will be stored
602             in C<$$output>.
603              
604             =back
605              
606             If the C<$output> parameter is any other type, C<IO::Compress::RawDeflate>::new will
607             return undef.
608              
609             =head2 Constructor Options
610              
611             C<OPTS> is any combination of zero or more the following options:
612              
613             =over 5
614              
615             =item C<< AutoClose => 0|1 >>
616              
617             This option is only valid when the C<$output> parameter is a filehandle. If
618             specified, and the value is true, it will result in the C<$output> being
619             closed once either the C<close> method is called or the C<IO::Compress::RawDeflate>
620             object is destroyed.
621              
622             This parameter defaults to 0.
623              
624             =item C<< Append => 0|1 >>
625              
626             Opens C<$output> in append mode.
627              
628             The behaviour of this option is dependent on the type of C<$output>.
629              
630             =over 5
631              
632             =item * A Buffer
633              
634             If C<$output> is a buffer and C<Append> is enabled, all compressed data
635             will be append to the end of C<$output>. Otherwise C<$output> will be
636             cleared before any data is written to it.
637              
638             =item * A Filename
639              
640             If C<$output> is a filename and C<Append> is enabled, the file will be
641             opened in append mode. Otherwise the contents of the file, if any, will be
642             truncated before any compressed data is written to it.
643              
644             =item * A Filehandle
645              
646             If C<$output> is a filehandle, the file pointer will be positioned to the
647             end of the file via a call to C<seek> before any compressed data is written
648             to it. Otherwise the file pointer will not be moved.
649              
650             =back
651              
652             This parameter defaults to 0.
653              
654             =item C<< Merge => 0|1 >>
655              
656             This option is used to compress input data and append it to an existing
657             compressed data stream in C<$output>. The end result is a single compressed
658             data stream stored in C<$output>.
659              
660             It is a fatal error to attempt to use this option when C<$output> is not an
661             RFC 1951 data stream.
662              
663             There are a number of other limitations with the C<Merge> option:
664              
665             =over 5
666              
667             =item 1
668              
669             This module needs to have been built with zlib 1.2.1 or better to work. A
670             fatal error will be thrown if C<Merge> is used with an older version of
671             zlib.
672              
673             =item 2
674              
675             If C<$output> is a file or a filehandle, it must be seekable.
676              
677             =back
678              
679             This parameter defaults to 0.
680              
681             =item -Level
682              
683             Defines the compression level used by zlib. The value should either be
684             a number between 0 and 9 (0 means no compression and 9 is maximum
685             compression), or one of the symbolic constants defined below.
686              
687             Z_NO_COMPRESSION
688             Z_BEST_SPEED
689             Z_BEST_COMPRESSION
690             Z_DEFAULT_COMPRESSION
691              
692             The default is Z_DEFAULT_COMPRESSION.
693              
694             Note, these constants are not imported by C<IO::Compress::RawDeflate> by default.
695              
696             use IO::Compress::RawDeflate qw(:strategy);
697             use IO::Compress::RawDeflate qw(:constants);
698             use IO::Compress::RawDeflate qw(:all);
699              
700             =item -Strategy
701              
702             Defines the strategy used to tune the compression. Use one of the symbolic
703             constants defined below.
704              
705             Z_FILTERED
706             Z_HUFFMAN_ONLY
707             Z_RLE
708             Z_FIXED
709             Z_DEFAULT_STRATEGY
710              
711             The default is Z_DEFAULT_STRATEGY.
712              
713             =item C<< Strict => 0|1 >>
714              
715             This is a placeholder option.
716              
717             =back
718              
719             =head2 Examples
720              
721             =head3 Streaming
722              
723             This very simple command line example demonstrates the streaming capabilities
724             of the module. The code reads data from STDIN or all the files given on the
725             commandline, compresses it, and writes the compressed data to STDOUT.
726              
727             use strict ;
728             use warnings ;
729             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
730              
731             my $z = IO::Compress::RawDeflate->new("-", Stream => 1)
732             or die "IO::Compress::RawDeflate failed: $RawDeflateError\n";
733              
734             while (<>) {
735             $z->print("abcde");
736             }
737             $z->close();
738              
739             Note the use of C<"-"> to means C<STDOUT>. Alternatively you can use C<\*STDOUT>.
740              
741             =head3 Compressing a file from the filesystem
742              
743             To read the contents of the file C<file1.txt> and write the compressed
744             data to the file C<file1.txt.1951> there are a few options
745              
746             Start by creating the compression object and opening the input file
747              
748             use strict ;
749             use warnings ;
750             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError) ;
751              
752             my $input = "file1.txt";
753             my $z = IO::Compress::RawDeflate->new("file1.txt.1951")
754             or die "IO::Compress::RawDeflate failed: $RawDeflateError\n";
755              
756             # open the input file
757             open my $fh, "<", "file1.txt"
758             or die "Cannot open file1.txt: $!\n";
759              
760             # loop through the input file & write to the compressed file
761             while (<$fh>) {
762             $z->print($_);
763             }
764              
765             # not forgetting to close the compressed file
766             $z->close();
767              
768             =head1 Methods
769              
770             =head2 print
771              
772             Usage is
773              
774             $z->print($data)
775             print $z $data
776              
777             Compresses and outputs the contents of the C<$data> parameter. This
778             has the same behaviour as the C<print> built-in.
779              
780             Returns true if successful.
781              
782             =head2 printf
783              
784             Usage is
785              
786             $z->printf($format, $data)
787             printf $z $format, $data
788              
789             Compresses and outputs the contents of the C<$data> parameter.
790              
791             Returns true if successful.
792              
793             =head2 syswrite
794              
795             Usage is
796              
797             $z->syswrite $data
798             $z->syswrite $data, $length
799             $z->syswrite $data, $length, $offset
800              
801             Compresses and outputs the contents of the C<$data> parameter.
802              
803             Returns the number of uncompressed bytes written, or C<undef> if
804             unsuccessful.
805              
806             =head2 write
807              
808             Usage is
809              
810             $z->write $data
811             $z->write $data, $length
812             $z->write $data, $length, $offset
813              
814             Compresses and outputs the contents of the C<$data> parameter.
815              
816             Returns the number of uncompressed bytes written, or C<undef> if
817             unsuccessful.
818              
819             =head2 flush
820              
821             Usage is
822              
823             $z->flush;
824             $z->flush($flush_type);
825              
826             Flushes any pending compressed data to the output file/buffer.
827              
828             This method takes an optional parameter, C<$flush_type>, that controls
829             how the flushing will be carried out. By default the C<$flush_type>
830             used is C<Z_FINISH>. Other valid values for C<$flush_type> are
831             C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
832             strongly recommended that you only set the C<flush_type> parameter if
833             you fully understand the implications of what it does - overuse of C<flush>
834             can seriously degrade the level of compression achieved. See the C<zlib>
835             documentation for details.
836              
837             Returns true on success.
838              
839             =head2 tell
840              
841             Usage is
842              
843             $z->tell()
844             tell $z
845              
846             Returns the uncompressed file offset.
847              
848             =head2 eof
849              
850             Usage is
851              
852             $z->eof();
853             eof($z);
854              
855             Returns true if the C<close> method has been called.
856              
857             =head2 seek
858              
859             $z->seek($position, $whence);
860             seek($z, $position, $whence);
861              
862             Provides a sub-set of the C<seek> functionality, with the restriction
863             that it is only legal to seek forward in the output file/buffer.
864             It is a fatal error to attempt to seek backward.
865              
866             Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
867              
868             The C<$whence> parameter takes one the usual values, namely SEEK_SET,
869             SEEK_CUR or SEEK_END.
870              
871             Returns 1 on success, 0 on failure.
872              
873             =head2 binmode
874              
875             Usage is
876              
877             $z->binmode
878             binmode $z ;
879              
880             This is a noop provided for completeness.
881              
882             =head2 opened
883              
884             $z->opened()
885              
886             Returns true if the object currently refers to a opened file/buffer.
887              
888             =head2 autoflush
889              
890             my $prev = $z->autoflush()
891             my $prev = $z->autoflush(EXPR)
892              
893             If the C<$z> object is associated with a file or a filehandle, this method
894             returns the current autoflush setting for the underlying filehandle. If
895             C<EXPR> is present, and is non-zero, it will enable flushing after every
896             write/print operation.
897              
898             If C<$z> is associated with a buffer, this method has no effect and always
899             returns C<undef>.
900              
901             B<Note> that the special variable C<$|> B<cannot> be used to set or
902             retrieve the autoflush setting.
903              
904             =head2 input_line_number
905              
906             $z->input_line_number()
907             $z->input_line_number(EXPR)
908              
909             This method always returns C<undef> when compressing.
910              
911             =head2 fileno
912              
913             $z->fileno()
914             fileno($z)
915              
916             If the C<$z> object is associated with a file or a filehandle, C<fileno>
917             will return the underlying file descriptor. Once the C<close> method is
918             called C<fileno> will return C<undef>.
919              
920             If the C<$z> object is associated with a buffer, this method will return
921             C<undef>.
922              
923             =head2 close
924              
925             $z->close() ;
926             close $z ;
927              
928             Flushes any pending compressed data and then closes the output file/buffer.
929              
930             For most versions of Perl this method will be automatically invoked if
931             the IO::Compress::RawDeflate object is destroyed (either explicitly or by the
932             variable with the reference to the object going out of scope). The
933             exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
934             these cases, the C<close> method will be called automatically, but
935             not until global destruction of all live objects when the program is
936             terminating.
937              
938             Therefore, if you want your scripts to be able to run on all versions
939             of Perl, you should call C<close> explicitly and not rely on automatic
940             closing.
941              
942             Returns true on success, otherwise 0.
943              
944             If the C<AutoClose> option has been enabled when the IO::Compress::RawDeflate
945             object was created, and the object is associated with a file, the
946             underlying file will also be closed.
947              
948             =head2 newStream([OPTS])
949              
950             Usage is
951              
952             $z->newStream( [OPTS] )
953              
954             Closes the current compressed data stream and starts a new one.
955              
956             OPTS consists of any of the options that are available when creating
957             the C<$z> object.
958              
959             See the L</"Constructor Options"> section for more details.
960              
961             =head2 deflateParams
962              
963             Usage is
964              
965             $z->deflateParams
966              
967             TODO
968              
969             =head1 Importing
970              
971             A number of symbolic constants are required by some methods in
972             C<IO::Compress::RawDeflate>. None are imported by default.
973              
974             =over 5
975              
976             =item :all
977              
978             Imports C<rawdeflate>, C<$RawDeflateError> and all symbolic
979             constants that can be used by C<IO::Compress::RawDeflate>. Same as doing this
980              
981             use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError :constants) ;
982              
983             =item :constants
984              
985             Import all symbolic constants. Same as doing this
986              
987             use IO::Compress::RawDeflate qw(:flush :level :strategy) ;
988              
989             =item :flush
990              
991             These symbolic constants are used by the C<flush> method.
992              
993             Z_NO_FLUSH
994             Z_PARTIAL_FLUSH
995             Z_SYNC_FLUSH
996             Z_FULL_FLUSH
997             Z_FINISH
998             Z_BLOCK
999              
1000             =item :level
1001              
1002             These symbolic constants are used by the C<Level> option in the constructor.
1003              
1004             Z_NO_COMPRESSION
1005             Z_BEST_SPEED
1006             Z_BEST_COMPRESSION
1007             Z_DEFAULT_COMPRESSION
1008              
1009             =item :strategy
1010              
1011             These symbolic constants are used by the C<Strategy> option in the constructor.
1012              
1013             Z_FILTERED
1014             Z_HUFFMAN_ONLY
1015             Z_RLE
1016             Z_FIXED
1017             Z_DEFAULT_STRATEGY
1018              
1019             =back
1020              
1021             =head1 EXAMPLES
1022              
1023             =head2 Apache::GZip Revisited
1024              
1025             See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited">
1026              
1027             =head2 Working with Net::FTP
1028              
1029             See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP">
1030              
1031             =head1 SUPPORT
1032              
1033             General feedback/questions/bug reports should be sent to
1034             L<https://github.com/pmqs/IO-Compress/issues> (preferred) or
1035             L<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.
1036              
1037             =head1 SEE ALSO
1038              
1039             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::Lzip>, L<IO::Uncompress::UnLzip>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Compress::Zstd>, L<IO::Uncompress::UnZstd>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1040              
1041             L<IO::Compress::FAQ|IO::Compress::FAQ>
1042              
1043             L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1044             L<Archive::Tar|Archive::Tar>,
1045             L<IO::Zlib|IO::Zlib>
1046              
1047             For RFC 1950, 1951 and 1952 see
1048             L<https://datatracker.ietf.org/doc/html/rfc1950>,
1049             L<https://datatracker.ietf.org/doc/html/rfc1951> and
1050             L<https://datatracker.ietf.org/doc/html/rfc1952>
1051              
1052             The I<zlib> compression library was written by Jean-loup Gailly
1053             C<gzip@prep.ai.mit.edu> and Mark Adler C<madler@alumni.caltech.edu>.
1054              
1055             The primary site for the I<zlib> compression library is
1056             L<http://www.zlib.org>.
1057              
1058             The primary site for the I<zlib-ng> compression library is
1059             L<https://github.com/zlib-ng/zlib-ng>.
1060              
1061             The primary site for gzip is L<http://www.gzip.org>.
1062              
1063             =head1 AUTHOR
1064              
1065             This module was written by Paul Marquess, C<pmqs@cpan.org>.
1066              
1067             =head1 MODIFICATION HISTORY
1068              
1069             See the Changes file.
1070              
1071             =head1 COPYRIGHT AND LICENSE
1072              
1073             Copyright (c) 2005-2026 Paul Marquess. All rights reserved.
1074              
1075             This program is free software; you can redistribute it and/or
1076             modify it under the same terms as Perl itself.