PHP zip_read - Syntax
zip_read(resource $ziphandler): mixed
The PHP zip_read() function is used to read an entity (a file or a directory) present in an opened Zip Archive Recsource Handler. It returns the Sub Resource Handler of a single entity present in a Main Resource Handler only if Success otherwise a PHP Warning.
zip_read(resource $ziphandler): mixed
resource $ziphandler |
Specifies the Resource Handler of an already opened Zip archive file (*Required) |
---|---|
Return mixed $zipEntry |
Returns the Sub Resource Handler of a single entity present in a Main Resource Handler otherwise a PHP Warning. |
Open the zip archieve using zip_open library method, once the method returns the zip handler then read the files and print their names.
<?php
class ZipExample {
public function processZipArchieve() {
$zipHandler = zip_open("./bbminfo.zip");
if(is_resource($zipHandler)) {
while($zipEntry = zip_read($zipHandler)) {
$fileName = zip_entry_name($zipEntry);
echo("File Name: " . $fileName);
echo "\n" ;
}
zip_close($zipHandler);
} else {
echo("Failed to Open. Error Code: " . $zipHandler);
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
Read all file and directory information in a zip archieve using zip_open library method in rb mode, Print the file name once the file gets opened successfully otherwise an error message.
<?php
class ZipExample {
public function processZipArchieve() {
$zipHandler = zip_open("./bbminfo-nested-folder.zip");
if(is_resource($zipHandler)) {
while($zipEntry = zip_read($zipHandler)) {
/* Opening a File in a Zip Archive */
$flag = zip_entry_open($zipHandler, $zipEntry, "rb");
if ($flag == true) {
$resourceName = zip_entry_name($zipEntry);
if(substr($resourceName, -1) == "/") {
echo("Directory: " . $resourceName . " opened Successfully. \n");
} else {
echo("File: " . $resourceName . " opened Successfully. \n");
}
} else {
echo("Failed to Open. \n");
}
}
zip_close($zipHandler);
} else {
echo("Failed to Open. Error Code: " . $zipHandler);
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
Try to open the zip archieve using zip_open library method with an invalid file or invalid file path, it will throw an PHP warning.
<?php
class ZipExample {
public function processZipArchieve() {
/* Invalid zip archieve or an invalid file path */
$zipHandler = zip_open("./invaid.zip");
while($zipEntry = zip_read($zipHandler)) {
$fileName = zip_entry_name($zipEntry);
echo("File Name: " . $fileName);
echo "\n" ;
}
}
}
$obj = new ZipExample();
$obj->processZipArchieve();