PHP - read zip file contents without saving on server - php

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.

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.)

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.

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:
Make a copy of the remote zip file to a temporary folder.
Use ZipArchive to parse the local copy of the zip file.
Read information in your feed.xml file.
Close and everything and delete the local copy of the zip file.

Related

Zip file download system laravel

I'm using Laravel 8
I've uploaded a zip file then want to download it. But here is a problem. After file download, it's not extracting.
Here is my code for download zip file:
$product = Product::where('id',1)->first();
$file = $product->main_file;
return response()->download($file,'filename.zip');
But, if I manually copy this zip file from my project then extract it, it's extracting fine.
Or, if I doing this: return Redirect()->to($file); It also downloads the file and extracts well without any problem. But, it's not a proper way to download. Isn't it?
So, What can I do now?
if you're trying to download via a link try this:
Download
but file name should be saved somewhere.

How to read a single file inside a remote zip archive (url with zip)

I would like to read zip file with TXT file inside, but my zip exists on different url, not on the local machine.
I tried both ways without success.
$result = file_get_contents('zip://http://www.abcde.com/12345.zip#1234.txt');
or
$fp = fopen('zip://http://www.abcde.com/12345.zip#1234.txt', 'r');
Any idea how to do it?
The best solution is to copy the zip from remote to local and then it should work better.

error while downloading zip archive from server

php ZipArchive can't open zip file that gets downloaded from a server.
I have a zip file that I created with winrar and uploaded to my server, file location is here: http://myserver.com/uploads/test.zip It's a valid zip file
Here is my code
When I run this code all I get is
error: Not a zip archive.
It opens the file and extracts it but complains that file already exists even if I delete file.
the file that I get when I access http://myserver.com/update/test.zip get corrupted and I have no idea why, this same code that I pasted above worked just a week ago.
The problem is actually the .zip file. I downloaded it and I cannot open the file with WinZip. Please try to make a new archive and upload it again on your server.
The code looks okay so I think it just the file that is the problem.
UPDATE #1:
The .zip file is now correct. Try to delete the files before you download and extract the new files. You can use unlink("uploads/update.zip") maybe you also need to clear the uploads/temp directory first.
UPDATE #2:
The download worked now for me. Try to add this header:
header("Content-Transfer-Encoding: Binary");
Also don't forget to close the ZipArchive after extracting:
$zip->close();

PHP Importing XML feed packed in a zip file

I want to automate the following:
Once a day my cronjob starts a PHP script that obtains a zipped XML file from an URL.
What would be the best way to handle this? Is there any way to directly read the XML file from within the zip file?
Right now, i'm just downloading the zipped file to the server and manually unpacking it later that day.
Any ideas? All suggestions are very much welcome.
You can use PHP's ZipArchive coupled with cURL to download and read the zip file.
Also, the ZipArchive class has a method called getStream which allows you to then use fread to access the contents without explicitly extracting the file.
The only problem I see if that the zip does have to be saved somewhere for PHP's library to read it. But, given you're already doing this, it shouldn't be an issue.
If you need an example, leave me a comment and I can write on up.
There is a collection of zip-related functions that can be used in PHP.
The problem with these is that it requires the compressed file to exist on the server (not just loaded from an external server somewhere using, for example, $file = file($url);).
If you were to save the file to your server then you could use $zip = zip_open($filename) and zip_read($zip) to process the zip file.

adding remote files to a zip file

Is there a way to add files to a zip file from another server with php's zip extension? ie.
addFile(array('localfile.txt,'http://www.domain.com/remotefile.txt'))
//(that obviously does not work)
I suppose I can download the files to a temporal folder and then add them to the zip file, but I was looking for a more automated solution or a function already made
use file_get_contents() and ZipArchive::addFromString()
$zipArchiveInstance->addFromString($filename, file_get_contents($mediaUrl));
This writes the contents fetched remotely straight into your php object (no need to write/read temp file)
It's not hard to read remote files in PHP.
file_get_contents("http://example.com/remote.txt");
Or to copy them locally:
copy("http://example.com/remote.txt", "/tmp/local.txt");
Whichever way you do it, the contents are going to have to be transferred to a local temp folder or memory before you can do anything with them.
Fetch them with cURL, add them from TEMP directory.

Categories