44

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?

1
  • 12
    This question is not actually stupid. I found myself here while googling for 'extract one file from zip php'. It is strange it has votes down.
    – ivkremer
    Feb 2, 2014 at 1:09

2 Answers 2

63

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;
11
  • how to tell if the file exists? if(!@$test = file_get_contents(... ?
    – e-info128
    May 2, 2012 at 19:29
  • @user1243068: I'm not sure. That should work. You can use is_file to check if the .zip exists. As for the file inside the zip, I'm not sure.
    – gen_Eric
    May 2, 2012 at 19:31
  • 2
    It also works with file, like $content=file('zip://test.zip#test.txt'); Jan 15, 2016 at 12:05
  • 3
    @Sami: The phar:// wrapper seems to support this. file_get_contents('phar://yourfile.tar.gz/path/to/file.txt'); See: stackoverflow.com/a/4878849
    – gen_Eric
    Jul 12, 2016 at 21:23
  • 1
    @RocketHazmat - have any idea how to do this with a remote https file? Mar 11, 2017 at 22:10
4

Please note @Rocket-Hazmat fopen solution may cause an infinite loop if a zip file is protected with a password, since fopen will fail and feof fails to return true.

You may want to change it to

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

This solves the infinite loop issue, but if your zip file is protected with a password then you may see something like

Warning: file_get_contents(zip://file.zip#file.txt): failed to open stream: operation failed

There's a solution however

As of PHP 7.2 support for encrypted archives was added.

So you can do it this way for both file_get_contents and fopen

$options = [
    'zip' => [
        'password' => '1234'
    ]
];

$context = stream_context_create($options);
echo file_get_contents('zip://file.zip#file.txt', false, $context);

A better solution however to check if a file exists or not before reading it without worrying about encrypted archives is using ZipArchive

$zip = new ZipArchive;
if ($zip->open('file.zip') !== TRUE) {
    exit('failed');
}
if ($zip->locateName('file.txt') !== false) {
    echo 'File exists';
} else {
    echo 'File does not exist';
}

This will work (no need to know the password)

Note: To locate a folder using locateName method you need to pass it like folder/ with a forward slash at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.