In PHP is it possible to inspect content of a Zip file without extracting its content first?
As found as a comment on http://www.php.net/ziparchive:
The following code can be used to get a list of all the file names in
a zip file.<?php
$za = new ZipArchive();
$za->open('theZip.zip');
for( $i = 0; $i < $za->numFiles; $i++ ){
$stat = $za->statIndex( $i );
print_r( basename( $stat['name'] ) . PHP_EOL );
}
?>
PHP ZipArchive - Search Entry Contents for String
You can use ZipArchive::getFromName() or ZipArchive::getFromIndex() to read the zipped file contents.
$zip = new ZipArchive;
$res = $zip->open("./test.zip");
for( $i = 0; $i < $zip->numFiles; $i++ ){
var_dump($zip->getFromIndex($i));
}
$zip->close();
PHP - read zip file contents without saving on server
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.
Opening PDF file in zip without extracting, using PHP
Use the zip:// wrapper:
You can use it with fopen/fread
or just file_get_contents
:
$pdf = file_get_contents('zip://datacollection.zip#data.pdf');
echo $pdf;
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;
how i can open a file in zip file without extract it by the PHP
From the PHP manual: ZipArchive::getFromName(). This is exactly what you need.
<?php
$z = new ZipArchive();
if ($z->open(dirname(__FILE__) . '/test_im.zip')) {
$string = $z->getFromName("mytext.txt");
echo $string;
}
?>
Best wishes,
Fabian
Access files in zip before extracting them
$zip = new ZipArchive();
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$name=$zip->getNameIndex($i);
$pieces=explode(DS,$name);
if(substr($name, -1)==DS) {
//validate directory structure if desired
}
else {
//validate file
$mime=$finfo->buffer($zip->getFromIndex($i));
}
}
}
Related Topics
How to Include External CSS and Js File in Laravel 5
Create New Xml File and Write Data to It
How to Render Zf2 View Within JSON Response
How to Alias the Name of a Column in Eloquent
How to Execute PHP Code Within JavaScript
Php/Symfony/Doctrine Memory Leak
PHP Simple Foreach Loop with HTML
What's the Difference Between Array_Merge and Array + Array
PHP Cli Getting Input from User and Then Dumping into Variable Possible
Codeigniter Activerecord, Retrieve Last Insert Id
Codeigniter: Try Catch Is Not Working in Model Class
How to Set a Class Attribute to a Symfony2 Form Input
Org.JSON.JSONexception: Value <Br of Type Java.Lang.String Cannot Be Converted to JSONobject