To run the examples, save bbminfo.zip beside your script and run it from that directory.
Reading the same archive with ZipArchive
This example requires the PHP ZIP extension.
PHP source code
<?php
$zip = new ZipArchive();
$result = $zip->open(__DIR__ . '/bbminfo.zip', ZipArchive::RDONLY);
if ($result !== true) {
throw new RuntimeException('Could not open the archive.');
}
try {
$text = $zip->getFromName('bbminfo/readme.txt', 15);
if ($text === false) {
throw new RuntimeException('Could not read the entry.');
}
echo $text, PHP_EOL;
} finally {
$zip->close();
}
Output
Thousands Lived
PHP zip_entry_read - Syntax
Syntax
zip_entry_read(resource $entry, int $length = 1024): string|false
PHP zip_entry_read - Parameter and return type
Arguments
resource $entry, int $length = 1024
Return value
A string containing up to the requested number of bytes, an empty string at end of file, or false on error.
Example: #1 - PHP zip_entry_read
A student wants to preview a readme before unpacking the course bundle. Open the member, read its bytes, and display the message in the preview.
Inputbbminfo.zip
First output lineFile: bbminfo/readme.txt
PHP source code
<?php
class ZipExample {
public function processZipArchieve() {
$zipHandler = zip_open("./bbminfo.zip");
if (!is_resource($zipHandler)) {
return;
}
while($zipEntry = zip_read($zipHandler)) {
/* Opening a File in a Zip Archive */
$flag = zip_entry_open($zipHandler, $zipEntry, "rb");
if ($flag == true) {
// Read the name of a file
$resourceName = zip_entry_name($zipEntry);
// Read the content of a file
$contents = zip_entry_read($zipEntry);
// Print the file name and its content
echo("File: " . $resourceName . "\n");
echo($contents . "\n");
// close the file entry
zip_entry_close($zipEntry);
} else {
echo("Failed to Open. \n");
}
}
zip_close($zipHandler);
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
Output
File: bbminfo/readme.txt
Thousands Lived without Love, but not without water. So SAVE WATER.
Advertisements
Example: #2 - PHP zip_entry_read
A small preview panel shows just the beginning of a readme. It requests fifteen bytes, which may contain fewer than fifteen complete characters when the text uses multibyte encoding.
Inputbbminfo.zip
First output lineFile: bbminfo/readme.txt
PHP source code
<?php
class ZipExample {
public function processZipArchieve() {
$zipHandler = zip_open("./bbminfo.zip");
if (!is_resource($zipHandler)) {
return;
}
while($zipEntry = zip_read($zipHandler)) {
/* Opening a File in a Zip Archive */
$flag = zip_entry_open($zipHandler, $zipEntry, "rb");
if ($flag == true) {
// Read the content of a file
$contents = zip_entry_read($zipEntry);
// Print the content of a file
echo($contents);
// close the file entry
zip_entry_close($zipEntry);
} else {
echo("Failed to Open. \n");
}
}
zip_close($zipHandler);
}
}
$obj = new ZipExample();
$obj->processZipArchieve();
Output
File: bbminfo/readme.txt
Thousands Lived
Inspect a choir archive
A teacher saves the bytes for the word alto in an uncompressed archive member. Open the member by name and read its text. What does the teacher see?
Givenparts/alto.txt · 4 bytes
Worked resultalto
The archive contains one stored member. Its entry metadata and contents can be inspected independently.