gotsilk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, so my problem is i have a test1.txt within the zip file test.zip. i want to be able to go and open the file within the zip file and go to a specific line and copy those lines into an output file. i figured i can just put every line into and array and just go to the specific spot in the array and and print the lines out that way, but the main issue is im not sure how to go about opening the file within the zip file. i dont have much right now bc i have been googling without any clear cut way to do it.
use strict; use warnings; use Archive::Zip; my $zip = Archive::Zip->new(); $zip->read('C:\Users\user1\Desktop\Stuff\perltooltesting\test.zip') or + die "could not open file $! \n"; my $fh = Archive::Zip::Read->new($zip,'test.txt');
need help with finishing this. thanks so much!

Replies are listed 'Best First'.
Re: Need to parse a file within a zip fille
by toolic (Bishop) on Apr 23, 2015 at 19:08 UTC

      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>

      i actually made some progress on my own, but still not there yet
      use warnings; use Archive::Zip; my $zip = Archive::Zip->new('test.zip'); # my $fh = $zip->memberNamed( { $zip => 'test1.txt'} ); my @members = $zip->memberNames(); # foreach my $member (@members) { # print $member."\n"; # } open OUT,">kiop_data.txt" or die "Cant open kiop_data.txt : $! \n "; open my $file,"<$fh" or die "cant open \'$fh\' : $! \n"; my $lines = @$file[196609]; # print $Lines[196609]."\n"; close IN; close OUT;
      now i know it sees the files within the zip i just need to open that particular file up and load every line in an array.

        I would probably do something like this although I don't know what kind of stress on the machine this would do since it's loading it into the buffer and not sure how it would react with a file that large


        #!/usr/bin/perl use strict ; use warnings ; use IO::Uncompress::Unzip qw(unzip $UnzipError) ; my $input = 'test.zip'; my $output ; unzip $input => \$output, Name => "test1.txt" or die "unzip failed: $U +nzipError\n"; my @lines = split("\n",$output); print $lines[196609];