Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a single file inside a zip archive

Tags:

php

zip

gzip

zlib

I need to read the content of a single file, "test.txt", inside of a zip file. The whole zip file is a very large file (2gb) and contains a lot of files (10,000,000), and as such extracting the whole thing is not a viable solution for me. How can I read a single file?

like image 670
e-info128 Avatar asked May 02 '12 19:05

e-info128


People also ask

Can you extract one file from a ZIP file?

Do one of the following: To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.

How do you view the contents in a zipped file?

Also, you can use the zip command with the -sf option to view the contents of the . zip file. Additionally, you can view the list of files in the . zip archive using the unzip command with the -l option.

How do I unzip a selected file?

Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.


1 Answers

Try using the zip:// wrapper:

$handle = fopen('zip://test.zip#test.txt', 'r');  $result = ''; while (!feof($handle)) {   $result .= fread($handle, 8192); } fclose($handle); echo $result; 

You can use file_get_contents too:

$result = file_get_contents('zip://test.zip#test.txt');  echo $result; 
like image 197
Rocket Hazmat Avatar answered Sep 23 '22 01:09

Rocket Hazmat