I'm trying to pull some data from a zip file on a remote HTTP server. To this end I thought I could simply UserAgent it over, print it to a File::Temp object and let Archive::Zip's readFromFileHandle suck the data into an object that I can pick out what I wanted. I seem to be having an issue getting methods from Archive::Zip to play nice with a File::Temp filehandle?

ActiveState 5.12.4

use File::Temp; use LWP; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use Data::Dumper; my $url = 'http://keep.drewhead.org/test.zip'; my $file = '../test.zip'; # a local copy of $url ###################################################################### # This works { my $zip = Archive::Zip->new(); my $zip_err = $zip->read( $file ); unless ($zip_err == AZ_OK ) { die "Archive::Zip read error on filehandle: $zip_err\n"; } foreach my $member ($zip->members()) { my $fileName = $member->fileName(); print "fileName = $fileName\n"; my $content = $member->contents(); print "contents $fileName = ".Dumper($content); } } ###################################################################### # This doesn't { my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get($url); my $tmp = File::Temp->new(); if ($response->is_success) { print $tmp $response->decoded_content; print "Wrote ".$tmp->filename()."\n"; } else { die "error ".$response->status_line; } unless (-B $tmp) { die "Did not get a filehandle\n"; } my $zip = Archive::Zip->new(); my $zip_err = $zip->readFromFileHandle( $tmp ); unless ($zip_err == AZ_OK ) { die "Archive::Zip read error on filehandle: $zip_err\n"; } foreach my $member ($zip->members()) { my $fileName = $member->fileName(); print "fileName = $fileName\n"; my $content = $member->contents(); print "contents $fileName = ".Dumper($content); } } ###################################################################### # This works but I'm replacing File::Temp functionality { my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get($url); my $tempfile = 'C:\Documents and Settings\ddowling\Local Settings\Te +mp\drewtmp1'; open(my $fh, ">$tempfile") || die "Unable to open $tempfile:$!\n"; binmode($fh); print $fh $response->decoded_content; close($fh); my $zip = Archive::Zip->new(); my $zip_err = $zip->read( $tempfile ); unless ($zip_err == AZ_OK ) { die "Archive::Zip read error on filehandle: $zip_err\n"; } foreach my $member ($zip->members()) { my $fileName = $member->fileName(); print "fileName = $fileName\n"; my $content = $member->contents(); print "contents $fileName = ".Dumper($content); } }
The first block there does what I expect the second block to do: print out the file names and their contents. However the second block complains:
Wrote C:\DOCUME~1\ddowling\LOCALS~1\Temp\mhtbVVHEN5 fileName = test2.txt fileName = test1.txt IO error: Can't open C:\DOCUME~1\ddowling\LOCALS~1\Temp\mhtbVVHEN5 : I +nvalid argument at C:/Perl/lib/Archive/Zip/FileMember.pm line 40 Archive::Zip::FileMember::_openFile('Archive::Zip::ZipFileMember=H +ASH(0x1c40c14)') called at C:/Perl/lib/Archive/Zip/FileMember.pm line + 30 Archive::Zip::FileMember::fh('Archive::Zip::ZipFileMember=HASH(0x1 +c40c14)') called at C:/Perl/lib/Archive/Zip/ZipFileMember.pm line 384 Archive::Zip::ZipFileMember::rewindData('Archive::Zip::ZipFileMemb +er=HASH(0x1c40c14)') called at C:/Perl/lib/Archive/Zip/Member.pm line + 960 Archive::Zip::Member::contents('Archive::Zip::ZipFileMember=HASH(0 +x1c40c14)') called at C:/Documents and Settings/ddowling/Desktop/eoc_ +cat_reason/example/zipseek.pl line 52 IO error: Can't open C:\DOCUME~1\ddowling\LOCALS~1\Temp\mhtbVVHEN5 : I +nvalid argument at C:/Perl/lib/Archive/Zip/FileMember.pm line 40 Archive::Zip::FileMember::_openFile('Archive::Zip::ZipFileMember=H +ASH(0x1c41184)') called at C:/Perl/lib/Archive/Zip/FileMember.pm line + 30 Archive::Zip::FileMember::fh('Archive::Zip::ZipFileMember=HASH(0x1 +c41184)') called at C:/Perl/lib/Archive/Zip/ZipFileMember.pm line 384 Archive::Zip::ZipFileMember::rewindData('Archive::Zip::ZipFileMemb +er=HASH(0x1c41184)') called at C:/Perl/lib/Archive/Zip/Member.pm line + 960 Archive::Zip::Member::contents('Archive::Zip::ZipFileMember=HASH(0 +x1c41184)') called at C:/Documents and Settings/ddowling/Desktop/eoc_ +cat_reason/example/zipseek.pl line 52
If I say that it appears that the contents() method is stringifing the Temp:File object filehandle am I reading this correctly? How do I make it stop doing that?

I realize I could just do what I have done in the 3rd block and eliminate File::Temp, but I'm trying to understand what's going on here... also I already have a module that is doing the URL retrieval and tmpfile storage for other non zip processes that I kinda what to continue using. File::Temp is nice to use if you don't want to have to worry about manually unlinking things at the end.

Feel free to bang against $url, zip on my hosting with nonsense data.


In reply to Reading data from ZIP data in a File::Temp filehandle problem by drewhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.