in reply to Re: Need to parse a file within a zip fille
in thread Need to parse a file within a zip fille

Taking toolic's suggestion, I did this, stealing fairly directly from the Synopsis, and as seen below, to good effect.

Three points of note:

  1. When using the text file technique, the lines appear to come back either pre-chopped or pre-chomped;
  2. The internal line-counter feature is 1-based, not zero-based, and;
  3. As written, it may have added an extra line to the end of the file. I have examined neither the accuracy of this supposition, nor its cause (if real). That is left as an exercise for the reader. If it is real, that might be due to the way the while()loop is constructed for reading the file.

#!/usr/bin/perl use strict; use Archive::Zip; use Archive::Zip::MemberRead; my ($zipfnm, $mbrfnm, @arglst) = @ARGV; { if (-e $zipfnm) { # ZIP file exists. Proceed. my $zipobj = Archive::Zip->new($zipfnm); # ZIP obje +ct pointer # Adjust Windows specifications my $mbranm = $mbrfnm; $mbranm =~ s/\\/\//g; # Proceed. my $mbrhan = Archive::Zip::MemberRead->new($zipobj, $mbranm) +; # Synopsis does not specify error handling return; # Left as an exercise for the reader :-) # OP indicates reading lines from the file, so use the text +file technique while (defined(my $inpbuf = $mbrhan->getline())) { # Note the line read is already chopped or chomped (!!) print "Line " . $mbrhan->input_line_number . ": [$inpbu +f]\n"; } } } exit; __END__

Results:

D:\PerlMonks>"C:\App\7-Zip\7z.exe" l -tzip test.zip 7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 Listing archive: test.zip -- Path = test.zip Type = zip Physical Size = 17540 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ----------------- +------- 1995-03-03 22:42:08 ....A 24361 16886 binary.dat 2015-03-10 15:40:58 ....A 22 22 test.dat 2015-03-10 15:41:09 ....A 28 28 test2.dat 2015-04-23 19:41:25 ....A 82 62 test3.txt ------------------- ----- ------------ ------------ ----------------- +------- 24493 16998 4 files, 0 folder +s D:\PerlMonks>type test3.txt This is line one. This is line two. This is line four. The third was blank. D:\PerlMonks>zipreadfile1.pl test.zip test3.txt Line 1: [This is line one.] Line 2: [This is line two.] Line 3: [] Line 4: [This is line four. The third was blank.] Line 4: [] D:\PerlMonks>