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 OneShot 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 The constructor takes one mandatory parameter, $input, defined below,
294 and zero or more "OPTS", defined in "Constructor Options".
295
296 Returns an "IO::Uncompress::UnZstd" object on success and undef on
297 failure. The variable $UnZstdError will contain an error message on
298 failure.
299
300 If you are running Perl 5.005 or better the object, $z, returned from
301 IO::Uncompress::UnZstd can be used exactly like an IO::File filehandle.
302 This means that all normal input file operations can be carried out
303 with $z. For example, to read a line from a compressed file/buffer you
304 can use either of these forms
305
306 $line = $z->getline();
307 $line = <$z>;
308
309 Below is a simple exaple of using the OO interface to read the
310 compressed file "myfile.zst" and write its contents to stdout.
311
312 my $filename = "myfile.zst";
313 my $z = IO::Uncompress::UnZstd->new($filename)
314 or die "IO::Uncompress::UnZstd failed: $UnZstdError\n";
315
316 while (<$z>) {
317 print $_;
318 }
319 $z->close();
320
321 See "EXAMPLES" for further examples
322
323 The mandatory parameter $input is used to determine the source of the
324 compressed data. This parameter can take one of three forms.
325
326 A filename
327 If the $input parameter is a scalar, it is assumed to be a
328 filename. This file will be opened for reading and the compressed
329 data will be read from it.
330
331 A filehandle
332 If the $input parameter is a filehandle, the compressed data will
333 be read from it. The string '-' can be used as an alias for
334 standard input.
335
336 A scalar reference
337 If $input is a scalar reference, the compressed data will be read
338 from $$input.
339
340 Constructor Options
341 The option names defined below are case insensitive and can be
342 optionally prefixed by a '-'. So all of the following are valid
343
344 -AutoClose
345 -autoclose
346 AUTOCLOSE
347 autoclose
348
349 OPTS is a combination of the following options:
350
351 "AutoClose => 0|1"
352 This option is only valid when the $input parameter is a
353 filehandle. If specified, and the value is true, it will result in
354 the file being closed once either the "close" method is called or
355 the IO::Uncompress::UnZstd object is destroyed.
356
357 This parameter defaults to 0.
358
359 "MultiStream => 0|1"
360 Allows multiple concatenated compressed streams to be treated as a
361 single compressed stream. Decompression will stop once either the
362 end of the file/buffer is reached, an error is encountered
363 (premature eof, corrupt compressed data) or the end of a stream is
364 not immediately followed by the start of another stream.
365
366 This parameter defaults to 0.
367
368 "Prime => $string"
369 This option will uncompress the contents of $string before
370 processing the input file/buffer.
371
372 This option can be useful when the compressed data is embedded in
373 another file/data structure and it is not possible to work out
374 where the compressed data begins without having to read the first
375 few bytes. If this is the case, the uncompression can be primed
376 with these bytes using this option.
377
378 "Transparent => 0|1"
379 If this option is set and the input file/buffer is not compressed
380 data, the module will allow reading of it anyway.
381
382 In addition, if the input file/buffer does contain compressed data
383 and there is non-compressed data immediately following it, setting
384 this option will make this module treat the whole file/buffer as a
385 single data stream.
386
387 This option defaults to 1.
388
389 "BlockSize => $num"
390 When reading the compressed input data, IO::Uncompress::UnZstd
391 will read it in blocks of $num bytes.
392
393 This option defaults to 4096.
394
395 "InputLength => $size"
396 When present this option will limit the number of compressed bytes
397 read from the input file/buffer to $size. This option can be used
398 in the situation where there is useful data directly after the
399 compressed data stream and you know beforehand the exact length of
400 the compressed data stream.
401
402 This option is mostly used when reading from a filehandle, in
403 which case the file pointer will be left pointing to the first
404 byte directly after the compressed data stream.
405
406 This option defaults to off.
407
408 "Append => 0|1"
409 This option controls what the "read" method does with uncompressed
410 data.
411
412 If set to 1, all uncompressed data will be appended to the output
413 parameter of the "read" method.
414
415 If set to 0, the contents of the output parameter of the "read"
416 method will be overwritten by the uncompressed data.
417
418 Defaults to 0.
419
420 "Strict => 0|1"
421 This option controls whether the extra checks defined below are
422 used when carrying out the decompression. When Strict is on, the
423 extra tests are carried out, when Strict is off they are not.
424
425 The default for this option is off.
426
428 read
429 Usage is
430
431 $status = $z->read($buffer)
432
433 Reads a block of compressed data (the size of the compressed block is
434 determined by the "Buffer" option in the constructor), uncompresses it
435 and writes any uncompressed data into $buffer. If the "Append"
436 parameter is set in the constructor, the uncompressed data will be
437 appended to the $buffer parameter. Otherwise $buffer will be
438 overwritten.
439
440 Returns the number of uncompressed bytes written to $buffer, zero if
441 eof or a negative number on error.
442
443 read
444 Usage is
445
446 $status = $z->read($buffer, $length)
447 $status = $z->read($buffer, $length, $offset)
448
449 $status = read($z, $buffer, $length)
450 $status = read($z, $buffer, $length, $offset)
451
452 Attempt to read $length bytes of uncompressed data into $buffer.
453
454 The main difference between this form of the "read" method and the
455 previous one, is that this one will attempt to return exactly $length
456 bytes. The only circumstances that this function will not is if end-of-
457 file or an IO error is encountered.
458
459 Returns the number of uncompressed bytes written to $buffer, zero if
460 eof or a negative number on error.
461
462 getline
463 Usage is
464
465 $line = $z->getline()
466 $line = <$z>
467
468 Reads a single line.
469
470 This method fully supports the use of the variable $/ (or
471 $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
472 what constitutes an end of line. Paragraph mode, record mode and file
473 slurp mode are all supported.
474
475 getc
476 Usage is
477
478 $char = $z->getc()
479
480 Read a single character.
481
482 ungetc
483 Usage is
484
485 $char = $z->ungetc($string)
486
487 getHeaderInfo
488 Usage is
489
490 $hdr = $z->getHeaderInfo();
491 @hdrs = $z->getHeaderInfo();
492
493 This method returns either a hash reference (in scalar context) or a
494 list or hash references (in array context) that contains information
495 about each of the header fields in the compressed data stream(s).
496
497 tell
498 Usage is
499
500 $z->tell()
501 tell $z
502
503 Returns the uncompressed file offset.
504
505 eof
506 Usage is
507
508 $z->eof();
509 eof($z);
510
511 Returns true if the end of the compressed input stream has been
512 reached.
513
514 seek
515 $z->seek($position, $whence);
516 seek($z, $position, $whence);
517
518 Provides a sub-set of the "seek" functionality, with the restriction
519 that it is only legal to seek forward in the input file/buffer. It is
520 a fatal error to attempt to seek backward.
521
522 Note that the implementation of "seek" in this module does not provide
523 true random access to a compressed file/buffer. It works by
524 uncompressing data from the current offset in the file/buffer until it
525 reaches the uncompressed offset specified in the parameters to "seek".
526 For very small files this may be acceptable behaviour. For large files
527 it may cause an unacceptable delay.
528
529 The $whence parameter takes one the usual values, namely SEEK_SET,
530 SEEK_CUR or SEEK_END.
531
532 Returns 1 on success, 0 on failure.
533
534 binmode
535 Usage is
536
537 $z->binmode
538 binmode $z ;
539
540 This is a noop provided for completeness.
541
542 opened
543 $z->opened()
544
545 Returns true if the object currently refers to a opened file/buffer.
546
547 autoflush
548 my $prev = $z->autoflush()
549 my $prev = $z->autoflush(EXPR)
550
551 If the $z object is associated with a file or a filehandle, this method
552 returns the current autoflush setting for the underlying filehandle. If
553 "EXPR" is present, and is non-zero, it will enable flushing after every
554 write/print operation.
555
556 If $z is associated with a buffer, this method has no effect and always
557 returns "undef".
558
559 Note that the special variable $| cannot be used to set or retrieve the
560 autoflush setting.
561
562 input_line_number
563 $z->input_line_number()
564 $z->input_line_number(EXPR)
565
566 Returns the current uncompressed line number. If "EXPR" is present it
567 has the effect of setting the line number. Note that setting the line
568 number does not change the current position within the file/buffer
569 being read.
570
571 The contents of $/ are used to determine what constitutes a line
572 terminator.
573
574 fileno
575 $z->fileno()
576 fileno($z)
577
578 If the $z object is associated with a file or a filehandle, "fileno"
579 will return the underlying file descriptor. Once the "close" method is
580 called "fileno" will return "undef".
581
582 If the $z object is associated with a buffer, this method will return
583 "undef".
584
585 close
586 $z->close() ;
587 close $z ;
588
589 Closes the output file/buffer.
590
591 For most versions of Perl this method will be automatically invoked if
592 the IO::Uncompress::UnZstd object is destroyed (either explicitly or by
593 the variable with the reference to the object going out of scope). The
594 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
595 cases, the "close" method will be called automatically, but not until
596 global destruction of all live objects when the program is terminating.
597
598 Therefore, if you want your scripts to be able to run on all versions
599 of Perl, you should call "close" explicitly and not rely on automatic
600 closing.
601
602 Returns true on success, otherwise 0.
603
604 If the "AutoClose" option has been enabled when the
605 IO::Uncompress::UnZstd object was created, and the object is associated
606 with a file, the underlying file will also be closed.
607
608 nextStream
609 Usage is
610
611 my $status = $z->nextStream();
612
613 Skips to the next compressed data stream in the input file/buffer. If a
614 new compressed data stream is found, the eof marker will be cleared and
615 $. will be reset to 0.
616
617 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
618 error was encountered.
619
620 trailingData
621 Usage is
622
623 my $data = $z->trailingData();
624
625 Returns the data, if any, that is present immediately after the
626 compressed data stream once uncompression is complete. It only makes
627 sense to call this method once the end of the compressed data stream
628 has been encountered.
629
630 This option can be used when there is useful information immediately
631 following the compressed data stream, and you don't know the length of
632 the compressed data stream.
633
634 If the input is a buffer, "trailingData" will return everything from
635 the end of the compressed data stream to the end of the buffer.
636
637 If the input is a filehandle, "trailingData" will return the data that
638 is left in the filehandle input buffer once the end of the compressed
639 data stream has been reached. You can then use the filehandle to read
640 the rest of the input file.
641
642 Don't bother using "trailingData" if the input is a filename.
643
644 If you know the length of the compressed data stream before you start
645 uncompressing, you can avoid having to use "trailingData" by setting
646 the "InputLength" option in the constructor.
647
649 No symbolic constants are required by IO::Uncompress::UnZstd at
650 present.
651
652 :all Imports "unzstd" and $UnZstdError. Same as doing this
653
654 use IO::Uncompress::UnZstd qw(unzstd $UnZstdError) ;
655
658 General feedback/questions/bug reports should be sent to
659 <https://github.com/pmqs/IO-Compress-Zstd/issues> (preferred) or
660 <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress-Zstd>.
661
663 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
664 IO::Compress::Deflate, IO::Uncompress::Inflate,
665 IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
666 IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma,
667 IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz,
668 IO::Compress::Lzip, IO::Uncompress::UnLzip, IO::Compress::Lzop,
669 IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf,
670 IO::Compress::Zstd, IO::Uncompress::AnyInflate,
671 IO::Uncompress::AnyUncompress
672
673 IO::Compress::FAQ
674
675 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
676
678 This module was written by Paul Marquess, "pmqs@cpan.org".
679
681 See the Changes file.
682
684 Copyright (c) 2005-2023 Paul Marquess. All rights reserved.
685
686 This program is free software; you can redistribute it and/or modify it
687 under the same terms as Perl itself.
688
689
690
691perl v5.38.0 2023-07-27 IO::Uncompress::UnZstd(3)