1IO::Compress::Lzma(3) User Contributed Perl DocumentationIO::Compress::Lzma(3)
2
3
4

NAME

6       IO::Compress::Lzma - Write lzma files/buffers
7

SYNOPSIS

9           use IO::Compress::Lzma qw(lzma $LzmaError) ;
10
11           my $status = lzma $input => $output [,OPTS]
12               or die "lzma failed: $LzmaError\n";
13
14           my $z = new IO::Compress::Lzma $output [,OPTS]
15               or die "lzma failed: $LzmaError\n";
16
17           $z->print($string);
18           $z->printf($format, $string);
19           $z->write($string);
20           $z->syswrite($string [, $length, $offset]);
21           $z->flush();
22           $z->tell();
23           $z->eof();
24           $z->seek($position, $whence);
25           $z->binmode();
26           $z->fileno();
27           $z->opened();
28           $z->autoflush();
29           $z->input_line_number();
30           $z->newStream( [OPTS] );
31
32           $z->close() ;
33
34           $LzmaError ;
35
36           # IO::File mode
37
38           print $z $string;
39           printf $z $format, $string;
40           tell $z
41           eof $z
42           seek $z, $position, $whence
43           binmode $z
44           fileno $z
45           close $z ;
46

DESCRIPTION

48       WARNING -- This is a Beta release.
49
50       ·    DO NOT use in production code.
51
52       ·    The documentation is incomplete in places.
53
54       ·    Parts of the interface defined here are tentative.
55
56       ·    Please report any problems you find.
57
58       This module provides a Perl interface that allows writing lzma
59       compressed data to files or buffer.
60
61       For reading lzma files/buffers, see the companion module
62       IO::Uncompress::UnLzma.
63

Functional Interface

65       A top-level function, "lzma", is provided to carry out "one-shot"
66       compression between buffers and/or files. For finer control over the
67       compression process, see the "OO Interface" section.
68
69           use IO::Compress::Lzma qw(lzma $LzmaError) ;
70
71           lzma $input_filename_or_reference => $output_filename_or_reference [,OPTS]
72               or die "lzma failed: $LzmaError\n";
73
74       The functional interface needs Perl5.005 or better.
75
76   lzma $input_filename_or_reference => $output_filename_or_reference [, OPTS]
77       "lzma" expects at least two parameters, $input_filename_or_reference
78       and $output_filename_or_reference and zero or more optional parameters
79       (see "Optional Parameters")
80
81       The $input_filename_or_reference parameter
82
83       The parameter, $input_filename_or_reference, is used to define the
84       source of the uncompressed data.
85
86       It can take one of the following forms:
87
88       A filename
89            If the $input_filename_or_reference parameter is a simple scalar,
90            it is assumed to be a filename. This file will be opened for
91            reading and the input data will be read from it.
92
93       A filehandle
94            If the $input_filename_or_reference parameter is a filehandle, the
95            input data will be read from it.  The string '-' can be used as an
96            alias for standard input.
97
98       A scalar reference
99            If $input_filename_or_reference is a scalar reference, the input
100            data will be read from $$input_filename_or_reference.
101
102       An array reference
103            If $input_filename_or_reference is an array reference, each
104            element in the array must be a filename.
105
106            The input data will be read from each file in turn.
107
108            The complete array will be walked to ensure that it only contains
109            valid filenames before any data is compressed.
110
111       An Input FileGlob string
112            If $input_filename_or_reference is a string that is delimited by
113            the characters "<" and ">" "lzma" will assume that it is an input
114            fileglob string. The input is the list of files that match the
115            fileglob.
116
117            See File::GlobMapper for more details.
118
119       If the $input_filename_or_reference parameter is any other type,
120       "undef" will be returned.
121
122       The $output_filename_or_reference parameter
123
124       The parameter $output_filename_or_reference is used to control the
125       destination of the compressed data. This parameter can take one of
126       these forms.
127
128       A filename
129            If the $output_filename_or_reference parameter is a simple scalar,
130            it is assumed to be a filename.  This file will be opened for
131            writing and the compressed data will be written to it.
132
133       A filehandle
134            If the $output_filename_or_reference parameter is a filehandle,
135            the compressed data will be written to it.  The string '-' can be
136            used as an alias for standard output.
137
138       A scalar reference
139            If $output_filename_or_reference is a scalar reference, the
140            compressed data will be stored in $$output_filename_or_reference.
141
142       An Array Reference
143            If $output_filename_or_reference is an array reference, the
144            compressed data will be pushed onto the array.
145
146       An Output FileGlob
147            If $output_filename_or_reference is a string that is delimited by
148            the characters "<" and ">" "lzma" will assume that it is an output
149            fileglob string. The output is the list of files that match the
150            fileglob.
151
152            When $output_filename_or_reference is an fileglob string,
153            $input_filename_or_reference must also be a fileglob string.
154            Anything else is an error.
155
156            See File::GlobMapper for more details.
157
158       If the $output_filename_or_reference parameter is any other type,
159       "undef" will be returned.
160
161   Notes
162       When $input_filename_or_reference maps to multiple files/buffers and
163       $output_filename_or_reference is a single file/buffer the input
164       files/buffers will be stored in $output_filename_or_reference as a
165       concatenated series of compressed data streams.
166
167   Optional Parameters
168       The optional parameters for the one-shot function "lzma" are (for the
169       most part) identical to those used with the OO interface defined in the
170       "Constructor Options" section. The exceptions are listed below
171
172       "AutoClose => 0|1"
173            This option applies to any input or output data streams to "lzma"
174            that are filehandles.
175
176            If "AutoClose" is specified, and the value is true, it will result
177            in all input and/or output filehandles being closed once "lzma"
178            has completed.
179
180            This parameter defaults to 0.
181
182       "BinModeIn => 0|1"
183            This option is now a no-op. All files will be read in binmode.
184
185       "Append => 0|1"
186            The behaviour of this option is dependent on the type of output
187            data stream.
188
189            ·    A Buffer
190
191                 If "Append" is enabled, all compressed data will be append to
192                 the end of the output buffer. Otherwise the output buffer
193                 will be cleared before any compressed data is written to it.
194
195            ·    A Filename
196
197                 If "Append" is enabled, the file will be opened in append
198                 mode. Otherwise the contents of the file, if any, will be
199                 truncated before any compressed data is written to it.
200
201            ·    A Filehandle
202
203                 If "Append" is enabled, the filehandle will be positioned to
204                 the end of the file via a call to "seek" before any
205                 compressed data is written to it.  Otherwise the file pointer
206                 will not be moved.
207
208            When "Append" is specified, and set to true, it will append all
209            compressed data to the output data stream.
210
211            So when the output is a filehandle it will carry out a seek to the
212            eof before writing any compressed data. If the output is a
213            filename, it will be opened for appending. If the output is a
214            buffer, all compressed data will be appended to the existing
215            buffer.
216
217            Conversely when "Append" is not specified, or it is present and is
218            set to false, it will operate as follows.
219
220            When the output is a filename, it will truncate the contents of
221            the file before writing any compressed data. If the output is a
222            filehandle its position will not be changed. If the output is a
223            buffer, it will be wiped before any compressed data is output.
224
225            Defaults to 0.
226
227   Examples
228       Here are a few example that show the capabilities of the module.
229
230       Streaming
231
232       This very simple command line example demonstrates the streaming
233       capabilities of the module.  The code reads data from STDIN, compresses
234       it, and writes the compressed data to STDOUT.
235
236           $ echo hello world | perl -MIO::Compress::Lzma=lzma -e 'lzma \*STDIN => \*STDOUT' >output.lzma
237
238       The special filename "-" can be used as a standin for both "\*STDIN"
239       and "\*STDOUT", so the above can be rewritten as
240
241           $ echo hello world | perl -MIO::Compress::Lzma=lzma -e 'lzma "-" => "-"' >output.lzma
242
243       Compressing a file from the filesystem
244
245       To read the contents of the file "file1.txt" and write the compressed
246       data to the file "file1.txt.lzma".
247
248           use strict ;
249           use warnings ;
250           use IO::Compress::Lzma qw(lzma $LzmaError) ;
251
252           my $input = "file1.txt";
253           lzma $input => "$input.lzma"
254               or die "lzma failed: $LzmaError\n";
255
256       Reading from a Filehandle and writing to an in-memory buffer
257
258       To read from an existing Perl filehandle, $input, and write the
259       compressed data to a buffer, $buffer.
260
261           use strict ;
262           use warnings ;
263           use IO::Compress::Lzma qw(lzma $LzmaError) ;
264           use IO::File ;
265
266           my $input = new IO::File "<file1.txt"
267               or die "Cannot open 'file1.txt': $!\n" ;
268           my $buffer ;
269           lzma $input => \$buffer
270               or die "lzma failed: $LzmaError\n";
271
272       Compressing multiple files
273
274       To compress all files in the directory "/my/home" that match "*.txt"
275       and store the compressed data in the same directory
276
277           use strict ;
278           use warnings ;
279           use IO::Compress::Lzma qw(lzma $LzmaError) ;
280
281           lzma '</my/home/*.txt>' => '<*.lzma>'
282               or die "lzma failed: $LzmaError\n";
283
284       and if you want to compress each file one at a time, this will do the
285       trick
286
287           use strict ;
288           use warnings ;
289           use IO::Compress::Lzma qw(lzma $LzmaError) ;
290
291           for my $input ( glob "/my/home/*.txt" )
292           {
293               my $output = "$input.lzma" ;
294               lzma $input => $output
295                   or die "Error compressing '$input': $LzmaError\n";
296           }
297

OO Interface

299   Constructor
300       The format of the constructor for "IO::Compress::Lzma" is shown below
301
302           my $z = new IO::Compress::Lzma $output [,OPTS]
303               or die "IO::Compress::Lzma failed: $LzmaError\n";
304
305       It returns an "IO::Compress::Lzma" object on success and undef on
306       failure.  The variable $LzmaError will contain an error message on
307       failure.
308
309       If you are running Perl 5.005 or better the object, $z, returned from
310       IO::Compress::Lzma can be used exactly like an IO::File filehandle.
311       This means that all normal output file operations can be carried out
312       with $z.  For example, to write to a compressed file/buffer you can use
313       either of these forms
314
315           $z->print("hello world\n");
316           print $z "hello world\n";
317
318       The mandatory parameter $output is used to control the destination of
319       the compressed data. This parameter can take one of these forms.
320
321       A filename
322            If the $output parameter is a simple scalar, it is assumed to be a
323            filename. This file will be opened for writing and the compressed
324            data will be written to it.
325
326       A filehandle
327            If the $output parameter is a filehandle, the compressed data will
328            be written to it.  The string '-' can be used as an alias for
329            standard output.
330
331       A scalar reference
332            If $output is a scalar reference, the compressed data will be
333            stored in $$output.
334
335       If the $output parameter is any other type, "IO::Compress::Lzma"::new
336       will return undef.
337
338   Constructor Options
339       "OPTS" is any combination of zero or more the following options:
340
341       "AutoClose => 0|1"
342            This option is only valid when the $output parameter is a
343            filehandle. If specified, and the value is true, it will result in
344            the $output being closed once either the "close" method is called
345            or the "IO::Compress::Lzma" object is destroyed.
346
347            This parameter defaults to 0.
348
349       "Append => 0|1"
350            Opens $output in append mode.
351
352            The behaviour of this option is dependent on the type of $output.
353
354            ·    A Buffer
355
356                 If $output is a buffer and "Append" is enabled, all
357                 compressed data will be append to the end of $output.
358                 Otherwise $output will be cleared before any data is written
359                 to it.
360
361            ·    A Filename
362
363                 If $output is a filename and "Append" is enabled, the file
364                 will be opened in append mode. Otherwise the contents of the
365                 file, if any, will be truncated before any compressed data is
366                 written to it.
367
368            ·    A Filehandle
369
370                 If $output is a filehandle, the file pointer will be
371                 positioned to the end of the file via a call to "seek" before
372                 any compressed data is written to it.  Otherwise the file
373                 pointer will not be moved.
374
375            This parameter defaults to 0.
376
377       "Filter => $filter"
378            When present  $filter  option must be an object of type
379            "Lzma::Filter::Lzma1".  See "Lzma::Filter::Lzma" for a definition
380            of "Lzma::Filter::Lzma1".
381
382            If this option is not present an "Lzma::Filter::Lzma1" object with
383            default values will be used.
384
385       "Strict => 0|1"
386            This is a placeholder option.
387
388   Examples
389       TODO
390

Methods

392   print
393       Usage is
394
395           $z->print($data)
396           print $z $data
397
398       Compresses and outputs the contents of the $data parameter. This has
399       the same behaviour as the "print" built-in.
400
401       Returns true if successful.
402
403   printf
404       Usage is
405
406           $z->printf($format, $data)
407           printf $z $format, $data
408
409       Compresses and outputs the contents of the $data parameter.
410
411       Returns true if successful.
412
413   syswrite
414       Usage is
415
416           $z->syswrite $data
417           $z->syswrite $data, $length
418           $z->syswrite $data, $length, $offset
419
420       Compresses and outputs the contents of the $data parameter.
421
422       Returns the number of uncompressed bytes written, or "undef" if
423       unsuccessful.
424
425   write
426       Usage is
427
428           $z->write $data
429           $z->write $data, $length
430           $z->write $data, $length, $offset
431
432       Compresses and outputs the contents of the $data parameter.
433
434       Returns the number of uncompressed bytes written, or "undef" if
435       unsuccessful.
436
437   flush
438       Usage is
439
440           $z->flush;
441
442       Flushes any pending compressed data to the output file/buffer.
443
444       Returns true on success.
445
446   tell
447       Usage is
448
449           $z->tell()
450           tell $z
451
452       Returns the uncompressed file offset.
453
454   eof
455       Usage is
456
457           $z->eof();
458           eof($z);
459
460       Returns true if the "close" method has been called.
461
462   seek
463           $z->seek($position, $whence);
464           seek($z, $position, $whence);
465
466       Provides a sub-set of the "seek" functionality, with the restriction
467       that it is only legal to seek forward in the output file/buffer.  It is
468       a fatal error to attempt to seek backward.
469
470       Empty parts of the file/buffer will have NULL (0x00) bytes written to
471       them.
472
473       The $whence parameter takes one the usual values, namely SEEK_SET,
474       SEEK_CUR or SEEK_END.
475
476       Returns 1 on success, 0 on failure.
477
478   binmode
479       Usage is
480
481           $z->binmode
482           binmode $z ;
483
484       This is a noop provided for completeness.
485
486   opened
487           $z->opened()
488
489       Returns true if the object currently refers to a opened file/buffer.
490
491   autoflush
492           my $prev = $z->autoflush()
493           my $prev = $z->autoflush(EXPR)
494
495       If the $z object is associated with a file or a filehandle, this method
496       returns the current autoflush setting for the underlying filehandle. If
497       "EXPR" is present, and is non-zero, it will enable flushing after every
498       write/print operation.
499
500       If $z is associated with a buffer, this method has no effect and always
501       returns "undef".
502
503       Note that the special variable $| cannot be used to set or retrieve the
504       autoflush setting.
505
506   input_line_number
507           $z->input_line_number()
508           $z->input_line_number(EXPR)
509
510       This method always returns "undef" when compressing.
511
512   fileno
513           $z->fileno()
514           fileno($z)
515
516       If the $z object is associated with a file or a filehandle, "fileno"
517       will return the underlying file descriptor. Once the "close" method is
518       called "fileno" will return "undef".
519
520       If the $z object is associated with a buffer, this method will return
521       "undef".
522
523   close
524           $z->close() ;
525           close $z ;
526
527       Flushes any pending compressed data and then closes the output
528       file/buffer.
529
530       For most versions of Perl this method will be automatically invoked if
531       the IO::Compress::Lzma object is destroyed (either explicitly or by the
532       variable with the reference to the object going out of scope). The
533       exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
534       cases, the "close" method will be called automatically, but not until
535       global destruction of all live objects when the program is terminating.
536
537       Therefore, if you want your scripts to be able to run on all versions
538       of Perl, you should call "close" explicitly and not rely on automatic
539       closing.
540
541       Returns true on success, otherwise 0.
542
543       If the "AutoClose" option has been enabled when the IO::Compress::Lzma
544       object was created, and the object is associated with a file, the
545       underlying file will also be closed.
546
547   newStream([OPTS])
548       Usage is
549
550           $z->newStream( [OPTS] )
551
552       Closes the current compressed data stream and starts a new one.
553
554       OPTS consists of any of the options that are available when creating
555       the $z object.
556
557       See the "Constructor Options" section for more details.
558

Importing

560       No symbolic constants are required by this IO::Compress::Lzma at
561       present.
562
563       :all Imports "lzma" and $LzmaError.  Same as doing this
564
565                use IO::Compress::Lzma qw(lzma $LzmaError) ;
566

EXAMPLES

SUPPORT

569       General feedback/questions/bug reports should be sent to
570       <https://github.com/pmqs/IO-Compress-Lzma/issues> (preferred) or
571       <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress-Lzma>.
572

SEE ALSO

574       Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
575       IO::Compress::Deflate, IO::Uncompress::Inflate,
576       IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
577       IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Uncompress::UnLzma,
578       IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip,
579       IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
580       IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
581       IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
582       IO::Uncompress::AnyUncompress
583
584       IO::Compress::FAQ
585
586       File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
587

AUTHOR

589       This module was written by Paul Marquess, "pmqs@cpan.org".
590

MODIFICATION HISTORY

592       See the Changes file.
593
595       Copyright (c) 2005-2019 Paul Marquess. All rights reserved.
596
597       This program is free software; you can redistribute it and/or modify it
598       under the same terms as Perl itself.
599
600
601
602perl v5.30.1                      2020-01-30             IO::Compress::Lzma(3)
Impressum