Commit | Line | Data |
---|---|---|
d54256af PM |
1 | |
2 | =head1 NAME | |
3 | ||
319fab50 | 4 | IO::Compress::FAQ -- Frequently Asked Questions about IO::Compress |
d54256af PM |
5 | |
6 | =head1 DESCRIPTION | |
7 | ||
8 | Common questions answered. | |
9 | ||
10 | =head2 Compatibility with Unix compress/uncompress. | |
11 | ||
319fab50 PM |
12 | Although C<Compress::Zlib> has a pair of functions called C<compress> and |
13 | C<uncompress>, they are I<not> related to the Unix programs of the same | |
14 | name. The C<Compress::Zlib> module is not compatible with Unix | |
15 | C<compress>. | |
d54256af PM |
16 | |
17 | If you have the C<uncompress> program available, you can use this to read | |
18 | compressed files | |
19 | ||
20 | open F, "uncompress -c $filename |"; | |
21 | while (<F>) | |
22 | { | |
23 | ... | |
24 | ||
25 | Alternatively, if you have the C<gunzip> program available, you can use | |
26 | this to read compressed files | |
27 | ||
28 | open F, "gunzip -c $filename |"; | |
29 | while (<F>) | |
30 | { | |
31 | ... | |
32 | ||
33 | and this to write compress files, if you have the C<compress> program | |
34 | available | |
35 | ||
36 | open F, "| compress -c $filename "; | |
37 | print F "data"; | |
38 | ... | |
39 | close F ; | |
40 | ||
41 | =head2 Accessing .tar.Z files | |
42 | ||
319fab50 PM |
43 | The C<Archive::Tar> module can optionally use C<Compress::Zlib> (via the |
44 | C<IO::Zlib> module) to access tar files that have been compressed with | |
45 | C<gzip>. Unfortunately tar files compressed with the Unix C<compress> | |
46 | utility cannot be read by C<Compress::Zlib> and so cannot be directly | |
47 | accessed by C<Archive::Tar>. | |
d54256af | 48 | |
319fab50 PM |
49 | If the C<uncompress> or C<gunzip> programs are available, you can use one |
50 | of these workarounds to read C<.tar.Z> files from C<Archive::Tar> | |
d54256af PM |
51 | |
52 | Firstly with C<uncompress> | |
53 | ||
54 | use strict; | |
55 | use warnings; | |
56 | use Archive::Tar; | |
57 | ||
58 | open F, "uncompress -c $filename |"; | |
59 | my $tar = Archive::Tar->new(*F); | |
60 | ... | |
61 | ||
62 | and this with C<gunzip> | |
63 | ||
64 | use strict; | |
65 | use warnings; | |
66 | use Archive::Tar; | |
67 | ||
68 | open F, "gunzip -c $filename |"; | |
69 | my $tar = Archive::Tar->new(*F); | |
70 | ... | |
71 | ||
72 | Similarly, if the C<compress> program is available, you can use this to | |
73 | write a C<.tar.Z> file | |
74 | ||
75 | use strict; | |
76 | use warnings; | |
77 | use Archive::Tar; | |
78 | use IO::File; | |
79 | ||
80 | my $fh = new IO::File "| compress -c >$filename"; | |
81 | my $tar = Archive::Tar->new(); | |
82 | ... | |
83 | $tar->write($fh); | |
84 | $fh->close ; | |
85 | ||
86 | =head2 Accessing Zip Files | |
87 | ||
88 | This module provides support for reading/writing zip files using the | |
89 | C<IO::Compress::Zip> and C<IO::Uncompress::Unzip> modules. | |
90 | ||
91 | The primary focus of the C<IO::Compress::Zip> and C<IO::Uncompress::Unzip> | |
92 | modules is to provide an C<IO::File> compatible streaming read/write | |
93 | interface to zip files/buffers. They are not fully flegged archivers. If | |
94 | you are looking for an archiver check out the C<Archive::Zip> module. You | |
95 | can find it on CPAN at | |
96 | ||
97 | http://www.cpan.org/modules/by-module/Archive/Archive-Zip-*.tar.gz | |
98 | ||
99 | =head2 Compressed files and Net::FTP | |
100 | ||
101 | The C<Net::FTP> module provides two low-level methods called C<stor> and | |
102 | C<retr> that both return filehandles. These filehandles can used with the | |
103 | C<IO::Compress/Uncompress> modules to compress or uncompress files read | |
104 | from or written to an FTP Server on the fly, without having to create a | |
105 | temporary file. | |
106 | ||
107 | Firstly, here is code that uses C<retr> to uncompressed a file as it is | |
108 | read from the FTP Server. | |
109 | ||
110 | use Net::FTP; | |
111 | use IO::Uncompress::Gunzip qw(:all); | |
112 | ||
113 | my $ftp = new Net::FTP ... | |
114 | ||
115 | my $retr_fh = $ftp->retr($compressed_filename); | |
116 | gunzip $retr_fh => $outFilename, AutoClose => 1 | |
117 | or die "Cannot uncompress '$compressed_file': $GunzipError\n"; | |
118 | ||
119 | and this to compress a file as it is written to the FTP Server | |
120 | ||
121 | use Net::FTP; | |
122 | use IO::Compress::Gzip qw(:all); | |
123 | ||
124 | my $stor_fh = $ftp->stor($filename); | |
125 | gzip "filename" => $stor_fh, AutoClose => 1 | |
126 | or die "Cannot compress '$filename': $GzipError\n"; | |
127 | ||
128 | =head2 How do I recompress using a different compression? | |
129 | ||
130 | This is easier that you might expect if you realise that all the | |
131 | C<IO::Compress::*> objects are derived from C<IO::File> and that all the | |
132 | C<IO::Uncompress::*> modules can read from an C<IO::File> filehandle. | |
133 | ||
134 | So, for example, say you have a file compressed with gzip that you want to | |
135 | recompress with bzip2. Here is all that is needed to carry out the | |
136 | recompression. | |
137 | ||
138 | use IO::Uncompress::Gunzip ':all'; | |
139 | use IO::Compress::Bzip2 ':all'; | |
140 | ||
141 | my $gzipFile = "somefile.gz"; | |
142 | my $bzipFile = "somefile.bz2"; | |
143 | ||
144 | my $gunzip = new IO::Uncompress::Gunzip $gzipFile | |
145 | or die "Cannot gunzip $gzipFile: $GunzipError\n" ; | |
146 | ||
147 | bzip2 $gunzip => $bzipFile | |
148 | or die "Cannot bzip2 to $bzipFile: $Bzip2Error\n" ; | |
149 | ||
150 | Note, there is a limitation of this technique. Some compression file | |
151 | formats store extra information along with the compressed data payload. For | |
152 | example, gzip can optionally store the original filename and Zip stores a | |
153 | lot of information about the original file. If the original compressed file | |
154 | contains any of this extra information, it will not be transferred to the | |
155 | new compressed file usign the technique above. | |
156 | ||
157 | =head2 Apache::GZip Revisited | |
158 | ||
159 | Below is a mod_perl Apache compression module, called C<Apache::GZip>, | |
160 | taken from | |
161 | F<http://perl.apache.org/docs/tutorials/tips/mod_perl_tricks/mod_perl_tricks.html#On_the_Fly_Compression> | |
162 | ||
163 | package Apache::GZip; | |
164 | #File: Apache::GZip.pm | |
165 | ||
166 | use strict vars; | |
167 | use Apache::Constants ':common'; | |
168 | use Compress::Zlib; | |
169 | use IO::File; | |
170 | use constant GZIP_MAGIC => 0x1f8b; | |
171 | use constant OS_MAGIC => 0x03; | |
172 | ||
173 | sub handler { | |
174 | my $r = shift; | |
175 | my ($fh,$gz); | |
176 | my $file = $r->filename; | |
177 | return DECLINED unless $fh=IO::File->new($file); | |
178 | $r->header_out('Content-Encoding'=>'gzip'); | |
179 | $r->send_http_header; | |
180 | return OK if $r->header_only; | |
181 | ||
182 | tie *STDOUT,'Apache::GZip',$r; | |
183 | print($_) while <$fh>; | |
184 | untie *STDOUT; | |
185 | return OK; | |
186 | } | |
187 | ||
188 | sub TIEHANDLE { | |
189 | my($class,$r) = @_; | |
190 | # initialize a deflation stream | |
191 | my $d = deflateInit(-WindowBits=>-MAX_WBITS()) || return undef; | |
192 | ||
193 | # gzip header -- don't ask how I found out | |
194 | $r->print(pack("nccVcc",GZIP_MAGIC,Z_DEFLATED,0,time(),0,OS_MAGIC)); | |
195 | ||
196 | return bless { r => $r, | |
197 | crc => crc32(undef), | |
198 | d => $d, | |
199 | l => 0 | |
200 | },$class; | |
201 | } | |
202 | ||
203 | sub PRINT { | |
204 | my $self = shift; | |
205 | foreach (@_) { | |
206 | # deflate the data | |
207 | my $data = $self->{d}->deflate($_); | |
208 | $self->{r}->print($data); | |
209 | # keep track of its length and crc | |
210 | $self->{l} += length($_); | |
211 | $self->{crc} = crc32($_,$self->{crc}); | |
212 | } | |
213 | } | |
214 | ||
215 | sub DESTROY { | |
216 | my $self = shift; | |
217 | ||
218 | # flush the output buffers | |
219 | my $data = $self->{d}->flush; | |
220 | $self->{r}->print($data); | |
221 | ||
222 | # print the CRC and the total length (uncompressed) | |
223 | $self->{r}->print(pack("LL",@{$self}{qw/crc l/})); | |
224 | } | |
225 | ||
226 | 1; | |
227 | ||
228 | Here's the Apache configuration entry you'll need to make use of it. Once | |
229 | set it will result in everything in the /compressed directory will be | |
230 | compressed automagically. | |
231 | ||
232 | <Location /compressed> | |
233 | SetHandler perl-script | |
234 | PerlHandler Apache::GZip | |
235 | </Location> | |
236 | ||
237 | Although at first sight there seems to be quite a lot going on in | |
238 | C<Apache::GZip>, you could sum up what the code was doing as follows -- | |
239 | read the contents of the file in C<< $r->filename >>, compress it and write | |
240 | the compressed data to standard output. That's all. | |
241 | ||
242 | This code has to jump through a few hoops to achieve this because | |
243 | ||
244 | =over | |
245 | ||
246 | =item 1. | |
247 | ||
248 | The gzip support in C<Compress::Zlib> version 1.x can only work with a real | |
249 | filesystem filehandle. The filehandles used by Apache modules are not | |
250 | associated with the filesystem. | |
251 | ||
252 | =item 2. | |
253 | ||
254 | That means all the gzip support has to be done by hand - in this case by | |
255 | creating a tied filehandle to deal with creating the gzip header and | |
256 | trailer. | |
257 | ||
258 | =back | |
259 | ||
260 | C<IO::Compress::Gzip> doesn't have that filehandle limitation (this was one | |
261 | of the reasons for writing it in the first place). So if | |
262 | C<IO::Compress::Gzip> is used instead of C<Compress::Zlib> the whole tied | |
263 | filehandle code can be removed. Here is the rewritten code. | |
264 | ||
265 | package Apache::GZip; | |
266 | ||
267 | use strict vars; | |
268 | use Apache::Constants ':common'; | |
269 | use IO::Compress::Gzip; | |
270 | use IO::File; | |
271 | ||
272 | sub handler { | |
273 | my $r = shift; | |
274 | my ($fh,$gz); | |
275 | my $file = $r->filename; | |
276 | return DECLINED unless $fh=IO::File->new($file); | |
277 | $r->header_out('Content-Encoding'=>'gzip'); | |
278 | $r->send_http_header; | |
279 | return OK if $r->header_only; | |
280 | ||
281 | my $gz = new IO::Compress::Gzip '-', Minimal => 1 | |
282 | or return DECLINED ; | |
283 | ||
284 | print $gz $_ while <$fh>; | |
285 | ||
286 | return OK; | |
287 | } | |
288 | ||
289 | or even more succinctly, like this, using a one-shot gzip | |
290 | ||
291 | package Apache::GZip; | |
292 | ||
293 | use strict vars; | |
294 | use Apache::Constants ':common'; | |
295 | use IO::Compress::Gzip qw(gzip); | |
296 | ||
297 | sub handler { | |
298 | my $r = shift; | |
299 | $r->header_out('Content-Encoding'=>'gzip'); | |
300 | $r->send_http_header; | |
301 | return OK if $r->header_only; | |
302 | ||
303 | gzip $r->filename => '-', Minimal => 1 | |
304 | or return DECLINED ; | |
305 | ||
306 | return OK; | |
307 | } | |
308 | ||
309 | 1; | |
310 | ||
311 | The use of one-shot C<gzip> above just reads from C<< $r->filename >> and | |
312 | writes the compressed data to standard output. | |
313 | ||
314 | Note the use of the C<Minimal> option in the code above. When using gzip | |
315 | for Content-Encoding you should I<always> use this option. In the example | |
316 | above it will prevent the filename being included in the gzip header and | |
317 | make the size of the gzip data stream a slight bit smaller. | |
318 | ||
319 | =head2 Using C<InputLength> to uncompress data embedded in a larger file/buffer. | |
320 | ||
321 | A fairly common use-case is where compressed data is embedded in a larger | |
322 | file/buffer and you want to read both. | |
323 | ||
324 | As an example consider the structure of a zip file. This is a well-defined | |
325 | file format that mixes both compressed and uncompressed sections of data in | |
326 | a single file. | |
327 | ||
328 | For the purposes of this discussion you can think of a zip file as sequence | |
329 | of compressed data streams, each of which is prefixed by an uncompressed | |
330 | local header. The local header contains information about the compressed | |
331 | data stream, including the name of the compressed file and, in particular, | |
332 | the length of the compressed data stream. | |
333 | ||
334 | To illustrate how to use C<InputLength> here is a script that walks a zip | |
335 | file and prints out how many lines are in each compressed file (if you | |
336 | intend write code to walking through a zip file for real see | |
e8796d61 | 337 | L<IO::Uncompress::Unzip/"Walking through a zip file"> ). Also, although |
cd0c0e65 | 338 | this example uses the zlib-based compression, the technique can be used by |
e8796d61 | 339 | the other C<IO::Uncompress::*> modules. |
d54256af PM |
340 | |
341 | use strict; | |
342 | use warnings; | |
343 | ||
344 | use IO::File; | |
345 | use IO::Uncompress::RawInflate qw(:all); | |
346 | ||
347 | use constant ZIP_LOCAL_HDR_SIG => 0x04034b50; | |
348 | use constant ZIP_LOCAL_HDR_LENGTH => 30; | |
349 | ||
350 | my $file = $ARGV[0] ; | |
351 | ||
352 | my $fh = new IO::File "<$file" | |
353 | or die "Cannot open '$file': $!\n"; | |
354 | ||
355 | while (1) | |
356 | { | |
357 | my $sig; | |
358 | my $buffer; | |
359 | ||
360 | my $x ; | |
361 | ($x = $fh->read($buffer, ZIP_LOCAL_HDR_LENGTH)) == ZIP_LOCAL_HDR_LENGTH | |
362 | or die "Truncated file: $!\n"; | |
363 | ||
364 | my $signature = unpack ("V", substr($buffer, 0, 4)); | |
365 | ||
366 | last unless $signature == ZIP_LOCAL_HDR_SIG; | |
367 | ||
368 | # Read Local Header | |
369 | my $gpFlag = unpack ("v", substr($buffer, 6, 2)); | |
370 | my $compressedMethod = unpack ("v", substr($buffer, 8, 2)); | |
371 | my $compressedLength = unpack ("V", substr($buffer, 18, 4)); | |
372 | my $uncompressedLength = unpack ("V", substr($buffer, 22, 4)); | |
373 | my $filename_length = unpack ("v", substr($buffer, 26, 2)); | |
374 | my $extra_length = unpack ("v", substr($buffer, 28, 2)); | |
375 | ||
376 | my $filename ; | |
377 | $fh->read($filename, $filename_length) == $filename_length | |
378 | or die "Truncated file\n"; | |
379 | ||
380 | $fh->read($buffer, $extra_length) == $extra_length | |
381 | or die "Truncated file\n"; | |
382 | ||
383 | if ($compressedMethod != 8 && $compressedMethod != 0) | |
384 | { | |
385 | warn "Skipping file '$filename' - not deflated $compressedMethod\n"; | |
386 | $fh->read($buffer, $compressedLength) == $compressedLength | |
387 | or die "Truncated file\n"; | |
388 | next; | |
389 | } | |
390 | ||
391 | if ($compressedMethod == 0 && $gpFlag & 8 == 8) | |
392 | { | |
393 | die "Streamed Stored not supported for '$filename'\n"; | |
394 | } | |
395 | ||
396 | next if $compressedLength == 0; | |
397 | ||
398 | # Done reading the Local Header | |
399 | ||
400 | my $inf = new IO::Uncompress::RawInflate $fh, | |
401 | Transparent => 1, | |
402 | InputLength => $compressedLength | |
403 | or die "Cannot uncompress $file [$filename]: $RawInflateError\n" ; | |
404 | ||
405 | my $line_count = 0; | |
406 | ||
407 | while (<$inf>) | |
408 | { | |
409 | ++ $line_count; | |
410 | } | |
411 | ||
412 | print "$filename: $line_count\n"; | |
413 | } | |
414 | ||
415 | The majority of the code above is concerned with reading the zip local | |
416 | header data. The code that I want to focus on is at the bottom. | |
417 | ||
418 | while (1) { | |
419 | ||
420 | # read local zip header data | |
421 | # get $filename | |
422 | # get $compressedLength | |
423 | ||
424 | my $inf = new IO::Uncompress::RawInflate $fh, | |
425 | Transparent => 1, | |
426 | InputLength => $compressedLength | |
427 | or die "Cannot uncompress $file [$filename]: $RawInflateError\n" ; | |
428 | ||
429 | my $line_count = 0; | |
430 | ||
431 | while (<$inf>) | |
432 | { | |
433 | ++ $line_count; | |
434 | } | |
435 | ||
436 | print "$filename: $line_count\n"; | |
437 | } | |
438 | ||
439 | The call to C<IO::Uncompress::RawInflate> creates a new filehandle C<$inf> | |
440 | that can be used to read from the parent filehandle C<$fh>, uncompressing | |
441 | it as it goes. The use of the C<InputLength> option will guarantee that | |
442 | I<at most> C<$compressedLength> bytes of compressed data will be read from | |
443 | the C<$fh> filehandle (The only exception is for an error case like a | |
444 | truncated file or a corrupt data stream). | |
445 | ||
446 | This means that once RawInflate is finished C<$fh> will be left at the | |
447 | byte directly after the compressed data stream. | |
448 | ||
449 | Now consider what the code looks like without C<InputLength> | |
450 | ||
451 | while (1) { | |
452 | ||
453 | # read local zip header data | |
454 | # get $filename | |
455 | # get $compressedLength | |
456 | ||
457 | # read all the compressed data into $data | |
458 | read($fh, $data, $compressedLength); | |
459 | ||
460 | my $inf = new IO::Uncompress::RawInflate \$data, | |
461 | Transparent => 1, | |
462 | or die "Cannot uncompress $file [$filename]: $RawInflateError\n" ; | |
463 | ||
464 | my $line_count = 0; | |
465 | ||
466 | while (<$inf>) | |
467 | { | |
468 | ++ $line_count; | |
469 | } | |
470 | ||
471 | print "$filename: $line_count\n"; | |
472 | } | |
473 | ||
474 | The difference here is the addition of the temporary variable C<$data>. | |
475 | This is used to store a copy of the compressed data while it is being | |
476 | uncompressed. | |
477 | ||
478 | If you know that C<$compressedLength> isn't that big then using temporary | |
479 | storage won't be a problem. But if C<$compressedLength> is very large or | |
480 | you are writing an application that other people will use, and so have no | |
481 | idea how big C<$compressedLength> will be, it could be an issue. | |
482 | ||
483 | Using C<InputLength> avoids the use of temporary storage and means the | |
484 | application can cope with large compressed data streams. | |
485 | ||
486 | One final point -- obviously C<InputLength> can only be used whenever you | |
487 | know the length of the compressed data beforehand, like here with a zip | |
488 | file. | |
489 | ||
490 | =head1 SEE ALSO | |
491 | ||
9b5fd1d4 | 492 | L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> |
d54256af PM |
493 | |
494 | L<Compress::Zlib::FAQ|Compress::Zlib::FAQ> | |
495 | ||
496 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, | |
497 | L<Archive::Tar|Archive::Tar>, | |
498 | L<IO::Zlib|IO::Zlib> | |
499 | ||
500 | =head1 AUTHOR | |
501 | ||
502 | This module was written by Paul Marquess, F<pmqs@cpan.org>. | |
503 | ||
504 | =head1 MODIFICATION HISTORY | |
505 | ||
506 | See the Changes file. | |
507 | ||
508 | =head1 COPYRIGHT AND LICENSE | |
509 | ||
cd0c0e65 | 510 | Copyright (c) 2005-2011 Paul Marquess. All rights reserved. |
d54256af PM |
511 | |
512 | This program is free software; you can redistribute it and/or | |
513 | modify it under the same terms as Perl itself. | |
514 |