Core\Zip::read
Core\Zip::read(
Core\Bytes::Source $archive,
string $name,
uint $maxBytes = 67108864,
uint $maxRatio = 1000,
): tainted bytesReads one file out of a zip archive and returns its content as bytes, already decompressed.
You name the file exactly as Core\Zip::entries lists it. A name that is not in the archive throws a ParseError. The whole archive is checked first, so an archive with one unsafe name throws even when the file you name is safe.
The archive can be bytes, an open file, an uploaded Core\Request\Part or a Core\Request\BodyStream. read reads the table of contents and then only the file you name. An upload is kept in memory while it is read.
A small file in an archive can decompress to gigabytes. This is called a zip bomb. So read has two limits: the most bytes the file may have, and the most bytes for each byte it takes in the archive. A file over either limit throws a ParseError. You can make both limits smaller, but not larger than the server's settings.
The content is tainted bytes, because the person who made the archive chose it.
The examples below read two files from an archive file, show both limits, and import the text files of an upload.
Parameters
$archiveCore\Bytes::SourceThe archive, asbytesor any sourceentriestakes. It is checked the same way as inentries. An unsafe entry anywhere in the archive makes this call throw too, whichever entry you read.$namestringThe entry to read. It is compared with the names in the archive exactly, and it is never treated as a path. A name thatentriesreturned works as it is.$maxBytesuintdefault 67108864The largest output this call may produce, in bytes. It can be lower than[limits] max_decompressed, but not higher.0means zero bytes, not unlimited.$maxRatiouintdefault 1000The largest output per byte of the compressed entry. This is the second part of the size limit, because a zip bomb is small before it is decompressed.
Return value
Returns tainted bytes. All the bytes of the entry. The result is never cut short. An entry larger than either limit throws an error. The bytes are tainted, like the names, because somebody else wrote the archive.
Errors
ParseErrorEvery case whereentriesthrows. It is also thrown when no entry has that name, or the entry uses a compression method this class cannot read. Other causes are a broken compressed stream, a wrong CRC-32, or output above either limit.
Examples
Read one file from an archive
<?nvs// Shows how to read one file out of a zip archive file.
// A small archive, written as base64 text so it fits in this file. It contains// `readme.txt` and `data/sales.csv`, both compressed.bytes $archive = Core\Encoding::fromBase64('UEsDBBQAAAAIAAAAIVykiLt8KQAAACcAAAAKAAAAcmVhZG1lLnR4dAvJSMzLVqjML1VIyy9SSMkvz8vJT0zJzEtXKMnILFYoSi3ILyrR4wIAUEsDBBQAAAAIAAAAIVx17X2pLgAAADIAAAAOAAAAZGF0YS9zYWxlcy5jc3bLzc8rydApyS9JzOHySswrTSyq1DE0MjDgcktNKoLwjE0NuHwTi5IzdAwNgRIAUEsBAhQAFAAAAAgAAAAhXKSIu3wpAAAAJwAAAAoAAAAAAAAAAAAAAKSBAAAAAHJlYWRtZS50eHRQSwECFAAUAAAACAAAACFcde19qS4AAAAyAAAADgAAAAAAAAAAAAAApIFRAAAAZGF0YS9zYWxlcy5jc3ZQSwUGAAAAAAIAAgB0AAAAqwAAAAAA');
// The program writes the archive to a file first, so that it has one to open.string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'report.zip');Core\IO::write($path, $archive);Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
// `Core\Zip::read` takes the open file and returns one file's content, decompressed.// This prints 39, the size of `readme.txt` in bytes.tainted bytes $readme = Core\Zip::read($file, 'readme.txt');echo Core\Bytes::length($readme), "\n";
// A file in a folder is read by its full name, exactly as `Core\Zip::entries`// lists it. This prints the four lines of the CSV file.tainted bytes $sales = Core\Zip::read($file, 'data/sales.csv');echo $sales as string;
$file->close();39month,totalJanuary,1200February,1350March,1100A file larger than the limit
<?nvs// Shows what happens when a file in an archive is larger than you allow.
// This archive contains one file, `log.txt`. It takes 148 bytes in the// archive and 60000 bytes after decompression.bytes $archive = Core\Encoding::fromBase64('UEsDBBQAAAAIAAAAIVwRHoollAAAAGDqAAAHAAAAbG9nLnR4dO3HNwEAIBAEsB0ViOME8BT9yGBJtlTmydp9pW5GK1VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVXV331QSwECFAAUAAAACAAAACFcER6KJZQAAABg6gAABwAAAAAAAAAAAAAApIEAAAAAbG9nLnR4dFBLBQYAAAAAAQABADUAAAC5AAAAAAA=');
// The third argument is the most bytes the file may have after decompression.// 60000 bytes are more than 1000, so this throws a `ParseError`.try { Core\Zip::read($archive, 'log.txt', 1000); echo "read\n";} catch (ParseError $error) { echo "too large\n";}
// The fourth argument is the most bytes of output for each byte in the archive.// 148 bytes at 100 each allow 14800 bytes, so this throws too.try { Core\Zip::read($archive, 'log.txt', 1000000, 100); echo "read\n";} catch (ParseError $error) { echo "compressed too much\n";}
// With the default limits the file is read. This prints 60000.echo Core\Bytes::length(Core\Zip::read($archive, 'log.txt')), "\n";
// A name that is not in the archive throws a `ParseError` too.try { Core\Zip::read($archive, 'missing.txt');} catch (ParseError $error) { echo "no such file\n";}too largecompressed too much60000no such fileImport product texts from an upload
<?nvs// Shows how to import every text file from an uploaded archive, with a size limit for each file.
class Import{ // Returns one line per file: its name and its text. // The text comes from the upload, so each line is a `tainted string`. public static function texts(bytes $upload): array<tainted string> { array<tainted string> $lines = []; foreach (Core\Zip::entries($upload) as tainted string $name) { // A folder name ends with `/`. A folder has no content to read. if (Core\Str::endsWith($name, '/')) { continue; } // Each file may have at most 10000 bytes. A larger file throws a // `ParseError`, and the line says "too large" instead. try { tainted bytes $content = Core\Zip::read($upload, $name, 10000); $lines[] = $name . ': ' . Core\Str::trim($content as string); } catch (ParseError $error) { $lines[] = $name . ': too large'; } } return $lines; }}
// This upload contains the folder `products/` and three text files in it.bytes $upload = Core\Encoding::fromBase64('UEsDBBQAAAAAAAAAIVwAAAAAAAAAAAAAAAAJAAAAcHJvZHVjdHMvUEsDBBQAAAAIAAAAIVyWa17GIwAAACEAAAASAAAAcHJvZHVjdHMvY2hhaXIudHh0c1Qoz89PSc1TSM5IzCxSKM8syVBIVCjOTytRKE5NLNHjAgBQSwMEFAAAAAgAAAAhXEwqV0EbAAAAGQAAABEAAABwcm9kdWN0cy9kZXNrLnR4dHNUSEktzlYozyzJUCgpz1dIKUosTy0q1uMCAFBLAwQUAAAACAAAACFcDWTkchwAAAAaAAAAEQAAAHByb2R1Y3RzL2xhbXAudHh0c1TIScwtUCjPLMlQSFQoTyzKVcjJTM8o0eMCAFBLAQIUABQAAAAAAAAAIVwAAAAAAAAAAAAAAAAJAAAAAAAAAAAAAADtQQAAAABwcm9kdWN0cy9QSwECFAAUAAAACAAAACFclmtexiMAAAAhAAAAEgAAAAAAAAAAAAAApIEnAAAAcHJvZHVjdHMvY2hhaXIudHh0UEsBAhQAFAAAAAgAAAAhXEwqV0EbAAAAGQAAABEAAAAAAAAAAAAAAKSBegAAAHByb2R1Y3RzL2Rlc2sudHh0UEsBAhQAFAAAAAgAAAAhXA1k5HIcAAAAGgAAABEAAAAAAAAAAAAAAKSBxAAAAHByb2R1Y3RzL2xhbXAudHh0UEsFBgAAAAAEAAQA9QAAAA8BAAAAAA==');
// This prints three lines, one for each product.foreach (Import::texts($upload) as tainted string $line) { echo $line, "\n";}products/chair.txt: A wooden chair with a soft seat.products/desk.txt: A desk with two drawers.products/lamp.txt: A lamp with a warm light.