Run `head` on a text file inside a zipped archive without unpacking the archive
The unzip
command line utility has a -p
option which dumps a file to standard out. Just pipe that into head
and it'll not bother extracting the whole file to disk.
Alternatively, from perldoc IO::Compress::Zip
:
my ($status, $bufferRef);my $member = $zip->memberNamed( 'xyz.txt' );$member->desiredCompressionMethod( COMPRESSION_STORED );$status = $member->rewindData();die "error $status" unless $status == AZ_OK;while ( ! $member->readIsDone() ){ ( $bufferRef, $status ) = $member->readChunk(); die "error $status" if $status != AZ_OK && $status != AZ_STREAM_END; # do something with $bufferRef: print $$bufferRef;}$member->endRead();
Modify to suit, i.e. by iterating over the file list $zip->memberNames()
, and only reading the first few lines.
Python's zipfile.ZipFile
allows you to access archived files as streams via ZipFile.open()
. From there you can process them as necessary.