1IO::Uncompress::UnZstd(U3s)er Contributed Perl DocumentatIiOo:n:Uncompress::UnZstd(3)
2
3
4
6 IO::Uncompress::UnZstd - Read zstd files/buffers
7
9 use IO::Uncompress::UnZstd qw(unzstd $UnZstdError) ;
10
11 my $status = unzstd $input => $output [,OPTS]
12 or die "unzstd failed: $UnZstdError\n";
13
14 my $z = IO::Uncompress::UnZstd->new( $input [OPTS] )
15 or die "unzstd failed: $UnZstdError\n";
16
17 $status = $z->read($buffer)
18 $status = $z->read($buffer, $length)
19 $status = $z->read($buffer, $length, $offset)
20 $line = $z->getline()
21 $char = $z->getc()
22 $char = $z->ungetc()
23 $char = $z->opened()
24
25 $data = $z->trailingData()
26 $status = $z->nextStream()
27 $data = $z->getHeaderInfo()
28 $z->tell()
29 $z->seek($position, $whence)
30 $z->binmode()
31 $z->fileno()
32 $z->eof()
33 $z->close()
34
35 $UnZstdError ;
36
37 # IO::File mode
38
39 <$z>
40 read($z, $buffer);
41 read($z, $buffer, $length);
42 read($z, $buffer, $length, $offset);
43 tell($z)
44 seek($z, $position, $whence)
45 binmode($z)
46 fileno($z)
47 eof($z)
48 close($z)
49
51 This module provides a Perl interface that allows the reading of zstd
52 files/buffers.
53
54 For writing zstd files/buffers, see the companion module
55 IO::Compress::Zstd.
56
58 A top-level function, "unzstd", is provided to carry out "one-shot"
59 uncompression between buffers and/or files. For finer control over the
60 uncompression process, see the "OO Interface" section.
61
62 use IO::Uncompress::UnZstd qw(unzstd $UnZstdError) ;
63
64 unzstd $input_filename_or_reference => $output_filename_or_reference [,OPTS]
65 or die "unzstd failed: $UnZstdError\n";
66
67 The functional interface needs Perl5.005 or better.
68
69 unzstd $input_filename_or_reference => $output_filename_or_reference [,
70 OPTS]
71 "unzstd" expects at least two parameters, $input_filename_or_reference
72 and $output_filename_or_reference and zero or more optional parameters
73 (see "Optional Parameters")
74
75 The $input_filename_or_reference parameter
76
77 The parameter, $input_filename_or_reference, is used to define the
78 source of the compressed data.
79
80 It can take one of the following forms:
81
82 A filename
83 If the $input_filename_or_reference parameter is a simple scalar,
84 it is assumed to be a filename. This file will be opened for
85 reading and the input data will be read from it.
86
87 A filehandle
88 If the $input_filename_or_reference parameter is a filehandle, the
89 input data will be read from it. The string '-' can be used as an
90 alias for standard input.
91
92 A scalar reference
93 If $input_filename_or_reference is a scalar reference, the input
94 data will be read from $$input_filename_or_reference.
95
96 An array reference
97 If $input_filename_or_reference is an array reference, each
98 element in the array must be a filename.
99
100 The input data will be read from each file in turn.
101
102 The complete array will be walked to ensure that it only contains
103 valid filenames before any data is uncompressed.
104
105 An Input FileGlob string
106 If $input_filename_or_reference is a string that is delimited by
107 the characters "<" and ">" "unzstd" will assume that it is an
108 input fileglob string. The input is the list of files that match
109 the fileglob.
110
111 See File::GlobMapper for more details.
112
113 If the $input_filename_or_reference parameter is any other type,
114 "undef" will be returned.
115
116 The $output_filename_or_reference parameter
117
118 The parameter $output_filename_or_reference is used to control the
119 destination of the uncompressed data. This parameter can take one of
120 these forms.
121
122 A filename
123 If the $output_filename_or_reference parameter is a simple scalar,
124 it is assumed to be a filename. This file will be opened for
125 writing and the uncompressed data will be written to it.
126
127 A filehandle
128 If the $output_filename_or_reference parameter is a filehandle,
129 the uncompressed data will be written to it. The string '-' can
130 be used as an alias for standard output.
131
132 A scalar reference
133 If $output_filename_or_reference is a scalar reference, the
134 uncompressed data will be stored in
135 $$output_filename_or_reference.
136
137 An Array Reference
138 If $output_filename_or_reference is an array reference, the
139 uncompressed data will be pushed onto the array.
140
141 An Output FileGlob
142 If $output_filename_or_reference is a string that is delimited by
143 the characters "<" and ">" "unzstd" will assume that it is an
144 output fileglob string. The output is the list of files that match
145 the fileglob.
146
147 When $output_filename_or_reference is an fileglob string,
148 $input_filename_or_reference must also be a fileglob string.
149 Anything else is an error.
150
151 See File::GlobMapper for more details.
152
153 If the $output_filename_or_reference parameter is any other type,
154 "undef" will be returned.
155
156 Notes
157 When $input_filename_or_reference maps to multiple compressed
158 files/buffers and $output_filename_or_reference is a single
159 file/buffer, after uncompression $output_filename_or_reference will
160 contain a concatenation of all the uncompressed data from each of the
161 input files/buffers.
162
163 Optional Parameters
164 The optional parameters for the one-shot function "unzstd" are (for the
165 most part) identical to those used with the OO interface defined in the
166 "Constructor Options" section. The exceptions are listed below
167
168 "AutoClose => 0|1"
169 This option applies to any input or output data streams to
170 "unzstd" that are filehandles.
171
172 If "AutoClose" is specified, and the value is true, it will result
173 in all input and/or output filehandles being closed once "unzstd"
174 has completed.
175
176 This parameter defaults to 0.
177
178 "BinModeOut => 0|1"
179 This option is now a no-op. All files will be written in binmode.
180
181 "Append => 0|1"
182 The behaviour of this option is dependent on the type of output
183 data stream.
184
185 • A Buffer
186
187 If "Append" is enabled, all uncompressed data will be append
188 to the end of the output buffer. Otherwise the output buffer
189 will be cleared before any uncompressed data is written to
190 it.
191
192 • A Filename
193
194 If "Append" is enabled, the file will be opened in append
195 mode. Otherwise the contents of the file, if any, will be
196 truncated before any uncompressed data is written to it.
197
198 • A Filehandle
199
200 If "Append" is enabled, the filehandle will be positioned to
201 the end of the file via a call to "seek" before any
202 uncompressed data is written to it. Otherwise the file
203 pointer will not be moved.
204
205 When "Append" is specified, and set to true, it will append all
206 uncompressed data to the output data stream.
207
208 So when the output is a filehandle it will carry out a seek to the
209 eof before writing any uncompressed data. If the output is a
210 filename, it will be opened for appending. If the output is a
211 buffer, all uncompressed data will be appended to the existing
212 buffer.
213
214 Conversely when "Append" is not specified, or it is present and is
215 set to false, it will operate as follows.
216
217 When the output is a filename, it will truncate the contents of
218 the file before writing any uncompressed data. If the output is a
219 filehandle its position will not be changed. If the output is a
220 buffer, it will be wiped before any uncompressed data is output.
221
222 Defaults to 0.
223
224 "MultiStream => 0|1"
225 This option is a no-op.
226
227 "TrailingData => $scalar"
228 This option is a no-op.
229
230 If you know the length of the compressed data stream before you
231 start uncompressing, you can avoid having to use "trailingData" by
232 setting the "InputLength" option.
233
234 Examples
235 To read the contents of the file "file1.txt.zst" and write the
236 uncompressed data to the file "file1.txt".
237
238 use strict ;
239 use warnings ;
240 use IO::Uncompress::UnZstd qw(unzstd $UnZstdError) ;
241
242 my $input = "file1.txt.zst";
243 my $output = "file1.txt";
244 unzstd $input => $output
245 or die "unzstd failed: $UnZstdError\n";
246
247 To read from an existing Perl filehandle, $input, and write the
248 uncompressed data to a buffer, $buffer.
249
250 use strict ;
251 use warnings ;
252 use IO::Uncompress::UnZstd qw(unzstd $UnZstdError) ;
253 use IO::File ;
254
255 my $input = IO::File->new( "<file1.txt.zst" )
256 or die "Cannot open 'file1.txt.zst': $!\n" ;
257 my $buffer ;
258 unzstd $input => \$buffer
259 or die "unzstd failed: $UnZstdError\n";
260
261 To uncompress all files in the directory "/my/home" that match
262 "*.txt.zst" and store the compressed data in the same directory
263
264 use strict ;
265 use warnings ;
266 use IO::Uncompress::UnZstd qw(unzstd $UnZstdError) ;
267
268 unzstd '</my/home/*.txt.zst>' => '</my/home/#1.txt>'
269 or die "unzstd failed: $UnZstdError\n";
270
271 and if you want to compress each file one at a time, this will do the
272 trick
273
274 use strict ;
275 use warnings ;
276 use IO::Uncompress::UnZstd qw(unzstd $UnZstdError) ;
277
278 for my $input ( glob "/my/home/*.txt.zst" )
279 {
280 my $output = $input;
281 $output =~ s/.zst// ;
282 unzstd $input => $output
283 or die "Error compressing '$input': $UnZstdError\n";
284 }
285
287 Constructor
288 The format of the constructor for IO::Uncompress::UnZstd is shown below
289
290 my $z = IO::Uncompress::UnZstd->new( $input [OPTS] )
291 or die "IO::Uncompress::UnZstd failed: $UnZstdError\n";
292
293 Returns an "IO::Uncompress::UnZstd" object on success and undef on
294 failure. The variable $UnZstdError will contain an error message on
295 failure.
296
297 If you are running Perl 5.005 or better the object, $z, returned from
298 IO::Uncompress::UnZstd can be used exactly like an IO::File filehandle.
299 This means that all normal input file operations can be carried out
300 with $z. For example, to read a line from a compressed file/buffer you
301 can use either of these forms
302
303 $line = $z->getline();
304 $line = <$z>;
305
306 The mandatory parameter $input is used to determine the source of the
307 compressed data. This parameter can take one of three forms.
308
309 A filename
310 If the $input parameter is a scalar, it is assumed to be a
311 filename. This file will be opened for reading and the compressed
312 data will be read from it.
313
314 A filehandle
315 If the $input parameter is a filehandle, the compressed data will
316 be read from it. The string '-' can be used as an alias for
317 standard input.
318
319 A scalar reference
320 If $input is a scalar reference, the compressed data will be read
321 from $$input.
322
323 Constructor Options
324 The option names defined below are case insensitive and can be
325 optionally prefixed by a '-'. So all of the following are valid
326
327 -AutoClose
328 -autoclose
329 AUTOCLOSE
330 autoclose
331
332 OPTS is a combination of the following options:
333
334 "AutoClose => 0|1"
335 This option is only valid when the $input parameter is a
336 filehandle. If specified, and the value is true, it will result in
337 the file being closed once either the "close" method is called or
338 the IO::Uncompress::UnZstd object is destroyed.
339
340 This parameter defaults to 0.
341
342 "MultiStream => 0|1"
343 Allows multiple concatenated compressed streams to be treated as a
344 single compressed stream. Decompression will stop once either the
345 end of the file/buffer is reached, an error is encountered
346 (premature eof, corrupt compressed data) or the end of a stream is
347 not immediately followed by the start of another stream.
348
349 This parameter defaults to 0.
350
351 "Prime => $string"
352 This option will uncompress the contents of $string before
353 processing the input file/buffer.
354
355 This option can be useful when the compressed data is embedded in
356 another file/data structure and it is not possible to work out
357 where the compressed data begins without having to read the first
358 few bytes. If this is the case, the uncompression can be primed
359 with these bytes using this option.
360
361 "Transparent => 0|1"
362 If this option is set and the input file/buffer is not compressed
363 data, the module will allow reading of it anyway.
364
365 In addition, if the input file/buffer does contain compressed data
366 and there is non-compressed data immediately following it, setting
367 this option will make this module treat the whole file/buffer as a
368 single data stream.
369
370 This option defaults to 1.
371
372 "BlockSize => $num"
373 When reading the compressed input data, IO::Uncompress::UnZstd
374 will read it in blocks of $num bytes.
375
376 This option defaults to 4096.
377
378 "InputLength => $size"
379 When present this option will limit the number of compressed bytes
380 read from the input file/buffer to $size. This option can be used
381 in the situation where there is useful data directly after the
382 compressed data stream and you know beforehand the exact length of
383 the compressed data stream.
384
385 This option is mostly used when reading from a filehandle, in
386 which case the file pointer will be left pointing to the first
387 byte directly after the compressed data stream.
388
389 This option defaults to off.
390
391 "Append => 0|1"
392 This option controls what the "read" method does with uncompressed
393 data.
394
395 If set to 1, all uncompressed data will be appended to the output
396 parameter of the "read" method.
397
398 If set to 0, the contents of the output parameter of the "read"
399 method will be overwritten by the uncompressed data.
400
401 Defaults to 0.
402
403 "Strict => 0|1"
404 This option controls whether the extra checks defined below are
405 used when carrying out the decompression. When Strict is on, the
406 extra tests are carried out, when Strict is off they are not.
407
408 The default for this option is off.
409
410 Examples
411 TODO
412
414 read
415 Usage is
416
417 $status = $z->read($buffer)
418
419 Reads a block of compressed data (the size of the compressed block is
420 determined by the "Buffer" option in the constructor), uncompresses it
421 and writes any uncompressed data into $buffer. If the "Append"
422 parameter is set in the constructor, the uncompressed data will be
423 appended to the $buffer parameter. Otherwise $buffer will be
424 overwritten.
425
426 Returns the number of uncompressed bytes written to $buffer, zero if
427 eof or a negative number on error.
428
429 read
430 Usage is
431
432 $status = $z->read($buffer, $length)
433 $status = $z->read($buffer, $length, $offset)
434
435 $status = read($z, $buffer, $length)
436 $status = read($z, $buffer, $length, $offset)
437
438 Attempt to read $length bytes of uncompressed data into $buffer.
439
440 The main difference between this form of the "read" method and the
441 previous one, is that this one will attempt to return exactly $length
442 bytes. The only circumstances that this function will not is if end-of-
443 file or an IO error is encountered.
444
445 Returns the number of uncompressed bytes written to $buffer, zero if
446 eof or a negative number on error.
447
448 getline
449 Usage is
450
451 $line = $z->getline()
452 $line = <$z>
453
454 Reads a single line.
455
456 This method fully supports the use of the variable $/ (or
457 $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
458 what constitutes an end of line. Paragraph mode, record mode and file
459 slurp mode are all supported.
460
461 getc
462 Usage is
463
464 $char = $z->getc()
465
466 Read a single character.
467
468 ungetc
469 Usage is
470
471 $char = $z->ungetc($string)
472
473 getHeaderInfo
474 Usage is
475
476 $hdr = $z->getHeaderInfo();
477 @hdrs = $z->getHeaderInfo();
478
479 This method returns either a hash reference (in scalar context) or a
480 list or hash references (in array context) that contains information
481 about each of the header fields in the compressed data stream(s).
482
483 tell
484 Usage is
485
486 $z->tell()
487 tell $z
488
489 Returns the uncompressed file offset.
490
491 eof
492 Usage is
493
494 $z->eof();
495 eof($z);
496
497 Returns true if the end of the compressed input stream has been
498 reached.
499
500 seek
501 $z->seek($position, $whence);
502 seek($z, $position, $whence);
503
504 Provides a sub-set of the "seek" functionality, with the restriction
505 that it is only legal to seek forward in the input file/buffer. It is
506 a fatal error to attempt to seek backward.
507
508 Note that the implementation of "seek" in this module does not provide
509 true random access to a compressed file/buffer. It works by
510 uncompressing data from the current offset in the file/buffer until it
511 reaches the uncompressed offset specified in the parameters to "seek".
512 For very small files this may be acceptable behaviour. For large files
513 it may cause an unacceptable delay.
514
515 The $whence parameter takes one the usual values, namely SEEK_SET,
516 SEEK_CUR or SEEK_END.
517
518 Returns 1 on success, 0 on failure.
519
520 binmode
521 Usage is
522
523 $z->binmode
524 binmode $z ;
525
526 This is a noop provided for completeness.
527
528 opened
529 $z->opened()
530
531 Returns true if the object currently refers to a opened file/buffer.
532
533 autoflush
534 my $prev = $z->autoflush()
535 my $prev = $z->autoflush(EXPR)
536
537 If the $z object is associated with a file or a filehandle, this method
538 returns the current autoflush setting for the underlying filehandle. If
539 "EXPR" is present, and is non-zero, it will enable flushing after every
540 write/print operation.
541
542 If $z is associated with a buffer, this method has no effect and always
543 returns "undef".
544
545 Note that the special variable $| cannot be used to set or retrieve the
546 autoflush setting.
547
548 input_line_number
549 $z->input_line_number()
550 $z->input_line_number(EXPR)
551
552 Returns the current uncompressed line number. If "EXPR" is present it
553 has the effect of setting the line number. Note that setting the line
554 number does not change the current position within the file/buffer
555 being read.
556
557 The contents of $/ are used to determine what constitutes a line
558 terminator.
559
560 fileno
561 $z->fileno()
562 fileno($z)
563
564 If the $z object is associated with a file or a filehandle, "fileno"
565 will return the underlying file descriptor. Once the "close" method is
566 called "fileno" will return "undef".
567
568 If the $z object is associated with a buffer, this method will return
569 "undef".
570
571 close
572 $z->close() ;
573 close $z ;
574
575 Closes the output file/buffer.
576
577 For most versions of Perl this method will be automatically invoked if
578 the IO::Uncompress::UnZstd object is destroyed (either explicitly or by
579 the variable with the reference to the object going out of scope). The
580 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
581 cases, the "close" method will be called automatically, but not until
582 global destruction of all live objects when the program is terminating.
583
584 Therefore, if you want your scripts to be able to run on all versions
585 of Perl, you should call "close" explicitly and not rely on automatic
586 closing.
587
588 Returns true on success, otherwise 0.
589
590 If the "AutoClose" option has been enabled when the
591 IO::Uncompress::UnZstd object was created, and the object is associated
592 with a file, the underlying file will also be closed.
593
594 nextStream
595 Usage is
596
597 my $status = $z->nextStream();
598
599 Skips to the next compressed data stream in the input file/buffer. If a
600 new compressed data stream is found, the eof marker will be cleared and
601 $. will be reset to 0.
602
603 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
604 error was encountered.
605
606 trailingData
607 Usage is
608
609 my $data = $z->trailingData();
610
611 Returns the data, if any, that is present immediately after the
612 compressed data stream once uncompression is complete. It only makes
613 sense to call this method once the end of the compressed data stream
614 has been encountered.
615
616 This option can be used when there is useful information immediately
617 following the compressed data stream, and you don't know the length of
618 the compressed data stream.
619
620 If the input is a buffer, "trailingData" will return everything from
621 the end of the compressed data stream to the end of the buffer.
622
623 If the input is a filehandle, "trailingData" will return the data that
624 is left in the filehandle input buffer once the end of the compressed
625 data stream has been reached. You can then use the filehandle to read
626 the rest of the input file.
627
628 Don't bother using "trailingData" if the input is a filename.
629
630 If you know the length of the compressed data stream before you start
631 uncompressing, you can avoid having to use "trailingData" by setting
632 the "InputLength" option in the constructor.
633
635 No symbolic constants are required by IO::Uncompress::UnZstd at
636 present.
637
638 :all Imports "unzstd" and $UnZstdError. Same as doing this
639
640 use IO::Uncompress::UnZstd qw(unzstd $UnZstdError) ;
641
644 General feedback/questions/bug reports should be sent to
645 <https://github.com/pmqs/IO-Compress-Zstd/issues> (preferred) or
646 <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress-Zstd>.
647
649 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
650 IO::Compress::Deflate, IO::Uncompress::Inflate,
651 IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
652 IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
653 IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz,
654 IO::Compress::Lzip, IO::Uncompress::UnLzip, IO::Compress::Lzop,
655 IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf,
656 IO::Compress::Zstd, IO::Uncompress::AnyInflate,
657 IO::Uncompress::AnyUncompress
658
659 IO::Compress::FAQ
660
661 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
662
664 This module was written by Paul Marquess, "pmqs@cpan.org".
665
667 See the Changes file.
668
670 Copyright (c) 2005-2022 Paul Marquess. All rights reserved.
671
672 This program is free software; you can redistribute it and/or modify it
673 under the same terms as Perl itself.
674
675
676
677perl v5.36.0 2023-01-20 IO::Uncompress::UnZstd(3)