Reputation: 1751
I have a remote ZIP file, and I need to read the content of a file located in the zip file, without having to copy the zip file to our server. There should be some solution to solve this, but I can't find it.
Example:
URL - http://example.com/feed.zip and
File in the ZIP Archive is feed.xml
I need to read the content of feed.xml
and save it in one variable. Thats it.
I have try with $z = new ZipArchive();
and open, but open can only read files from the server and not remote files.
Upvotes: 1
Views: 4933
Reputation: 22174
I had the same problem and the best I could get was saving zip content to temp file and using ZipArchive
to get content of zipped file:
public static function unzip($zipData) {
// save content into temp file
$tempFile = tempnam(sys_get_temp_dir(), 'feed');
file_put_contents($tempFile, $zipData);
// unzip content of first file from archive
$zip = new ZipArchive();
$zip->open($tempFile);
$data = $zip->getFromIndex(0);
// cleanup temp file
$zip->close();
unlink($tempFile);
return $data;
}
This is basically implementation of @mauris suggestion.
Upvotes: 3
Reputation: 145482
A super lazy solution would be:
$feed = `wget http://example.com/feed.zip -qO- | unzip - -p feed.xml`;
Note that the url should be escaped (escapeshellarg) if it's not a fixed string.
But you could also investigate other ZIP classes than the built-in ZipArchive. PEAR or PclZip might allow to read from streams without workarounds. (But php://memory or a string stream wrapper might be feasible as well, if you're really bent on eschewing temporary files.)
Upvotes: 5
Reputation: 43619
That requires the concept of temporary files. Technically speaking: yes, you are going to make a temporary copy of the remote zip file that is locally available because your ZipArchive
does not work on remote zip files at all.
Following these steps to implement temporary copy should help you:
ZipArchive
to parse the local copy of the zip file.Upvotes: 2