Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,30 +273,39 @@ protected function extractNative($archive, $destination)
throw new \RuntimeException('Unable to create destination folder ' . \dirname($destination));
}

// Read files in the archive
for ($index = 0; $index < $zip->numFiles; $index++) {
$file = $zip->getNameIndex($index);
$tempDir = sys_get_temp_dir() . '/' . uniqid('extract_', true);

// don't try to extract based on number of files since this causes ulimit issues
// instead extract everything to a temporary folder then move to the final location
$result = $zip->extractTo($tempDir);

if (!$result) {
throw new \RuntimeException('Unable to extract ZIP contents.');
}

for ($i = 0; $i < $zip->numFiles; $i++) {
$file = $zip->getNameIndex($i);
Comment on lines +286 to +287

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make sense anymore? shouldn't we just make a recursive copy of the temp folder?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine by me


if (substr($file, -1) === '/') {
continue;
}

$buffer = $zip->getFromIndex($index);

if ($buffer === false) {
throw new \RuntimeException('Unable to read ZIP entry');
}
$source = $tempDir . '/' . $file;
$target = $destination . '/' . $file;

if (!$this->isBelow($destination, $destination . '/' . $file)) {
throw new \RuntimeException('Unable to write outside of destination path', 100);
// Ensure parent dir exists
if (!Folder::create(dirname($target))) {
throw new \RuntimeException('Unable to create directory ' . dirname($target));
}

if (File::write($destination . '/' . $file, $buffer) === false) {
throw new \RuntimeException('Unable to write ZIP entry to file ' . $destination . '/' . $file);
// Move file from temporary location to destination
if (!rename($source, $target)) {
throw new \RuntimeException('Unable to move temporary file to destination ' . $target);
}
}

$zip->close();
// Clean up temp folder
Folder::delete($tempDir);

return true;
}
Expand Down