1IO::Compress::Deflate(3U)ser Contributed Perl DocumentatiIoOn::Compress::Deflate(3)
2
3
4
6 IO::Compress::Deflate - Write RFC 1950 files/buffers
7
9 use IO::Compress::Deflate qw(deflate $DeflateError) ;
10
11 my $status = deflate $input => $output [,OPTS]
12 or die "deflate failed: $DeflateError\n";
13
14 my $z = new IO::Compress::Deflate $output [,OPTS]
15 or die "deflate failed: $DeflateError\n";
16
17 $z->print($string);
18 $z->printf($format, $string);
19 $z->write($string);
20 $z->syswrite($string [, $length, $offset]);
21 $z->flush();
22 $z->tell();
23 $z->eof();
24 $z->seek($position, $whence);
25 $z->binmode();
26 $z->fileno();
27 $z->opened();
28 $z->autoflush();
29 $z->input_line_number();
30 $z->newStream( [OPTS] );
31
32 $z->deflateParams();
33
34 $z->close() ;
35
36 $DeflateError ;
37
38 # IO::File mode
39
40 print $z $string;
41 printf $z $format, $string;
42 tell $z
43 eof $z
44 seek $z, $position, $whence
45 binmode $z
46 fileno $z
47 close $z ;
48
50 This module provides a Perl interface that allows writing compressed
51 data to files or buffer as defined in RFC 1950.
52
53 For reading RFC 1950 files/buffers, see the companion module IO::Uncom‐
54 press::Inflate.
55
57 A top-level function, "deflate", is provided to carry out "one-shot"
58 compression between buffers and/or files. For finer control over the
59 compression process, see the "OO Interface" section.
60
61 use IO::Compress::Deflate qw(deflate $DeflateError) ;
62
63 deflate $input => $output [,OPTS]
64 or die "deflate failed: $DeflateError\n";
65
66 The functional interface needs Perl5.005 or better.
67
68 deflate $input => $output [, OPTS]
69
70 "deflate" expects at least two parameters, $input and $output.
71
72 The $input parameter
73
74 The parameter, $input, is used to define the source of the uncompressed
75 data.
76
77 It can take one of the following forms:
78
79 A filename
80 If the $input parameter is a simple scalar, it is assumed to be a
81 filename. This file will be opened for reading and the input data
82 will be read from it.
83
84 A filehandle
85 If the $input parameter is a filehandle, the input data will be
86 read from it. The string '-' can be used as an alias for standard
87 input.
88
89 A scalar reference
90 If $input is a scalar reference, the input data will be read from
91 $$input.
92
93 An array reference
94 If $input is an array reference, each element in the array must be
95 a filename.
96
97 The input data will be read from each file in turn.
98
99 The complete array will be walked to ensure that it only contains
100 valid filenames before any data is compressed.
101
102 An Input FileGlob string
103 If $input is a string that is delimited by the characters "<" and
104 ">" "deflate" will assume that it is an input fileglob string. The
105 input is the list of files that match the fileglob.
106
107 If the fileglob does not match any files ...
108
109 See File::GlobMapper for more details.
110
111 If the $input parameter is any other type, "undef" will be returned.
112
113 The $output parameter
114
115 The parameter $output is used to control the destination of the com‐
116 pressed data. This parameter can take one of these forms.
117
118 A filename
119 If the $output parameter is a simple scalar, it is assumed to be a
120 filename. This file will be opened for writing and the compressed
121 data will be written to it.
122
123 A filehandle
124 If the $output parameter is a filehandle, the compressed data will
125 be written to it. The string '-' can be used as an alias for
126 standard output.
127
128 A scalar reference
129 If $output is a scalar reference, the compressed data will be
130 stored in $$output.
131
132 An Array Reference
133 If $output is an array reference, the compressed data will be
134 pushed onto the array.
135
136 An Output FileGlob
137 If $output is a string that is delimited by the characters "<" and
138 ">" "deflate" will assume that it is an output fileglob string.
139 The output is the list of files that match the fileglob.
140
141 When $output is an fileglob string, $input must also be a fileglob
142 string. Anything else is an error.
143
144 If the $output parameter is any other type, "undef" will be returned.
145
146 Notes
147
148 When $input maps to multiple files/buffers and $output is a single
149 file/buffer the input files/buffers will be stored in $output as a con‐
150 catenated series of compressed data streams.
151
152 Optional Parameters
153
154 Unless specified below, the optional parameters for "deflate", "OPTS",
155 are the same as those used with the OO interface defined in the "Con‐
156 structor Options" section below.
157
158 "AutoClose => 0⎪1"
159 This option applies to any input or output data streams to
160 "deflate" that are filehandles.
161
162 If "AutoClose" is specified, and the value is true, it will result
163 in all input and/or output filehandles being closed once "deflate"
164 has completed.
165
166 This parameter defaults to 0.
167
168 "BinModeIn => 0⎪1"
169 When reading from a file or filehandle, set "binmode" before read‐
170 ing.
171
172 Defaults to 0.
173
174 "Append => 0⎪1"
175 TODO
176
177 Examples
178
179 To read the contents of the file "file1.txt" and write the compressed
180 data to the file "file1.txt.1950".
181
182 use strict ;
183 use warnings ;
184 use IO::Compress::Deflate qw(deflate $DeflateError) ;
185
186 my $input = "file1.txt";
187 deflate $input => "$input.1950"
188 or die "deflate failed: $DeflateError\n";
189
190 To read from an existing Perl filehandle, $input, and write the com‐
191 pressed data to a buffer, $buffer.
192
193 use strict ;
194 use warnings ;
195 use IO::Compress::Deflate qw(deflate $DeflateError) ;
196 use IO::File ;
197
198 my $input = new IO::File "<file1.txt"
199 or die "Cannot open 'file1.txt': $!\n" ;
200 my $buffer ;
201 deflate $input => \$buffer
202 or die "deflate failed: $DeflateError\n";
203
204 To compress all files in the directory "/my/home" that match "*.txt"
205 and store the compressed data in the same directory
206
207 use strict ;
208 use warnings ;
209 use IO::Compress::Deflate qw(deflate $DeflateError) ;
210
211 deflate '</my/home/*.txt>' => '<*.1950>'
212 or die "deflate failed: $DeflateError\n";
213
214 and if you want to compress each file one at a time, this will do the
215 trick
216
217 use strict ;
218 use warnings ;
219 use IO::Compress::Deflate qw(deflate $DeflateError) ;
220
221 for my $input ( glob "/my/home/*.txt" )
222 {
223 my $output = "$input.1950" ;
224 deflate $input => $output
225 or die "Error compressing '$input': $DeflateError\n";
226 }
227
229 Constructor
230
231 The format of the constructor for "IO::Compress::Deflate" is shown
232 below
233
234 my $z = new IO::Compress::Deflate $output [,OPTS]
235 or die "IO::Compress::Deflate failed: $DeflateError\n";
236
237 It returns an "IO::Compress::Deflate" object on success and undef on
238 failure. The variable $DeflateError will contain an error message on
239 failure.
240
241 If you are running Perl 5.005 or better the object, $z, returned from
242 IO::Compress::Deflate can be used exactly like an IO::File filehandle.
243 This means that all normal output file operations can be carried out
244 with $z. For example, to write to a compressed file/buffer you can use
245 either of these forms
246
247 $z->print("hello world\n");
248 print $z "hello world\n";
249
250 The mandatory parameter $output is used to control the destination of
251 the compressed data. This parameter can take one of these forms.
252
253 A filename
254 If the $output parameter is a simple scalar, it is assumed to be a
255 filename. This file will be opened for writing and the compressed
256 data will be written to it.
257
258 A filehandle
259 If the $output parameter is a filehandle, the compressed data will
260 be written to it. The string '-' can be used as an alias for
261 standard output.
262
263 A scalar reference
264 If $output is a scalar reference, the compressed data will be
265 stored in $$output.
266
267 If the $output parameter is any other type, "IO::Com‐
268 press::Deflate"::new will return undef.
269
270 Constructor Options
271
272 "OPTS" is any combination of the following options:
273
274 "AutoClose => 0⎪1"
275 This option is only valid when the $output parameter is a filehan‐
276 dle. If specified, and the value is true, it will result in the
277 $output being closed once either the "close" method is called or
278 the "IO::Compress::Deflate" object is destroyed.
279
280 This parameter defaults to 0.
281
282 "Append => 0⎪1"
283 Opens $output in append mode.
284
285 The behaviour of this option is dependent on the type of $output.
286
287 * A Buffer
288 If $output is a buffer and "Append" is enabled, all com‐
289 pressed data will be append to the end if $output. Otherwise
290 $output will be cleared before any data is written to it.
291
292 * A Filename
293 If $output is a filename and "Append" is enabled, the file
294 will be opened in append mode. Otherwise the contents of the
295 file, if any, will be truncated before any compressed data is
296 written to it.
297
298 * A Filehandle
299 If $output is a filehandle, the file pointer will be posi‐
300 tioned to the end of the file via a call to "seek" before any
301 compressed data is written to it. Otherwise the file pointer
302 will not be moved.
303
304 This parameter defaults to 0.
305
306 "Merge => 0⎪1"
307 This option is used to compress input data and append it to an
308 existing compressed data stream in $output. The end result is a
309 single compressed data stream stored in $output.
310
311 It is a fatal error to attempt to use this option when $output is
312 not an RFC 1950 data stream.
313
314 There are a number of other limitations with the "Merge" option:
315
316 1 This module needs to have been built with zlib 1.2.1 or bet‐
317 ter to work. A fatal error will be thrown if "Merge" is used
318 with an older version of zlib.
319
320 2 If $output is a file or a filehandle, it must be seekable.
321
322 This parameter defaults to 0.
323
324 -Level
325 Defines the compression level used by zlib. The value should
326 either be a number between 0 and 9 (0 means no compression and 9
327 is maximum compression), or one of the symbolic constants defined
328 below.
329
330 Z_NO_COMPRESSION
331 Z_BEST_SPEED
332 Z_BEST_COMPRESSION
333 Z_DEFAULT_COMPRESSION
334
335 The default is Z_DEFAULT_COMPRESSION.
336
337 Note, these constants are not imported by "IO::Compress::Deflate"
338 by default.
339
340 use IO::Compress::Deflate qw(:strategy);
341 use IO::Compress::Deflate qw(:constants);
342 use IO::Compress::Deflate qw(:all);
343
344 -Strategy
345 Defines the strategy used to tune the compression. Use one of the
346 symbolic constants defined below.
347
348 Z_FILTERED
349 Z_HUFFMAN_ONLY
350 Z_RLE
351 Z_FIXED
352 Z_DEFAULT_STRATEGY
353
354 The default is Z_DEFAULT_STRATEGY.
355
356 "Strict => 0⎪1"
357 This is a placeholder option.
358
359 Examples
360
361 TODO
362
364 print
365
366 Usage is
367
368 $z->print($data)
369 print $z $data
370
371 Compresses and outputs the contents of the $data parameter. This has
372 the same behaviour as the "print" built-in.
373
374 Returns true if successful.
375
376 printf
377
378 Usage is
379
380 $z->printf($format, $data)
381 printf $z $format, $data
382
383 Compresses and outputs the contents of the $data parameter.
384
385 Returns true if successful.
386
387 syswrite
388
389 Usage is
390
391 $z->syswrite $data
392 $z->syswrite $data, $length
393 $z->syswrite $data, $length, $offset
394
395 Compresses and outputs the contents of the $data parameter.
396
397 Returns the number of uncompressed bytes written, or "undef" if unsuc‐
398 cessful.
399
400 write
401
402 Usage is
403
404 $z->write $data
405 $z->write $data, $length
406 $z->write $data, $length, $offset
407
408 Compresses and outputs the contents of the $data parameter.
409
410 Returns the number of uncompressed bytes written, or "undef" if unsuc‐
411 cessful.
412
413 flush
414
415 Usage is
416
417 $z->flush;
418 $z->flush($flush_type);
419
420 Flushes any pending compressed data to the output file/buffer.
421
422 This method takes an optional parameter, $flush_type, that controls how
423 the flushing will be carried out. By default the $flush_type used is
424 "Z_FINISH". Other valid values for $flush_type are "Z_NO_FLUSH",
425 "Z_SYNC_FLUSH", "Z_FULL_FLUSH" and "Z_BLOCK". It is strongly recom‐
426 mended that you only set the "flush_type" parameter if you fully under‐
427 stand the implications of what it does - overuse of "flush" can seri‐
428 ously degrade the level of compression achieved. See the "zlib" docu‐
429 mentation for details.
430
431 Returns true on success.
432
433 tell
434
435 Usage is
436
437 $z->tell()
438 tell $z
439
440 Returns the uncompressed file offset.
441
442 eof
443
444 Usage is
445
446 $z->eof();
447 eof($z);
448
449 Returns true if the "close" method has been called.
450
451 seek
452
453 $z->seek($position, $whence);
454 seek($z, $position, $whence);
455
456 Provides a sub-set of the "seek" functionality, with the restriction
457 that it is only legal to seek forward in the output file/buffer. It is
458 a fatal error to attempt to seek backward.
459
460 Empty parts of the file/buffer will have NULL (0x00) bytes written to
461 them.
462
463 The $whence parameter takes one the usual values, namely SEEK_SET,
464 SEEK_CUR or SEEK_END.
465
466 Returns 1 on success, 0 on failure.
467
468 binmode
469
470 Usage is
471
472 $z->binmode
473 binmode $z ;
474
475 This is a noop provided for completeness.
476
477 opened
478
479 $z->opened()
480
481 Returns true if the object currently refers to a opened file/buffer.
482
483 autoflush
484
485 my $prev = $z->autoflush()
486 my $prev = $z->autoflush(EXPR)
487
488 If the $z object is associated with a file or a filehandle, this method
489 returns the current autoflush setting for the underlying filehandle. If
490 "EXPR" is present, and is non-zero, it will enable flushing after every
491 write/print operation.
492
493 If $z is associated with a buffer, this method has no effect and always
494 returns "undef".
495
496 Note that the special variable $⎪ cannot be used to set or retrieve the
497 autoflush setting.
498
499 input_line_number
500
501 $z->input_line_number()
502 $z->input_line_number(EXPR)
503
504 This method always returns "undef" when compressing.
505
506 fileno
507
508 $z->fileno()
509 fileno($z)
510
511 If the $z object is associated with a file or a filehandle, this method
512 will return the underlying file descriptor.
513
514 If the $z object is is associated with a buffer, this method will
515 return undef.
516
517 close
518
519 $z->close() ;
520 close $z ;
521
522 Flushes any pending compressed data and then closes the output
523 file/buffer.
524
525 For most versions of Perl this method will be automatically invoked if
526 the IO::Compress::Deflate object is destroyed (either explicitly or by
527 the variable with the reference to the object going out of scope). The
528 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
529 cases, the "close" method will be called automatically, but not until
530 global destruction of all live objects when the program is terminating.
531
532 Therefore, if you want your scripts to be able to run on all versions
533 of Perl, you should call "close" explicitly and not rely on automatic
534 closing.
535
536 Returns true on success, otherwise 0.
537
538 If the "AutoClose" option has been enabled when the IO::Com‐
539 press::Deflate object was created, and the object is associated with a
540 file, the underlying file will also be closed.
541
542 newStream([OPTS])
543
544 Usage is
545
546 $z->newStream( [OPTS] )
547
548 Closes the current compressed data stream and starts a new one.
549
550 OPTS consists of any of the the options that are available when creat‐
551 ing the $z object.
552
553 See the "Constructor Options" section for more details.
554
555 deflateParams
556
557 Usage is
558
559 $z->deflateParams
560
561 TODO
562
564 A number of symbolic constants are required by some methods in
565 "IO::Compress::Deflate". None are imported by default.
566
567 :all Imports "deflate", $DeflateError and all symbolic constants that
568 can be used by "IO::Compress::Deflate". Same as doing this
569
570 use IO::Compress::Deflate qw(deflate $DeflateError :constants) ;
571
572 :constants
573 Import all symbolic constants. Same as doing this
574
575 use IO::Compress::Deflate qw(:flush :level :strategy) ;
576
577 :flush
578 These symbolic constants are used by the "flush" method.
579
580 Z_NO_FLUSH
581 Z_PARTIAL_FLUSH
582 Z_SYNC_FLUSH
583 Z_FULL_FLUSH
584 Z_FINISH
585 Z_BLOCK
586
587 :level
588 These symbolic constants are used by the "Level" option in the
589 constructor.
590
591 Z_NO_COMPRESSION
592 Z_BEST_SPEED
593 Z_BEST_COMPRESSION
594 Z_DEFAULT_COMPRESSION
595
596 :strategy
597 These symbolic constants are used by the "Strategy" option in the
598 constructor.
599
600 Z_FILTERED
601 Z_HUFFMAN_ONLY
602 Z_RLE
603 Z_FIXED
604 Z_DEFAULT_STRATEGY
605
606 For
607
609 TODO
610
612 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip, IO::Uncom‐
613 press::Inflate, IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
614 IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzop,
615 IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf,
616 IO::Uncompress::AnyInflate, IO::Uncompress::AnyUncompress
617
618 Compress::Zlib::FAQ
619
620 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
621
622 For RFC 1950, 1951 and 1952 see http://www.faqs.org/rfcs/rfc1950.html,
623 http://www.faqs.org/rfcs/rfc1951.html and
624 http://www.faqs.org/rfcs/rfc1952.html
625
626 The zlib compression library was written by Jean-loup Gailly
627 gzip@prep.ai.mit.edu and Mark Adler madler@alumni.caltech.edu.
628
629 The primary site for the zlib compression library is
630 http://www.zlib.org.
631
632 The primary site for gzip is http://www.gzip.org.
633
635 This module was written by Paul Marquess, pmqs@cpan.org.
636
638 See the Changes file.
639
641 Copyright (c) 2005-2007 Paul Marquess. All rights reserved.
642
643 This program is free software; you can redistribute it and/or modify it
644 under the same terms as Perl itself.
645
646
647
648perl v5.8.8 2007-06-18 IO::Compress::Deflate(3)