Debian Codegraph @

upstream/debian-unstable/man3/IO::Compress::Gzip.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "IO::Compress::Gzip 3perl"
58 .TH IO::Compress::Gzip 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 IO::Compress::Gzip \- Write RFC 1952 files/buffers
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 1
68 \& use IO::Compress::Gzip qw(gzip $GzipError) ;
69 \&
70 \& my $status = gzip $input => $output [,OPTS]
71 \& or die "gzip failed: $GzipError\en";
72 \&
73 \& my $z = IO::Compress::Gzip\->new( $output [,OPTS] )
74 \& or die "gzip failed: $GzipError\en";
75 \&
76 \& $z\->print($string);
77 \& $z\->printf($format, $string);
78 \& $z\->write($string);
79 \& $z\->syswrite($string [, $length, $offset]);
80 \& $z\->flush();
81 \& $z\->tell();
82 \& $z\->eof();
83 \& $z\->seek($position, $whence);
84 \& $z\->binmode();
85 \& $z\->fileno();
86 \& $z\->opened();
87 \& $z\->autoflush();
88 \& $z\->input_line_number();
89 \& $z\->newStream( [OPTS] );
90 \&
91 \& $z\->deflateParams();
92 \&
93 \& $z\->close() ;
94 \&
95 \& $GzipError ;
96 \&
97 \& # IO::File mode
98 \&
99 \& print $z $string;
100 \& printf $z $format, $string;
101 \& tell $z
102 \& eof $z
103 \& seek $z, $position, $whence
104 \& binmode $z
105 \& fileno $z
106 \& close $z ;
107 .Ve
108 .SH DESCRIPTION
109 .IX Header "DESCRIPTION"
110 This module provides a Perl interface that allows writing compressed
111 data to files or buffer as defined in RFC 1952.
112 .PP
113 All the gzip headers defined in RFC 1952 can be created using
114 this module.
115 .PP
116 For reading RFC 1952 files/buffers, see the companion module
117 IO::Uncompress::Gunzip.
118 .SH "Functional Interface"
119 .IX Header "Functional Interface"
120 A top-level function, \f(CW\*(C`gzip\*(C'\fR, is provided to carry out
121 "one-shot" compression between buffers and/or files. For finer
122 control over the compression process, see the "OO Interface"
123 section.
124 .PP
125 .Vb 1
126 \& use IO::Compress::Gzip qw(gzip $GzipError) ;
127 \&
128 \& gzip $input_filename_or_reference => $output_filename_or_reference [,OPTS]
129 \& or die "gzip failed: $GzipError\en";
130 .Ve
131 .PP
132 The functional interface needs Perl5.005 or better.
133 .ie n .SS "gzip $input_filename_or_reference => $output_filename_or_reference [, OPTS]"
134 .el .SS "gzip \f(CW$input_filename_or_reference\fP => \f(CW$output_filename_or_reference\fP [, OPTS]"
135 .IX Subsection "gzip $input_filename_or_reference => $output_filename_or_reference [, OPTS]"
136 \&\f(CW\*(C`gzip\*(C'\fR expects at least two parameters,
137 \&\f(CW$input_filename_or_reference\fR and \f(CW$output_filename_or_reference\fR
138 and zero or more optional parameters (see "Optional Parameters")
139 .PP
140 \fIThe \fR\f(CI$input_filename_or_reference\fR\fI parameter\fR
141 .IX Subsection "The $input_filename_or_reference parameter"
142 .PP
143 The parameter, \f(CW$input_filename_or_reference\fR, is used to define the
144 source of the uncompressed data.
145 .PP
146 It can take one of the following forms:
147 .IP "A filename" 5
148 .IX Item "A filename"
149 If the \f(CW$input_filename_or_reference\fR parameter is a simple scalar, it is
150 assumed to be a filename. This file will be opened for reading and the
151 input data will be read from it.
152 .IP "A filehandle" 5
153 .IX Item "A filehandle"
154 If the \f(CW$input_filename_or_reference\fR parameter is a filehandle, the input
155 data will be read from it. The string '\-' can be used as an alias for
156 standard input.
157 .IP "A scalar reference" 5
158 .IX Item "A scalar reference"
159 If \f(CW$input_filename_or_reference\fR is a scalar reference, the input data
160 will be read from \f(CW$$input_filename_or_reference\fR.
161 .IP "An array reference" 5
162 .IX Item "An array reference"
163 If \f(CW$input_filename_or_reference\fR is an array reference, each element in
164 the array must be a filename.
165 .Sp
166 The input data will be read from each file in turn.
167 .Sp
168 The complete array will be walked to ensure that it only
169 contains valid filenames before any data is compressed.
170 .IP "An Input FileGlob string" 5
171 .IX Item "An Input FileGlob string"
172 If \f(CW$input_filename_or_reference\fR is a string that is delimited by the
173 characters "<" and ">" \f(CW\*(C`gzip\*(C'\fR will assume that it is an
174 \&\fIinput fileglob string\fR. The input is the list of files that match the
175 fileglob.
176 .Sp
177 See File::GlobMapper for more details.
178 .PP
179 If the \f(CW$input_filename_or_reference\fR parameter is any other type,
180 \&\f(CW\*(C`undef\*(C'\fR will be returned.
181 .PP
182 In addition, if \f(CW$input_filename_or_reference\fR is a simple filename,
183 the default values for
184 the \f(CW\*(C`Name\*(C'\fR and \f(CW\*(C`Time\*(C'\fR options will be sourced from that file.
185 .PP
186 If you do not want to use these defaults they can be overridden by
187 explicitly setting the \f(CW\*(C`Name\*(C'\fR and \f(CW\*(C`Time\*(C'\fR options or by setting the
188 \&\f(CW\*(C`Minimal\*(C'\fR parameter.
189 .PP
190 \fIThe \fR\f(CI$output_filename_or_reference\fR\fI parameter\fR
191 .IX Subsection "The $output_filename_or_reference parameter"
192 .PP
193 The parameter \f(CW$output_filename_or_reference\fR is used to control the
194 destination of the compressed data. This parameter can take one of
195 these forms.
196 .IP "A filename" 5
197 .IX Item "A filename"
198 If the \f(CW$output_filename_or_reference\fR parameter is a simple scalar, it is
199 assumed to be a filename. This file will be opened for writing and the
200 compressed data will be written to it.
201 .IP "A filehandle" 5
202 .IX Item "A filehandle"
203 If the \f(CW$output_filename_or_reference\fR parameter is a filehandle, the
204 compressed data will be written to it. The string '\-' can be used as
205 an alias for standard output.
206 .IP "A scalar reference" 5
207 .IX Item "A scalar reference"
208 If \f(CW$output_filename_or_reference\fR is a scalar reference, the
209 compressed data will be stored in \f(CW$$output_filename_or_reference\fR.
210 .IP "An Array Reference" 5
211 .IX Item "An Array Reference"
212 If \f(CW$output_filename_or_reference\fR is an array reference,
213 the compressed data will be pushed onto the array.
214 .IP "An Output FileGlob" 5
215 .IX Item "An Output FileGlob"
216 If \f(CW$output_filename_or_reference\fR is a string that is delimited by the
217 characters "<" and ">" \f(CW\*(C`gzip\*(C'\fR will assume that it is an
218 \&\fIoutput fileglob string\fR. The output is the list of files that match the
219 fileglob.
220 .Sp
221 When \f(CW$output_filename_or_reference\fR is an fileglob string,
222 \&\f(CW$input_filename_or_reference\fR must also be a fileglob string. Anything
223 else is an error.
224 .Sp
225 See File::GlobMapper for more details.
226 .PP
227 If the \f(CW$output_filename_or_reference\fR parameter is any other type,
228 \&\f(CW\*(C`undef\*(C'\fR will be returned.
229 .SS Notes
230 .IX Subsection "Notes"
231 When \f(CW$input_filename_or_reference\fR maps to multiple files/buffers and
232 \&\f(CW$output_filename_or_reference\fR is a single
233 file/buffer the input files/buffers will be stored
234 in \f(CW$output_filename_or_reference\fR as a concatenated series of compressed data streams.
235 .SS "Optional Parameters"
236 .IX Subsection "Optional Parameters"
237 The optional parameters for the one-shot function \f(CW\*(C`gzip\*(C'\fR
238 are (for the most part) identical to those used with the OO interface defined in the
239 "Constructor Options" section. The exceptions are listed below
240 .ie n .IP """AutoClose => 0|1""" 5
241 .el .IP "\f(CWAutoClose => 0|1\fR" 5
242 .IX Item "AutoClose => 0|1"
243 This option applies to any input or output data streams to
244 \&\f(CW\*(C`gzip\*(C'\fR that are filehandles.
245 .Sp
246 If \f(CW\*(C`AutoClose\*(C'\fR is specified, and the value is true, it will result in all
247 input and/or output filehandles being closed once \f(CW\*(C`gzip\*(C'\fR has
248 completed.
249 .Sp
250 This parameter defaults to 0.
251 .ie n .IP """BinModeIn => 0|1""" 5
252 .el .IP "\f(CWBinModeIn => 0|1\fR" 5
253 .IX Item "BinModeIn => 0|1"
254 This option is now a no-op. All files will be read in binmode.
255 .ie n .IP """Append => 0|1""" 5
256 .el .IP "\f(CWAppend => 0|1\fR" 5
257 .IX Item "Append => 0|1"
258 The behaviour of this option is dependent on the type of output data
259 stream.
260 .RS 5
261 .IP \(bu 5
262 A Buffer
263 .Sp
264 If \f(CW\*(C`Append\*(C'\fR is enabled, all compressed data will be append to the end of
265 the output buffer. Otherwise the output buffer will be cleared before any
266 compressed data is written to it.
267 .IP \(bu 5
268 A Filename
269 .Sp
270 If \f(CW\*(C`Append\*(C'\fR is enabled, the file will be opened in append mode. Otherwise
271 the contents of the file, if any, will be truncated before any compressed
272 data is written to it.
273 .IP \(bu 5
274 A Filehandle
275 .Sp
276 If \f(CW\*(C`Append\*(C'\fR is enabled, the filehandle will be positioned to the end of
277 the file via a call to \f(CW\*(C`seek\*(C'\fR before any compressed data is
278 written to it. Otherwise the file pointer will not be moved.
279 .RE
280 .RS 5
281 .Sp
282 When \f(CW\*(C`Append\*(C'\fR is specified, and set to true, it will \fIappend\fR all compressed
283 data to the output data stream.
284 .Sp
285 So when the output is a filehandle it will carry out a seek to the eof
286 before writing any compressed data. If the output is a filename, it will be opened for
287 appending. If the output is a buffer, all compressed data will be
288 appended to the existing buffer.
289 .Sp
290 Conversely when \f(CW\*(C`Append\*(C'\fR is not specified, or it is present and is set to
291 false, it will operate as follows.
292 .Sp
293 When the output is a filename, it will truncate the contents of the file
294 before writing any compressed data. If the output is a filehandle
295 its position will not be changed. If the output is a buffer, it will be
296 wiped before any compressed data is output.
297 .Sp
298 Defaults to 0.
299 .RE
300 .SS "Oneshot Examples"
301 .IX Subsection "Oneshot Examples"
302 Here are a few example that show the capabilities of the module.
303 .PP
304 \fIStreaming\fR
305 .IX Subsection "Streaming"
306 .PP
307 This very simple command line example demonstrates the streaming capabilities of the module.
308 The code reads data from STDIN, compresses it, and writes the compressed data to STDOUT.
309 .PP
310 .Vb 1
311 \& $ echo hello world | perl \-MIO::Compress::Gzip=gzip \-e \*(Aqgzip \e*STDIN => \e*STDOUT\*(Aq >output.gz
312 .Ve
313 .PP
314 The special filename "\-" can be used as a standin for both \f(CW\*(C`\e*STDIN\*(C'\fR and \f(CW\*(C`\e*STDOUT\*(C'\fR,
315 so the above can be rewritten as
316 .PP
317 .Vb 1
318 \& $ echo hello world | perl \-MIO::Compress::Gzip=gzip \-e \*(Aqgzip "\-" => "\-"\*(Aq >output.gz
319 .Ve
320 .PP
321 \fICompressing a file from the filesystem\fR
322 .IX Subsection "Compressing a file from the filesystem"
323 .PP
324 To read the contents of the file \f(CW\*(C`file1.txt\*(C'\fR and write the compressed
325 data to the file \f(CW\*(C`file1.txt.gz\*(C'\fR.
326 .PP
327 .Vb 3
328 \& use strict ;
329 \& use warnings ;
330 \& use IO::Compress::Gzip qw(gzip $GzipError) ;
331 \&
332 \& my $input = "file1.txt";
333 \& gzip $input => "$input.gz"
334 \& or die "gzip failed: $GzipError\en";
335 .Ve
336 .PP
337 \fIReading from a Filehandle and writing to an in-memory buffer\fR
338 .IX Subsection "Reading from a Filehandle and writing to an in-memory buffer"
339 .PP
340 To read from an existing Perl filehandle, \f(CW$input\fR, and write the
341 compressed data to a buffer, \f(CW$buffer\fR.
342 .PP
343 .Vb 4
344 \& use strict ;
345 \& use warnings ;
346 \& use IO::Compress::Gzip qw(gzip $GzipError) ;
347 \& use IO::File ;
348 \&
349 \& my $input = IO::File\->new( "<file1.txt" )
350 \& or die "Cannot open \*(Aqfile1.txt\*(Aq: $!\en" ;
351 \& my $buffer ;
352 \& gzip $input => \e$buffer
353 \& or die "gzip failed: $GzipError\en";
354 .Ve
355 .PP
356 \fICompressing multiple files\fR
357 .IX Subsection "Compressing multiple files"
358 .PP
359 To compress all files in the directory "/my/home" that match "*.txt"
360 and store the compressed data in the same directory
361 .PP
362 .Vb 3
363 \& use strict ;
364 \& use warnings ;
365 \& use IO::Compress::Gzip qw(gzip $GzipError) ;
366 \&
367 \& gzip \*(Aq</my/home/*.txt>\*(Aq => \*(Aq<*.gz>\*(Aq
368 \& or die "gzip failed: $GzipError\en";
369 .Ve
370 .PP
371 and if you want to compress each file one at a time, this will do the trick
372 .PP
373 .Vb 3
374 \& use strict ;
375 \& use warnings ;
376 \& use IO::Compress::Gzip qw(gzip $GzipError) ;
377 \&
378 \& for my $input ( glob "/my/home/*.txt" )
379 \& {
380 \& my $output = "$input.gz" ;
381 \& gzip $input => $output
382 \& or die "Error compressing \*(Aq$input\*(Aq: $GzipError\en";
383 \& }
384 .Ve
385 .SH "OO Interface"
386 .IX Header "OO Interface"
387 .SS Constructor
388 .IX Subsection "Constructor"
389 The format of the constructor for \f(CW\*(C`IO::Compress::Gzip\*(C'\fR is shown below
390 .PP
391 .Vb 2
392 \& my $z = IO::Compress::Gzip\->new( $output [,OPTS] )
393 \& or die "IO::Compress::Gzip failed: $GzipError\en";
394 .Ve
395 .PP
396 The constructor takes one mandatory parameter, \f(CW$output\fR, defined below and
397 zero or more \f(CW\*(C`OPTS\*(C'\fR, defined in "Constructor Options".
398 .PP
399 It returns an \f(CW\*(C`IO::Compress::Gzip\*(C'\fR object on success and \f(CW\*(C`undef\*(C'\fR on failure.
400 The variable \f(CW$GzipError\fR will contain an error message on failure.
401 .PP
402 If you are running Perl 5.005 or better the object, \f(CW$z\fR, returned from
403 IO::Compress::Gzip can be used exactly like an IO::File filehandle.
404 This means that all normal output file operations can be carried out
405 with \f(CW$z\fR.
406 For example, to write to a compressed file/buffer you can use either of
407 these forms
408 .PP
409 .Vb 2
410 \& $z\->print("hello world\en");
411 \& print $z "hello world\en";
412 .Ve
413 .PP
414 Below is a simple exaple of using the OO interface to create an output file
415 \&\f(CW\*(C`myfile.gz\*(C'\fR and write some data to it.
416 .PP
417 .Vb 3
418 \& my $filename = "myfile.gz";
419 \& my $z = IO::Compress::Gzip\->new($filename)
420 \& or die "IO::Compress::Gzip failed: $GzipError\en";
421 \&
422 \& $z\->print("abcde");
423 \& $z\->close();
424 .Ve
425 .PP
426 See the "Examples" for more.
427 .PP
428 The mandatory parameter \f(CW$output\fR is used to control the destination
429 of the compressed data. This parameter can take one of these forms.
430 .IP "A filename" 5
431 .IX Item "A filename"
432 If the \f(CW$output\fR parameter is a simple scalar, it is assumed to be a
433 filename. This file will be opened for writing and the compressed data
434 will be written to it.
435 .IP "A filehandle" 5
436 .IX Item "A filehandle"
437 If the \f(CW$output\fR parameter is a filehandle, the compressed data will be
438 written to it.
439 The string '\-' can be used as an alias for standard output.
440 .IP "A scalar reference" 5
441 .IX Item "A scalar reference"
442 If \f(CW$output\fR is a scalar reference, the compressed data will be stored
443 in \f(CW$$output\fR.
444 .PP
445 If the \f(CW$output\fR parameter is any other type, \f(CW\*(C`IO::Compress::Gzip\*(C'\fR::new will
446 return undef.
447 .SS "Constructor Options"
448 .IX Subsection "Constructor Options"
449 \&\f(CW\*(C`OPTS\*(C'\fR is any combination of zero or more the following options:
450 .ie n .IP """AutoClose => 0|1""" 5
451 .el .IP "\f(CWAutoClose => 0|1\fR" 5
452 .IX Item "AutoClose => 0|1"
453 This option is only valid when the \f(CW$output\fR parameter is a filehandle. If
454 specified, and the value is true, it will result in the \f(CW$output\fR being
455 closed once either the \f(CW\*(C`close\*(C'\fR method is called or the \f(CW\*(C`IO::Compress::Gzip\*(C'\fR
456 object is destroyed.
457 .Sp
458 This parameter defaults to 0.
459 .ie n .IP """Append => 0|1""" 5
460 .el .IP "\f(CWAppend => 0|1\fR" 5
461 .IX Item "Append => 0|1"
462 Opens \f(CW$output\fR in append mode.
463 .Sp
464 The behaviour of this option is dependent on the type of \f(CW$output\fR.
465 .RS 5
466 .IP \(bu 5
467 A Buffer
468 .Sp
469 If \f(CW$output\fR is a buffer and \f(CW\*(C`Append\*(C'\fR is enabled, all compressed data
470 will be append to the end of \f(CW$output\fR. Otherwise \f(CW$output\fR will be
471 cleared before any data is written to it.
472 .IP \(bu 5
473 A Filename
474 .Sp
475 If \f(CW$output\fR is a filename and \f(CW\*(C`Append\*(C'\fR is enabled, the file will be
476 opened in append mode. Otherwise the contents of the file, if any, will be
477 truncated before any compressed data is written to it.
478 .IP \(bu 5
479 A Filehandle
480 .Sp
481 If \f(CW$output\fR is a filehandle, the file pointer will be positioned to the
482 end of the file via a call to \f(CW\*(C`seek\*(C'\fR before any compressed data is written
483 to it. Otherwise the file pointer will not be moved.
484 .RE
485 .RS 5
486 .Sp
487 This parameter defaults to 0.
488 .RE
489 .ie n .IP """Merge => 0|1""" 5
490 .el .IP "\f(CWMerge => 0|1\fR" 5
491 .IX Item "Merge => 0|1"
492 This option is used to compress input data and append it to an existing
493 compressed data stream in \f(CW$output\fR. The end result is a single compressed
494 data stream stored in \f(CW$output\fR.
495 .Sp
496 It is a fatal error to attempt to use this option when \f(CW$output\fR is not an
497 RFC 1952 data stream.
498 .Sp
499 There are a number of other limitations with the \f(CW\*(C`Merge\*(C'\fR option:
500 .RS 5
501 .IP 1. 5
502 This module needs to have been built with zlib 1.2.1 or better to work. A
503 fatal error will be thrown if \f(CW\*(C`Merge\*(C'\fR is used with an older version of
504 zlib.
505 .IP 2. 5
506 If \f(CW$output\fR is a file or a filehandle, it must be seekable.
507 .RE
508 .RS 5
509 .Sp
510 This parameter defaults to 0.
511 .RE
512 .IP \-Level 5
513 .IX Item "-Level"
514 Defines the compression level used by zlib. The value should either be
515 a number between 0 and 9 (0 means no compression and 9 is maximum
516 compression), or one of the symbolic constants defined below.
517 .Sp
518 .Vb 4
519 \& Z_NO_COMPRESSION
520 \& Z_BEST_SPEED
521 \& Z_BEST_COMPRESSION
522 \& Z_DEFAULT_COMPRESSION
523 .Ve
524 .Sp
525 The default is Z_DEFAULT_COMPRESSION.
526 .Sp
527 Note, these constants are not imported by \f(CW\*(C`IO::Compress::Gzip\*(C'\fR by default.
528 .Sp
529 .Vb 3
530 \& use IO::Compress::Gzip qw(:strategy);
531 \& use IO::Compress::Gzip qw(:constants);
532 \& use IO::Compress::Gzip qw(:all);
533 .Ve
534 .IP \-Strategy 5
535 .IX Item "-Strategy"
536 Defines the strategy used to tune the compression. Use one of the symbolic
537 constants defined below.
538 .Sp
539 .Vb 5
540 \& Z_FILTERED
541 \& Z_HUFFMAN_ONLY
542 \& Z_RLE
543 \& Z_FIXED
544 \& Z_DEFAULT_STRATEGY
545 .Ve
546 .Sp
547 The default is Z_DEFAULT_STRATEGY.
548 .ie n .IP """Minimal => 0|1""" 5
549 .el .IP "\f(CWMinimal => 0|1\fR" 5
550 .IX Item "Minimal => 0|1"
551 If specified, this option will force the creation of the smallest possible
552 compliant gzip header (which is exactly 10 bytes long) as defined in
553 RFC 1952.
554 .Sp
555 See the section titled "Compliance" in RFC 1952 for a definition
556 of the values used for the fields in the gzip header.
557 .Sp
558 All other parameters that control the content of the gzip header will
559 be ignored if this parameter is set to 1.
560 .Sp
561 This parameter defaults to 0.
562 .ie n .IP """Comment => $comment""" 5
563 .el .IP "\f(CWComment => $comment\fR" 5
564 .IX Item "Comment => $comment"
565 Stores the contents of \f(CW$comment\fR in the COMMENT field in
566 the gzip header.
567 By default, no comment field is written to the gzip file.
568 .Sp
569 If the \f(CW\*(C`\-Strict\*(C'\fR option is enabled, the comment can only consist of ISO
570 8859\-1 characters plus line feed.
571 .Sp
572 If the \f(CW\*(C`\-Strict\*(C'\fR option is disabled, the comment field can contain any
573 character except NULL. If any null characters are present, the field
574 will be truncated at the first NULL.
575 .ie n .IP """Name => $string""" 5
576 .el .IP "\f(CWName => $string\fR" 5
577 .IX Item "Name => $string"
578 Stores the contents of \f(CW$string\fR in the gzip NAME header field. If
579 \&\f(CW\*(C`Name\*(C'\fR is not specified, no gzip NAME field will be created.
580 .Sp
581 If the \f(CW\*(C`\-Strict\*(C'\fR option is enabled, \f(CW$string\fR can only consist of ISO
582 8859\-1 characters.
583 .Sp
584 If \f(CW\*(C`\-Strict\*(C'\fR is disabled, then \f(CW$string\fR can contain any character
585 except NULL. If any null characters are present, the field will be
586 truncated at the first NULL.
587 .ie n .IP """Time => $number""" 5
588 .el .IP "\f(CWTime => $number\fR" 5
589 .IX Item "Time => $number"
590 Sets the MTIME field in the gzip header to \f(CW$number\fR.
591 .Sp
592 This field defaults to the time the \f(CW\*(C`IO::Compress::Gzip\*(C'\fR object was created
593 if this option is not specified.
594 .ie n .IP """TextFlag => 0|1""" 5
595 .el .IP "\f(CWTextFlag => 0|1\fR" 5
596 .IX Item "TextFlag => 0|1"
597 This parameter controls the setting of the FLG.FTEXT bit in the gzip
598 header. It is used to signal that the data stored in the gzip file/buffer
599 is probably text.
600 .Sp
601 The default is 0.
602 .ie n .IP """HeaderCRC => 0|1""" 5
603 .el .IP "\f(CWHeaderCRC => 0|1\fR" 5
604 .IX Item "HeaderCRC => 0|1"
605 When true this parameter will set the FLG.FHCRC bit to 1 in the gzip header
606 and set the CRC16 header field to the CRC of the complete gzip header
607 except the CRC16 field itself.
608 .Sp
609 \&\fBNote\fR that gzip files created with the \f(CW\*(C`HeaderCRC\*(C'\fR flag set to 1 cannot
610 be read by most, if not all, of the standard gunzip utilities, most
611 notably gzip version 1.2.4. You should therefore avoid using this option if
612 you want to maximize the portability of your gzip files.
613 .Sp
614 This parameter defaults to 0.
615 .ie n .IP """OS_Code => $value""" 5
616 .el .IP "\f(CWOS_Code => $value\fR" 5
617 .IX Item "OS_Code => $value"
618 Stores \f(CW$value\fR in the gzip OS header field. A number between 0 and 255 is
619 valid.
620 .Sp
621 If not specified, this parameter defaults to the OS code of the Operating
622 System this module was built on. The value 3 is used as a catch-all for all
623 Unix variants and unknown Operating Systems.
624 .ie n .IP """ExtraField => $data""" 5
625 .el .IP "\f(CWExtraField => $data\fR" 5
626 .IX Item "ExtraField => $data"
627 This parameter allows additional metadata to be stored in the ExtraField in
628 the gzip header. An RFC 1952 compliant ExtraField consists of zero or more
629 subfields. Each subfield consists of a two byte header followed by the
630 subfield data.
631 .Sp
632 The list of subfields can be supplied in any of the following formats
633 .Sp
634 .Vb 12
635 \& \-ExtraField => [$id1, $data1,
636 \& $id2, $data2,
637 \& ...
638 \& ]
639 \& \-ExtraField => [ [$id1 => $data1],
640 \& [$id2 => $data2],
641 \& ...
642 \& ]
643 \& \-ExtraField => { $id1 => $data1,
644 \& $id2 => $data2,
645 \& ...
646 \& }
647 .Ve
648 .Sp
649 Where \f(CW$id1\fR, \f(CW$id2\fR are two byte subfield ID's. The second byte of
650 the ID cannot be 0, unless the \f(CW\*(C`Strict\*(C'\fR option has been disabled.
651 .Sp
652 If you use the hash syntax, you have no control over the order in which
653 the ExtraSubFields are stored, plus you cannot have SubFields with
654 duplicate ID.
655 .Sp
656 Alternatively the list of subfields can by supplied as a scalar, thus
657 .Sp
658 .Vb 1
659 \& \-ExtraField => $rawdata
660 .Ve
661 .Sp
662 If you use the raw format, and the \f(CW\*(C`Strict\*(C'\fR option is enabled,
663 \&\f(CW\*(C`IO::Compress::Gzip\*(C'\fR will check that \f(CW$rawdata\fR consists of zero or more
664 conformant sub-fields. When \f(CW\*(C`Strict\*(C'\fR is disabled, \f(CW$rawdata\fR can
665 consist of any arbitrary byte stream.
666 .Sp
667 The maximum size of the Extra Field 65535 bytes.
668 .ie n .IP """ExtraFlags => $value""" 5
669 .el .IP "\f(CWExtraFlags => $value\fR" 5
670 .IX Item "ExtraFlags => $value"
671 Sets the XFL byte in the gzip header to \f(CW$value\fR.
672 .Sp
673 If this option is not present, the value stored in XFL field will be
674 determined by the setting of the \f(CW\*(C`Level\*(C'\fR option.
675 .Sp
676 If \f(CW\*(C`Level => Z_BEST_SPEED\*(C'\fR has been specified then XFL is set to 2.
677 If \f(CW\*(C`Level => Z_BEST_COMPRESSION\*(C'\fR has been specified then XFL is set to 4.
678 Otherwise XFL is set to 0.
679 .ie n .IP """Strict => 0|1""" 5
680 .el .IP "\f(CWStrict => 0|1\fR" 5
681 .IX Item "Strict => 0|1"
682 \&\f(CW\*(C`Strict\*(C'\fR will optionally police the values supplied with other options
683 to ensure they are compliant with RFC1952.
684 .Sp
685 This option is enabled by default.
686 .Sp
687 If \f(CW\*(C`Strict\*(C'\fR is enabled the following behaviour will be policed:
688 .RS 5
689 .IP \(bu 5
690 The value supplied with the \f(CW\*(C`Name\*(C'\fR option can only contain ISO 8859\-1
691 characters.
692 .IP \(bu 5
693 The value supplied with the \f(CW\*(C`Comment\*(C'\fR option can only contain ISO 8859\-1
694 characters plus line-feed.
695 .IP \(bu 5
696 The values supplied with the \f(CW\*(C`\-Name\*(C'\fR and \f(CW\*(C`\-Comment\*(C'\fR options cannot
697 contain multiple embedded nulls.
698 .IP \(bu 5
699 If an \f(CW\*(C`ExtraField\*(C'\fR option is specified and it is a simple scalar,
700 it must conform to the sub-field structure as defined in RFC 1952.
701 .IP \(bu 5
702 If an \f(CW\*(C`ExtraField\*(C'\fR option is specified the second byte of the ID will be
703 checked in each subfield to ensure that it does not contain the reserved
704 value 0x00.
705 .RE
706 .RS 5
707 .Sp
708 When \f(CW\*(C`Strict\*(C'\fR is disabled the following behaviour will be policed:
709 .IP \(bu 5
710 The value supplied with \f(CW\*(C`\-Name\*(C'\fR option can contain
711 any character except NULL.
712 .IP \(bu 5
713 The value supplied with \f(CW\*(C`\-Comment\*(C'\fR option can contain any character
714 except NULL.
715 .IP \(bu 5
716 The values supplied with the \f(CW\*(C`\-Name\*(C'\fR and \f(CW\*(C`\-Comment\*(C'\fR options can contain
717 multiple embedded nulls. The string written to the gzip header will
718 consist of the characters up to, but not including, the first embedded
719 NULL.
720 .IP \(bu 5
721 If an \f(CW\*(C`ExtraField\*(C'\fR option is specified and it is a simple scalar, the
722 structure will not be checked. The only error is if the length is too big.
723 .IP \(bu 5
724 The ID header in an \f(CW\*(C`ExtraField\*(C'\fR sub-field can consist of any two bytes.
725 .RE
726 .RS 5
727 .RE
728 .SS Examples
729 .IX Subsection "Examples"
730 \fIStreaming\fR
731 .IX Subsection "Streaming"
732 .PP
733 This very simple command line example demonstrates the streaming capabilities
734 of the module. The code reads data from STDIN or all the files given on the
735 commandline, compresses it, and writes the compressed data to STDOUT.
736 .PP
737 .Vb 3
738 \& use strict ;
739 \& use warnings ;
740 \& use IO::Compress::Gzip qw(gzip $GzipError) ;
741 \&
742 \& my $z = IO::Compress::Gzip\->new("\-", Stream => 1)
743 \& or die "IO::Compress::Gzip failed: $GzipError\en";
744 \&
745 \& while (<>) {
746 \& $z\->print("abcde");
747 \& }
748 \& $z\->close();
749 .Ve
750 .PP
751 Note the use of \f(CW"\-"\fR to means \f(CW\*(C`STDOUT\*(C'\fR. Alternatively you can use \f(CW\*(C`\e*STDOUT\*(C'\fR.
752 .PP
753 \fICompressing a file from the filesystem\fR
754 .IX Subsection "Compressing a file from the filesystem"
755 .PP
756 To read the contents of the file \f(CW\*(C`file1.txt\*(C'\fR and write the compressed
757 data to the file \f(CW\*(C`file1.txt.gz\*(C'\fR there are a few options
758 .PP
759 Start by creating the compression object and opening the input file
760 .PP
761 .Vb 3
762 \& use strict ;
763 \& use warnings ;
764 \& use IO::Compress::Gzip qw(gzip $GzipError) ;
765 \&
766 \& my $input = "file1.txt";
767 \& my $z = IO::Compress::Gzip\->new("file1.txt.gz")
768 \& or die "IO::Compress::Gzip failed: $GzipError\en";
769 \&
770 \& # open the input file
771 \& open my $fh, "<", "file1.txt"
772 \& or die "Cannot open file1.txt: $!\en";
773 \&
774 \& # loop through the input file & write to the compressed file
775 \& while (<$fh>) {
776 \& $z\->print($_);
777 \& }
778 \&
779 \& # not forgetting to close the compressed file
780 \& $z\->close();
781 .Ve
782 .SH Methods
783 .IX Header "Methods"
784 .SS print
785 .IX Subsection "print"
786 Usage is
787 .PP
788 .Vb 2
789 \& $z\->print($data)
790 \& print $z $data
791 .Ve
792 .PP
793 Compresses and outputs the contents of the \f(CW$data\fR parameter. This
794 has the same behaviour as the \f(CW\*(C`print\*(C'\fR built-in.
795 .PP
796 Returns true if successful.
797 .SS printf
798 .IX Subsection "printf"
799 Usage is
800 .PP
801 .Vb 2
802 \& $z\->printf($format, $data)
803 \& printf $z $format, $data
804 .Ve
805 .PP
806 Compresses and outputs the contents of the \f(CW$data\fR parameter.
807 .PP
808 Returns true if successful.
809 .SS syswrite
810 .IX Subsection "syswrite"
811 Usage is
812 .PP
813 .Vb 3
814 \& $z\->syswrite $data
815 \& $z\->syswrite $data, $length
816 \& $z\->syswrite $data, $length, $offset
817 .Ve
818 .PP
819 Compresses and outputs the contents of the \f(CW$data\fR parameter.
820 .PP
821 Returns the number of uncompressed bytes written, or \f(CW\*(C`undef\*(C'\fR if
822 unsuccessful.
823 .SS write
824 .IX Subsection "write"
825 Usage is
826 .PP
827 .Vb 3
828 \& $z\->write $data
829 \& $z\->write $data, $length
830 \& $z\->write $data, $length, $offset
831 .Ve
832 .PP
833 Compresses and outputs the contents of the \f(CW$data\fR parameter.
834 .PP
835 Returns the number of uncompressed bytes written, or \f(CW\*(C`undef\*(C'\fR if
836 unsuccessful.
837 .SS flush
838 .IX Subsection "flush"
839 Usage is
840 .PP
841 .Vb 2
842 \& $z\->flush;
843 \& $z\->flush($flush_type);
844 .Ve
845 .PP
846 Flushes any pending compressed data to the output file/buffer.
847 .PP
848 This method takes an optional parameter, \f(CW$flush_type\fR, that controls
849 how the flushing will be carried out. By default the \f(CW$flush_type\fR
850 used is \f(CW\*(C`Z_FINISH\*(C'\fR. Other valid values for \f(CW$flush_type\fR are
851 \&\f(CW\*(C`Z_NO_FLUSH\*(C'\fR, \f(CW\*(C`Z_SYNC_FLUSH\*(C'\fR, \f(CW\*(C`Z_FULL_FLUSH\*(C'\fR and \f(CW\*(C`Z_BLOCK\*(C'\fR. It is
852 strongly recommended that you only set the \f(CW\*(C`flush_type\*(C'\fR parameter if
853 you fully understand the implications of what it does \- overuse of \f(CW\*(C`flush\*(C'\fR
854 can seriously degrade the level of compression achieved. See the \f(CW\*(C`zlib\*(C'\fR
855 documentation for details.
856 .PP
857 Returns true on success.
858 .SS tell
859 .IX Subsection "tell"
860 Usage is
861 .PP
862 .Vb 2
863 \& $z\->tell()
864 \& tell $z
865 .Ve
866 .PP
867 Returns the uncompressed file offset.
868 .SS eof
869 .IX Subsection "eof"
870 Usage is
871 .PP
872 .Vb 2
873 \& $z\->eof();
874 \& eof($z);
875 .Ve
876 .PP
877 Returns true if the \f(CW\*(C`close\*(C'\fR method has been called.
878 .SS seek
879 .IX Subsection "seek"
880 .Vb 2
881 \& $z\->seek($position, $whence);
882 \& seek($z, $position, $whence);
883 .Ve
884 .PP
885 Provides a sub-set of the \f(CW\*(C`seek\*(C'\fR functionality, with the restriction
886 that it is only legal to seek forward in the output file/buffer.
887 It is a fatal error to attempt to seek backward.
888 .PP
889 Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
890 .PP
891 The \f(CW$whence\fR parameter takes one the usual values, namely SEEK_SET,
892 SEEK_CUR or SEEK_END.
893 .PP
894 Returns 1 on success, 0 on failure.
895 .SS binmode
896 .IX Subsection "binmode"
897 Usage is
898 .PP
899 .Vb 2
900 \& $z\->binmode
901 \& binmode $z ;
902 .Ve
903 .PP
904 This is a noop provided for completeness.
905 .SS opened
906 .IX Subsection "opened"
907 .Vb 1
908 \& $z\->opened()
909 .Ve
910 .PP
911 Returns true if the object currently refers to a opened file/buffer.
912 .SS autoflush
913 .IX Subsection "autoflush"
914 .Vb 2
915 \& my $prev = $z\->autoflush()
916 \& my $prev = $z\->autoflush(EXPR)
917 .Ve
918 .PP
919 If the \f(CW$z\fR object is associated with a file or a filehandle, this method
920 returns the current autoflush setting for the underlying filehandle. If
921 \&\f(CW\*(C`EXPR\*(C'\fR is present, and is non-zero, it will enable flushing after every
922 write/print operation.
923 .PP
924 If \f(CW$z\fR is associated with a buffer, this method has no effect and always
925 returns \f(CW\*(C`undef\*(C'\fR.
926 .PP
927 \&\fBNote\fR that the special variable \f(CW$|\fR \fBcannot\fR be used to set or
928 retrieve the autoflush setting.
929 .SS input_line_number
930 .IX Subsection "input_line_number"
931 .Vb 2
932 \& $z\->input_line_number()
933 \& $z\->input_line_number(EXPR)
934 .Ve
935 .PP
936 This method always returns \f(CW\*(C`undef\*(C'\fR when compressing.
937 .SS fileno
938 .IX Subsection "fileno"
939 .Vb 2
940 \& $z\->fileno()
941 \& fileno($z)
942 .Ve
943 .PP
944 If the \f(CW$z\fR object is associated with a file or a filehandle, \f(CW\*(C`fileno\*(C'\fR
945 will return the underlying file descriptor. Once the \f(CW\*(C`close\*(C'\fR method is
946 called \f(CW\*(C`fileno\*(C'\fR will return \f(CW\*(C`undef\*(C'\fR.
947 .PP
948 If the \f(CW$z\fR object is associated with a buffer, this method will return
949 \&\f(CW\*(C`undef\*(C'\fR.
950 .SS close
951 .IX Subsection "close"
952 .Vb 2
953 \& $z\->close() ;
954 \& close $z ;
955 .Ve
956 .PP
957 Flushes any pending compressed data and then closes the output file/buffer.
958 .PP
959 For most versions of Perl this method will be automatically invoked if
960 the IO::Compress::Gzip object is destroyed (either explicitly or by the
961 variable with the reference to the object going out of scope). The
962 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
963 these cases, the \f(CW\*(C`close\*(C'\fR method will be called automatically, but
964 not until global destruction of all live objects when the program is
965 terminating.
966 .PP
967 Therefore, if you want your scripts to be able to run on all versions
968 of Perl, you should call \f(CW\*(C`close\*(C'\fR explicitly and not rely on automatic
969 closing.
970 .PP
971 Returns true on success, otherwise 0.
972 .PP
973 If the \f(CW\*(C`AutoClose\*(C'\fR option has been enabled when the IO::Compress::Gzip
974 object was created, and the object is associated with a file, the
975 underlying file will also be closed.
976 .SS newStream([OPTS])
977 .IX Subsection "newStream([OPTS])"
978 Usage is
979 .PP
980 .Vb 1
981 \& $z\->newStream( [OPTS] )
982 .Ve
983 .PP
984 Closes the current compressed data stream and starts a new one.
985 .PP
986 OPTS consists of any of the options that are available when creating
987 the \f(CW$z\fR object.
988 .PP
989 See the "Constructor Options" section for more details.
990 .SS deflateParams
991 .IX Subsection "deflateParams"
992 Usage is
993 .PP
994 .Vb 1
995 \& $z\->deflateParams
996 .Ve
997 .PP
998 TODO
999 .SH Importing
1000 .IX Header "Importing"
1001 A number of symbolic constants are required by some methods in
1002 \&\f(CW\*(C`IO::Compress::Gzip\*(C'\fR. None are imported by default.
1003 .IP :all 5
1004 .IX Item ":all"
1005 Imports \f(CW\*(C`gzip\*(C'\fR, \f(CW$GzipError\fR and all symbolic
1006 constants that can be used by \f(CW\*(C`IO::Compress::Gzip\*(C'\fR. Same as doing this
1007 .Sp
1008 .Vb 1
1009 \& use IO::Compress::Gzip qw(gzip $GzipError :constants) ;
1010 .Ve
1011 .IP :constants 5
1012 .IX Item ":constants"
1013 Import all symbolic constants. Same as doing this
1014 .Sp
1015 .Vb 1
1016 \& use IO::Compress::Gzip qw(:flush :level :strategy) ;
1017 .Ve
1018 .IP :flush 5
1019 .IX Item ":flush"
1020 These symbolic constants are used by the \f(CW\*(C`flush\*(C'\fR method.
1021 .Sp
1022 .Vb 6
1023 \& Z_NO_FLUSH
1024 \& Z_PARTIAL_FLUSH
1025 \& Z_SYNC_FLUSH
1026 \& Z_FULL_FLUSH
1027 \& Z_FINISH
1028 \& Z_BLOCK
1029 .Ve
1030 .IP :level 5
1031 .IX Item ":level"
1032 These symbolic constants are used by the \f(CW\*(C`Level\*(C'\fR option in the constructor.
1033 .Sp
1034 .Vb 4
1035 \& Z_NO_COMPRESSION
1036 \& Z_BEST_SPEED
1037 \& Z_BEST_COMPRESSION
1038 \& Z_DEFAULT_COMPRESSION
1039 .Ve
1040 .IP :strategy 5
1041 .IX Item ":strategy"
1042 These symbolic constants are used by the \f(CW\*(C`Strategy\*(C'\fR option in the constructor.
1043 .Sp
1044 .Vb 5
1045 \& Z_FILTERED
1046 \& Z_HUFFMAN_ONLY
1047 \& Z_RLE
1048 \& Z_FIXED
1049 \& Z_DEFAULT_STRATEGY
1050 .Ve
1051 .SH EXAMPLES
1052 .IX Header "EXAMPLES"
1053 .SS "Apache::GZip Revisited"
1054 .IX Subsection "Apache::GZip Revisited"
1055 See IO::Compress::FAQ
1056 .SS "Working with Net::FTP"
1057 .IX Subsection "Working with Net::FTP"
1058 See IO::Compress::FAQ
1059 .SH SUPPORT
1060 .IX Header "SUPPORT"
1061 General feedback/questions/bug reports should be sent to
1062 <https://github.com/pmqs/IO\-Copress/issues> (preferred) or
1063 <https://rt.cpan.org/Public/Dist/Display.html?Name=IO\-Copress>.
1064 .SH "SEE ALSO"
1065 .IX Header "SEE ALSO"
1066 Compress::Zlib, IO::Uncompress::Gunzip, IO::Compress::Deflate, IO::Uncompress::Inflate, IO::Compress::RawDeflate, IO::Uncompress::RawInflate, IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip, IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd, IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate, IO::Uncompress::AnyUncompress
1067 .PP
1068 IO::Compress::FAQ
1069 .PP
1070 File::GlobMapper, Archive::Zip,
1071 Archive::Tar,
1072 IO::Zlib
1073 .PP
1074 For RFC 1950, 1951 and 1952 see
1075 <https://datatracker.ietf.org/doc/html/rfc1950>,
1076 <https://datatracker.ietf.org/doc/html/rfc1951> and
1077 <https://datatracker.ietf.org/doc/html/rfc1952>
1078 .PP
1079 The \fIzlib\fR compression library was written by Jean-loup Gailly
1080 \&\f(CW\*(C`[email protected]\*(C'\fR and Mark Adler \f(CW\*(C`[email protected]\*(C'\fR.
1081 .PP
1082 The primary site for the \fIzlib\fR compression library is
1083 <http://www.zlib.org>.
1084 .PP
1085 The primary site for the \fIzlib-ng\fR compression library is
1086 <https://github.com/zlib\-ng/zlib\-ng>.
1087 .PP
1088 The primary site for gzip is <http://www.gzip.org>.
1089 .SH AUTHOR
1090 .IX Header "AUTHOR"
1091 This module was written by Paul Marquess, \f(CW\*(C`[email protected]\*(C'\fR.
1092 .SH "MODIFICATION HISTORY"
1093 .IX Header "MODIFICATION HISTORY"
1094 See the Changes file.
1095 .SH "COPYRIGHT AND LICENSE"
1096 .IX Header "COPYRIGHT AND LICENSE"
1097 Copyright (c) 2005\-2024 Paul Marquess. All rights reserved.
1098 .PP
1099 This program is free software; you can redistribute it and/or
1100 modify it under the same terms as Perl itself.