How to read a single file inside a zip archive

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;

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.

Tags:

Php

Zip

Zlib

Gzip