PHP
Chilkat2 PHP
Unzip a ZIP Entry Directly into a BinData Object Using ZipEntry.UnzipToBd
See more Zip Examples
This example demonstrates how to use the ZipEntry.UnzipToBd method to inflate a ZIP entry directly into a BinData object.
The entry contents are uncompressed entirely in memory without creating a file on disk.
This is useful when:
- Processing ZIP entry data entirely in memory
- Avoiding temporary filesystem files
- Working with binary files such as images, PDFs, or certificates
- Passing uncompressed ZIP data directly to other APIs
The example opens a ZIP archive, locates a PDF entry, inflates it into a BinData object, and then writes the uncompressed bytes to a new file.
Suppose the ZIP archive contains:
docs/report.pdf The entry is uncompressed directly into memory before optionally being saved to:
qa_output/report.pdf
Chilkat PHP Downloads
<?php
use Chilkat2\BinData;
use Chilkat2\Zip;
use Chilkat2\ZipEntry;
$success = false;
$zip = new Zip();
// Open an existing ZIP archive.
try {
$zip->OpenZip('qa_data/zips/documents.zip');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// Locate the PDF entry within the ZIP archive.
$entry = new ZipEntry();
$success = $zip->EntryOf('docs/report.pdf', $entry);
if (!$success) {
echo "ZIP entry not found.\n";
$zip->CloseZip();
exit;
}
// ------------------------------------------------------------
// Inflate the ZIP entry directly into a BinData object.
//
// The uncompressed bytes are stored entirely in memory.
//
$pdfData = new BinData();
try {
$entry->UnzipToBd($pdfData);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'Uncompressed size = ', $pdfData->NumBytes, "\n";
echo "\n";
// ------------------------------------------------------------
// Optionally save the uncompressed bytes to a file.
//
try {
$pdfData->WriteFile('qa_output/report.pdf');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$zip->CloseZip();
echo "PDF extracted successfully.\n";