1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-10-10 12:54:29 +02:00

#54 overwriting open zip archive fixed

This commit is contained in:
wapplay
2020-05-04 14:13:39 +03:00
parent 3942bf2005
commit dbddcda001
3 changed files with 42 additions and 39 deletions

View File

@@ -1611,6 +1611,17 @@ class ZipFile implements ZipFileInterface
}
$this->saveAsStream($handle);
$reopen = false;
if ($this->reader !== null) {
$meta = $this->reader->getStreamMetaData();
if ($meta['wrapper_type'] === 'plainfile' && isset($meta['uri']) && $meta['uri'] === $filename) {
$this->reader->close();
$reopen = true;
}
}
if (!@rename($tempFilename, $filename)) {
if (is_file($tempFilename)) {
unlink($tempFilename);
@@ -1619,6 +1630,10 @@ class ZipFile implements ZipFileInterface
throw new ZipException('Can not move ' . $tempFilename . ' to ' . $filename);
}
if ($reopen) {
return $this->openFile($filename);
}
return $this;
}
@@ -1822,24 +1837,11 @@ class ZipFile implements ZipFileInterface
$meta = $this->reader->getStreamMetaData();
if ($meta['wrapper_type'] === 'plainfile' && isset($meta['uri'])) {
$this->saveAsFile($meta['uri']);
$this->close();
if (!($handle = @fopen($meta['uri'], 'rb'))) {
throw new ZipException("File {$meta['uri']} can't open.");
}
} else {
$handle = @fopen('php://temp', 'r+b');
if (!$handle) {
throw new ZipException('php://temp cannot open for write.');
}
$this->writeZipToStream($handle);
$this->close();
if ($meta['wrapper_type'] !== 'plainfile' || !isset($meta['uri'])) {
throw new ZipException('Overwrite is only supported for open local files.');
}
return $this->openFromStream($handle);
return $this->saveAsFile($meta['uri']);
}
/**