I need to read files from ZIP archives with PHP.
And my zip is not on the disk. It is accessible with http url.
There is the extension ZipArchive . It works fine if a file is on disk . Is it possible to use this extension with some opened resource instead of opening a file by path?
Important note, i can not download a zip file temporary to disk. I can work only with the file remotely.
If not ZipArchive, maybe there are other similar libraries for Zip that can read from stream?
There is simple example which doesn't work.
$zip = new ZipArchive();
$fh = fopen('zipfiles/1.zip','r');
$status = $zip->open($fh);
if ($status !== TRUE) {
exit("Error $status");
}
echo "Number of files: ".$zip->numFiles;
But this one works fine
$zip = new ZipArchive();
$status = $zip->open('zipfiles/1.zip');
if ($status !== TRUE) {
exit("Error $status");
}
echo "Number of files: ".$zip->numFiles;
My Answer:
> Unfortunately, the ZipArchive extension in PHP does not support reading from a stream or resource directly. It requires a file path to open a ZIP archive.
However, you can use other libraries like `ZipStream` or `ZipStream-PHP` which allow you to create ZIP archives from streams or resources. These libraries can be used to read files from a remote ZIP archive without downloading it to disk.
Here is an example using ZipStream-PHP:
php
use PclZip\ZipStream;
$zipStream = new ZipStream();
$zipStream->open('http://example.com/remote_zip.zip');
foreach ($zipStream->getFiles() as $file) {
// Process each file in the ZIP archive
echo $file->getName() . "\n";
}
$zipStream->close();
You can find more information and documentation on how to use ZipStream-PHP on their GitHub repository: https://github.com/maennchen/ZipStream-PHP
Please note that using remote resources in this way may have performance implications and may not be suitable for large ZIP archives or frequent access.
Rate this post
3 of 5 based on 1320 votesComments