1IO::Compress::Lzma(3) User Contributed Perl DocumentationIO::Compress::Lzma(3)
2
3
4
6 IO::Compress::Lzma - Write lzma files/buffers
7
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
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
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 => $output [,OPTS]
72 or die "lzma failed: $LzmaError\n";
73
74 The functional interface needs Perl5.005 or better.
75
76 lzma $input => $output [, OPTS]
77 "lzma" expects at least two parameters, $input and $output.
78
79 The $input parameter
80
81 The parameter, $input, is used to define the source of the uncompressed
82 data.
83
84 It can take one of the following forms:
85
86 A filename
87 If the $input parameter is a simple scalar, it is assumed to be a
88 filename. This file will be opened for reading and the input data
89 will be read from it.
90
91 A filehandle
92 If the $input parameter is a filehandle, the input data will be
93 read from it. The string '-' can be used as an alias for standard
94 input.
95
96 A scalar reference
97 If $input is a scalar reference, the input data will be read from
98 $$input.
99
100 An array reference
101 If $input is an array reference, each element in the array must be
102 a filename.
103
104 The input data will be read from each file in turn.
105
106 The complete array will be walked to ensure that it only contains
107 valid filenames before any data is compressed.
108
109 An Input FileGlob string
110 If $input is a string that is delimited by the characters "<" and
111 ">" "lzma" will assume that it is an input fileglob string. The
112 input is the list of files that match the fileglob.
113
114 See File::GlobMapper for more details.
115
116 If the $input parameter is any other type, "undef" will be returned.
117
118 The $output parameter
119
120 The parameter $output is used to control the destination of the
121 compressed data. This parameter can take one of these forms.
122
123 A filename
124 If the $output parameter is a simple scalar, it is assumed to be a
125 filename. This file will be opened for writing and the compressed
126 data will be written to it.
127
128 A filehandle
129 If the $output parameter is a filehandle, the compressed data will
130 be written to it. The string '-' can be used as an alias for
131 standard output.
132
133 A scalar reference
134 If $output is a scalar reference, the compressed data will be
135 stored in $$output.
136
137 An Array Reference
138 If $output is an array reference, the compressed data will be
139 pushed onto the array.
140
141 An Output FileGlob
142 If $output is a string that is delimited by the characters "<" and
143 ">" "lzma" will assume that it is an output fileglob string. The
144 output is the list of files that match the fileglob.
145
146 When $output is an fileglob string, $input must also be a fileglob
147 string. Anything else is an error.
148
149 See File::GlobMapper for more details.
150
151 If the $output parameter is any other type, "undef" will be returned.
152
153 Notes
154 When $input maps to multiple files/buffers and $output is a single
155 file/buffer the input files/buffers will be stored in $output as a
156 concatenated series of compressed data streams.
157
158 Optional Parameters
159 Unless specified below, the optional parameters for "lzma", "OPTS", are
160 the same as those used with the OO interface defined in the
161 "Constructor Options" section below.
162
163 "AutoClose => 0|1"
164 This option applies to any input or output data streams to "lzma"
165 that are filehandles.
166
167 If "AutoClose" is specified, and the value is true, it will result
168 in all input and/or output filehandles being closed once "lzma"
169 has completed.
170
171 This parameter defaults to 0.
172
173 "BinModeIn => 0|1"
174 When reading from a file or filehandle, set "binmode" before
175 reading.
176
177 Defaults to 0.
178
179 "Append => 0|1"
180 The behaviour of this option is dependent on the type of output
181 data stream.
182
183 · A Buffer
184
185 If "Append" is enabled, all compressed data will be append to
186 the end of the output buffer. Otherwise the output buffer
187 will be cleared before any compressed data is written to it.
188
189 · A Filename
190
191 If "Append" is enabled, the file will be opened in append
192 mode. Otherwise the contents of the file, if any, will be
193 truncated before any compressed data is written to it.
194
195 · A Filehandle
196
197 If "Append" is enabled, the filehandle will be positioned to
198 the end of the file via a call to "seek" before any
199 compressed data is written to it. Otherwise the file pointer
200 will not be moved.
201
202 When "Append" is specified, and set to true, it will append all
203 compressed data to the output data stream.
204
205 So when the output is a filehandle it will carry out a seek to the
206 eof before writing any compressed data. If the output is a
207 filename, it will be opened for appending. If the output is a
208 buffer, all compressed data will be appened to the existing
209 buffer.
210
211 Conversely when "Append" is not specified, or it is present and is
212 set to false, it will operate as follows.
213
214 When the output is a filename, it will truncate the contents of
215 the file before writing any compressed data. If the output is a
216 filehandle its position will not be changed. If the output is a
217 buffer, it will be wiped before any compressed data is output.
218
219 Defaults to 0.
220
221 Examples
222 To read the contents of the file "file1.txt" and write the compressed
223 data to the file "file1.txt.lzma".
224
225 use strict ;
226 use warnings ;
227 use IO::Compress::Lzma qw(lzma $LzmaError) ;
228
229 my $input = "file1.txt";
230 lzma $input => "$input.lzma"
231 or die "lzma failed: $LzmaError\n";
232
233 To read from an existing Perl filehandle, $input, and write the
234 compressed data to a buffer, $buffer.
235
236 use strict ;
237 use warnings ;
238 use IO::Compress::Lzma qw(lzma $LzmaError) ;
239 use IO::File ;
240
241 my $input = new IO::File "<file1.txt"
242 or die "Cannot open 'file1.txt': $!\n" ;
243 my $buffer ;
244 lzma $input => \$buffer
245 or die "lzma failed: $LzmaError\n";
246
247 To compress all files in the directory "/my/home" that match "*.txt"
248 and store the compressed data in the same directory
249
250 use strict ;
251 use warnings ;
252 use IO::Compress::Lzma qw(lzma $LzmaError) ;
253
254 lzma '</my/home/*.txt>' => '<*.lzma>'
255 or die "lzma failed: $LzmaError\n";
256
257 and if you want to compress each file one at a time, this will do the
258 trick
259
260 use strict ;
261 use warnings ;
262 use IO::Compress::Lzma qw(lzma $LzmaError) ;
263
264 for my $input ( glob "/my/home/*.txt" )
265 {
266 my $output = "$input.lzma" ;
267 lzma $input => $output
268 or die "Error compressing '$input': $LzmaError\n";
269 }
270
272 Constructor
273 The format of the constructor for "IO::Compress::Lzma" is shown below
274
275 my $z = new IO::Compress::Lzma $output [,OPTS]
276 or die "IO::Compress::Lzma failed: $LzmaError\n";
277
278 It returns an "IO::Compress::Lzma" object on success and undef on
279 failure. The variable $LzmaError will contain an error message on
280 failure.
281
282 If you are running Perl 5.005 or better the object, $z, returned from
283 IO::Compress::Lzma can be used exactly like an IO::File filehandle.
284 This means that all normal output file operations can be carried out
285 with $z. For example, to write to a compressed file/buffer you can use
286 either of these forms
287
288 $z->print("hello world\n");
289 print $z "hello world\n";
290
291 The mandatory parameter $output is used to control the destination of
292 the compressed data. This parameter can take one of these forms.
293
294 A filename
295 If the $output parameter is a simple scalar, it is assumed to be a
296 filename. This file will be opened for writing and the compressed
297 data will be written to it.
298
299 A filehandle
300 If the $output parameter is a filehandle, the compressed data will
301 be written to it. The string '-' can be used as an alias for
302 standard output.
303
304 A scalar reference
305 If $output is a scalar reference, the compressed data will be
306 stored in $$output.
307
308 If the $output parameter is any other type, "IO::Compress::Lzma"::new
309 will return undef.
310
311 Constructor Options
312 "OPTS" is any combination of the following options:
313
314 "AutoClose => 0|1"
315 This option is only valid when the $output parameter is a
316 filehandle. If specified, and the value is true, it will result in
317 the $output being closed once either the "close" method is called
318 or the "IO::Compress::Lzma" object is destroyed.
319
320 This parameter defaults to 0.
321
322 "Append => 0|1"
323 Opens $output in append mode.
324
325 The behaviour of this option is dependent on the type of $output.
326
327 · A Buffer
328
329 If $output is a buffer and "Append" is enabled, all
330 compressed data will be append to the end of $output.
331 Otherwise $output will be cleared before any data is written
332 to it.
333
334 · A Filename
335
336 If $output is a filename and "Append" is enabled, the file
337 will be opened in append mode. Otherwise the contents of the
338 file, if any, will be truncated before any compressed data is
339 written to it.
340
341 · A Filehandle
342
343 If $output is a filehandle, the file pointer will be
344 positioned to the end of the file via a call to "seek" before
345 any compressed data is written to it. Otherwise the file
346 pointer will not be moved.
347
348 This parameter defaults to 0.
349
350 "Filter => $filter"
351 When present $filter option must be an object of type
352 "Lzma::Filter::Lzma1". See "Lzma::Filter::Lzma" for a definition
353 of "Lzma::Filter::Lzma1".
354
355 If this option is not present an "Lzma::Filter::Lzma1" object with
356 default values will be used.
357
358 "Strict => 0|1"
359 This is a placeholder option.
360
361 Examples
362 TODO
363
365 print
366 Usage is
367
368 $z->print($data)
369 print $z $data
370
371 Compresses and outputs the contents of the $data parameter. This has
372 the same behaviour as the "print" built-in.
373
374 Returns true if successful.
375
376 printf
377 Usage is
378
379 $z->printf($format, $data)
380 printf $z $format, $data
381
382 Compresses and outputs the contents of the $data parameter.
383
384 Returns true if successful.
385
386 syswrite
387 Usage is
388
389 $z->syswrite $data
390 $z->syswrite $data, $length
391 $z->syswrite $data, $length, $offset
392
393 Compresses and outputs the contents of the $data parameter.
394
395 Returns the number of uncompressed bytes written, or "undef" if
396 unsuccessful.
397
398 write
399 Usage is
400
401 $z->write $data
402 $z->write $data, $length
403 $z->write $data, $length, $offset
404
405 Compresses and outputs the contents of the $data parameter.
406
407 Returns the number of uncompressed bytes written, or "undef" if
408 unsuccessful.
409
410 flush
411 Usage is
412
413 $z->flush;
414
415 Flushes any pending compressed data to the output file/buffer.
416
417 Returns true on success.
418
419 tell
420 Usage is
421
422 $z->tell()
423 tell $z
424
425 Returns the uncompressed file offset.
426
427 eof
428 Usage is
429
430 $z->eof();
431 eof($z);
432
433 Returns true if the "close" method has been called.
434
435 seek
436 $z->seek($position, $whence);
437 seek($z, $position, $whence);
438
439 Provides a sub-set of the "seek" functionality, with the restriction
440 that it is only legal to seek forward in the output file/buffer. It is
441 a fatal error to attempt to seek backward.
442
443 Empty parts of the file/buffer will have NULL (0x00) bytes written to
444 them.
445
446 The $whence parameter takes one the usual values, namely SEEK_SET,
447 SEEK_CUR or SEEK_END.
448
449 Returns 1 on success, 0 on failure.
450
451 binmode
452 Usage is
453
454 $z->binmode
455 binmode $z ;
456
457 This is a noop provided for completeness.
458
459 opened
460 $z->opened()
461
462 Returns true if the object currently refers to a opened file/buffer.
463
464 autoflush
465 my $prev = $z->autoflush()
466 my $prev = $z->autoflush(EXPR)
467
468 If the $z object is associated with a file or a filehandle, this method
469 returns the current autoflush setting for the underlying filehandle. If
470 "EXPR" is present, and is non-zero, it will enable flushing after every
471 write/print operation.
472
473 If $z is associated with a buffer, this method has no effect and always
474 returns "undef".
475
476 Note that the special variable $| cannot be used to set or retrieve the
477 autoflush setting.
478
479 input_line_number
480 $z->input_line_number()
481 $z->input_line_number(EXPR)
482
483 This method always returns "undef" when compressing.
484
485 fileno
486 $z->fileno()
487 fileno($z)
488
489 If the $z object is associated with a file or a filehandle, "fileno"
490 will return the underlying file descriptor. Once the "close" method is
491 called "fileno" will return "undef".
492
493 If the $z object is is associated with a buffer, this method will
494 return "undef".
495
496 close
497 $z->close() ;
498 close $z ;
499
500 Flushes any pending compressed data and then closes the output
501 file/buffer.
502
503 For most versions of Perl this method will be automatically invoked if
504 the IO::Compress::Lzma object is destroyed (either explicitly or by the
505 variable with the reference to the object going out of scope). The
506 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
507 cases, the "close" method will be called automatically, but not until
508 global destruction of all live objects when the program is terminating.
509
510 Therefore, if you want your scripts to be able to run on all versions
511 of Perl, you should call "close" explicitly and not rely on automatic
512 closing.
513
514 Returns true on success, otherwise 0.
515
516 If the "AutoClose" option has been enabled when the IO::Compress::Lzma
517 object was created, and the object is associated with a file, the
518 underlying file will also be closed.
519
520 newStream([OPTS])
521 Usage is
522
523 $z->newStream( [OPTS] )
524
525 Closes the current compressed data stream and starts a new one.
526
527 OPTS consists of any of the the options that are available when
528 creating the $z object.
529
530 See the "Constructor Options" section for more details.
531
533 No symbolic constants are required by this IO::Compress::Lzma at
534 present.
535
536 :all Imports "lzma" and $LzmaError. Same as doing this
537
538 use IO::Compress::Lzma qw(lzma $LzmaError) ;
539
541 Apache::GZip Revisited
542 See IO::Compress::Lzma::FAQ
543
545 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
546 IO::Compress::Deflate, IO::Uncompress::Inflate,
547 IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
548 IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Uncompress::UnLzma,
549 IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzop,
550 IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf,
551 IO::Uncompress::AnyInflate, IO::Uncompress::AnyUncompress
552
553 Compress::Zlib::FAQ
554
555 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
556
558 This module was written by Paul Marquess, pmqs@cpan.org.
559
561 See the Changes file.
562
564 Copyright (c) 2005-2010 Paul Marquess. All rights reserved.
565
566 This program is free software; you can redistribute it and/or modify it
567 under the same terms as Perl itself.
568
569
570
571perl v5.12.1 2010-07-24 IO::Compress::Lzma(3)