Debian Codegraph @

upstream/opensuse-tumbleweed/man3/IO::Compress::Gzip.3pm

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