in reply to Question: parse info in a Zipped XML document attached in an email
do I have to use a temporary file??
If your zip data is small enough to fit in memory, then you do not have to use a temporary file. Archive::Zip supports reading from a file handle, but the file handle must be seekable.
Since perl 5.8.0, one can open a string. Unfortunately, the resulting file handle is not compatible with Archive::Zip.
#!/usr/bin/perl use strict; use warnings; use Archive::Zip; my $zipfile = "test.zip"; open(ZIP, "<", "$zipfile") or die "$zipfile: $!"; my $zipdata = do { local $/; <ZIP>; }; close(ZIP); open(my $ZIP, "<", \$zipfile) or die "\$zipfile: $!"; my $zip = Archive::Zip->new(); my $status = $zip->readFromFileHandle($ZIP); print "\$status = $status\n"; foreach my $member ($zip->members()) { print "member: " . $member->fileName() . "\n"; } close(ZIP);
produces the following error message:
error: file not seekable at /usr/lib/perl5/site_perl/5.8.8/Archive/Zip/Archive.pm line 437 Archive::Zip::Archive::readFromFileHandle('Archive::Zip::Archi +ve=HASH(0x88d4c54)', 'GLOB(0x83d9c28)') called at ./test.pl line 16 $status = 2
But IO::String gives compatible file handles, so you can use something like the following.
#!/usr/bin/perl use strict; use warnings; use Archive::Zip; use IO::String; my $zipfile = "test.zip"; open(ZIP, "<", "$zipfile") or die "$zipfile: $!"; my $zipdata = do { local $/; <ZIP>; }; close(ZIP); my $io = IO::String->new($zipdata); my $zip = Archive::Zip->new(); my $status = $zip->readFromFileHandle($io); print "\$status = $status\n"; foreach my $member ($zip->members()) { print "member: " . $member->fileName() . "\n"; } close(ZIP);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Question: parse info in a Zipped XML document attached in an email
by lihao (Monk) on Nov 20, 2008 at 20:49 UTC |