I'm trying to create a PHP script that receives and extracts a zip file. The first thing I do when i get the file is to try to extract the files inside to see if everything is okay and the folder structure of the zip is as it should. If all goes well, the script goes on and extracts the files. The problem is here. It seems that if I call zip_entry_read()
once on a zip entry I get the content, but if I call it again on the same zip entry reference it returns nothing. The meta data is still there. zip_entry_name()
, size and all that returns the correct data but not the content. Why oh why ?
Here's 开发者_开发知识库and example.
$target = zip_open("file.zip");
while (false !== ($entry = zip_read($target)) {
// ... some code here
$name = zip_entry_name($entry); // returns filename.txt
$filesize = intval(zip_entry_filesize($entry)); // returns the filesize
$data = zip_entry_read($entry, $filesize); // returns the content of the file
$name = zip_entry_name($entry); // returns the same filename.txt
$filesize = intval(zip_entry_filesize($entry)); // returns the same filesize
$data = zip_entry_read($entry, $filesize); // ---> returns NOTHING <---
// .. some more code here
}
Thank you.
I ran a quick test, and the suggested solution (closing and opening the entry again) dosn't seem to work. The second execution still returns an empty string. Anyway since the zip_entry_read function return a simple string I would simply copy it in another variable, if it's not a problem.
[UPDATE] Well, if copying the value is not an option, you can try the OO version of the ZipArchive class (it's the object oriented version of the zip library). Something like the code below works and returns two times the desired zip entry. Maybe you can tailor it to your needs.
target = new ZipArchive();
$target->open("file.zip");
for($i = 0; $i < $target->numFiles; $i++)
{
$stat = $target->statIndex($i);
print_r($stat);
$data = $target->getFromIndex($i);
print_r($data);
echo "<br />Control<br />";
$data = $target->getFromIndex($i);
print_r($data);
echo "<br />Control<br />";
}
精彩评论