Core\Zip::extract
Core\Zip::extract(
Core\Bytes::Source $archive,
string $destination,
uint $maxBytes = 67108864,
uint $maxRatio = 1000,
): uintWrites every file of a zip archive into a folder and returns the number of files it wrote. The folder and every folder the archive names inside it are created if they do not exist.
On a server, a program may write only in the folders that Core\IO::createTemporaryDirectory made for it. Any other folder needs the fs.read and fs.write capabilities.
Every name in the archive is checked before anything is written. An archive with one unsafe name, such as ../config.ini, throws a ParseError and writes no file. A name that already exists on the disk throws an IOError.
The archive can be bytes, an open file, an uploaded Core\Request\Part or a Core\Request\BodyStream. extract reads one file of the archive at a time. An upload is kept in memory while it is read.
A small archive can decompress to gigabytes, which is called a zip bomb. So extract has the same two limits as Core\Zip::read, counted over all files together. A file over them throws a ParseError, and the files written before it stay on the disk.
The examples below unpack an archive file, show the limit for the whole archive, and install themes that users 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 before it writes anything.$destinationstringThe directory to write the files to. It is created if it does not exist. The call needsfs.writeand alsofs.readfor it, because Novis reads the directories above a name to find where it points.$maxBytesuintdefault 67108864The largest output this call may produce for the whole archive, in bytes. It is not a limit per entry. Many small entries can add up to an attack too. It can be lower than[limits] max_decompressed, but not higher.$maxRatiouintdefault 1000The largest output per byte of a compressed entry. This limit is per entry, because each entry is decompressed on its own.
Return value
Returns uint. The number of files written. A directory entry is created, but it is not counted. So the number is the number of new files.
Errors
ParseErrorEvery case whereentriesthrows. It is also thrown for a wrong CRC-32, or for output above either limit, per entry or for the whole archive.RuntimeErrorfs.readorfs.writeis not granted for the destination. It is also thrown when a directory inside it points outside it. This happens when somebody creates a link while the files are written.IOErrorCreating or writing a file failed. A file with the name of an entry may already exist, becauseextractnever overwrites a file. Or the disk failed. The safety checks of this class throw aParseErroror aRuntimeErrorinstead.
Examples
Unpack an archive into a folder
<?nvs// Shows how to write every file of a zip archive file into a folder.
// A small archive, written as base64 text so it fits in this file. It contains// the folder `site/` with `site/index.html` and `site/css/style.css`.bytes $archive = Core\Encoding::fromBase64('UEsDBBQAAAAAAAAAIVoAAAAAAAAAAAAAAAAFAAAAc2l0ZS9QSwMEFAAAAAgAAAAhWmWf6MURAAAAEQAAAA8AAABzaXRlL2luZGV4Lmh0bWyzyTC0C0/NSc7PTbXRB7K5AFBLAwQUAAAACAAAACFa5ti4fBYAAAAUAAAAEgAAAHNpdGUvY3NzL3N0eWxlLmNzc8swVKhWSM7PyS+yUshLLKu0VqjlAgBQSwECFAAUAAAAAAAAACFaAAAAAAAAAAAAAAAABQAAAAAAAAAAAAAA7UEAAAAAc2l0ZS9QSwECFAAUAAAACAAAACFaZZ/oxREAAAARAAAADwAAAAAAAAAAAAAApIEjAAAAc2l0ZS9pbmRleC5odG1sUEsBAhQAFAAAAAgAAAAhWubYuHwWAAAAFAAAABIAAAAAAAAAAAAAAKSBYQAAAHNpdGUvY3NzL3N0eWxlLmNzc1BLBQYAAAAAAwADALAAAACnAAAAAAA=');
// The program writes the archive to a file first, so that it has one to open.string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'site.zip');Core\IO::write($path, $archive);Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);string $folder = Core\IO::createTemporaryDirectory();
// `Core\Zip::extract` takes the open file and writes each file under `$folder`.// It returns the number of files. Folders are not counted, so this prints 2.uint $written = Core\Zip::extract($file, $folder);echo $written, "\n";$file->close();
// The files are where the archive put them. This prints the two lines of HTML and CSS.echo Core\IO::readText(Core\Path::join($folder, 'site/index.html'));echo Core\IO::readText(Core\Path::join($folder, 'site/css/style.css'));2<h1>Welcome</h1>h1 { color: navy; }A limit for the whole archive
<?nvs// Shows how one size limit applies to all the files of an archive together.
// This archive contains `january.csv` and `february.csv`. Each file has 27 bytes.bytes $archive = Core\Encoding::fromBase64('UEsDBBQAAAAIAAAAIVosewUBHQAAABsAAAALAAAAamFudWFyeS5jc3ZLSazUKU7MSS3mMtQxNDLgMtKxNOUy1jE0MeYCAFBLAwQUAAAACAAAACFaR4Xlqx0AAAAbAAAADAAAAGZlYnJ1YXJ5LmNzdktJrNQpTsxJLeYy1LGw4DLSMTQ25DLWMTQw4gIAUEsBAhQAFAAAAAgAAAAhWix7BQEdAAAAGwAAAAsAAAAAAAAAAAAAAKSBAAAAAGphbnVhcnkuY3N2UEsBAhQAFAAAAAgAAAAhWkeF5asdAAAAGwAAAAwAAAAAAAAAAAAAAKSBRgAAAGZlYnJ1YXJ5LmNzdlBLBQYAAAAAAgACAHMAAACNAAAAAAA=');string $folder = Core\IO::createTemporaryDirectory();
// The third argument is the most bytes all files may have together.// 27 + 27 is more than 40, so the second file throws a `ParseError`.try { Core\Zip::extract($archive, $folder, 40); echo "all files written\n";} catch (ParseError $error) { echo "the archive is too large\n";}
// The files written before the error stay on the disk.// This prints "january.csv: yes" and "february.csv: no".array<string> $names = ['january.csv', 'february.csv'];foreach ($names as string $name) { echo $name, ': ', Core\IO::exists(Core\Path::join($folder, $name)) ? 'yes' : 'no', "\n";}the archive is too largejanuary.csv: yesfebruary.csv: noInstall an uploaded theme
<?nvs// Shows how to install themes that users upload as zip archives, each in its own folder.
class Themes{ // Writes the files of one upload into a new folder and returns a short report. // Each theme may have at most 1 MiB of files. public static function install(bytes $upload): string { string $folder = Core\IO::createTemporaryDirectory(); try { uint $files = Core\Zip::extract($upload, $folder, 1048576); return 'installed ' . $files . ' files'; } catch (ParseError $error) { // An unsafe or broken archive throws before any file is written. // So the new folder is still empty. return 'rejected, the folder has ' . Core\Arr::count(Core\IO::list($folder)) . ' files'; } }}
// A good theme: `theme/layout.html` and `theme/theme.json`.bytes $good = Core\Encoding::fromBase64('UEsDBBQAAAAAAAAAIVoAAAAAAAAAAAAAAAAGAAAAdGhlbWUvUEsDBBQAAAAIAAAAIVrDe0MsGgAAABsAAAARAAAAdGhlbWUvbGF5b3V0Lmh0bWyzyU3MzLOrrlZIzs8rSc0rUaittdEHi3EBAFBLAwQUAAAACAAAACFaH5ZKNhUAAAATAAAAEAAAAHRoZW1lL3RoZW1lLmpzb26rVspLzE1VslJQciwtKc3NU6rlAgBQSwECFAAUAAAAAAAAACFaAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAA7UEAAAAAdGhlbWUvUEsBAhQAFAAAAAgAAAAhWsN7QywaAAAAGwAAABEAAAAAAAAAAAAAAKSBJAAAAHRoZW1lL2xheW91dC5odG1sUEsBAhQAFAAAAAgAAAAhWh+WSjYVAAAAEwAAABAAAAAAAAAAAAAAAKSBbQAAAHRoZW1lL3RoZW1lLmpzb25QSwUGAAAAAAMAAwCxAAAAsAAAAAAA');// A bad theme: it also has the file `../config.php`, which is outside the folder.bytes $bad = Core\Encoding::fromBase64('UEsDBBQAAAAIAAAAIVrDe0MsGgAAABsAAAARAAAAdGhlbWUvbGF5b3V0Lmh0bWyzyU3MzLOrrlZIzs8rSc0rUaittdEHi3EBAFBLAwQUAAAACAAAACFaWCayOxQAAAASAAAADQAAAC4uL2NvbmZpZy5waHCzsS/IKFDQ11coSi3ISUxOTeECAFBLAQIUABQAAAAIAAAAIVrDe0MsGgAAABsAAAARAAAAAAAAAAAAAACkgQAAAAB0aGVtZS9sYXlvdXQuaHRtbFBLAQIUABQAAAAIAAAAIVpYJrI7FAAAABIAAAANAAAAAAAAAAAAAACkgUkAAAAuLi9jb25maWcucGhwUEsFBgAAAAACAAIAegAAAIgAAAAAAA==');
// This prints "installed 2 files" and then "rejected, the folder has 0 files".echo Themes::install($good), "\n";echo Themes::install($bad), "\n";installed 2 filesrejected, the folder has 0 files