-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVEMENT] Several missing fixes in Zip/unzip/IRSS/Filesystem
- Loading branch information
Showing
24 changed files
with
876 additions
and
496 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,9 @@ | |
|
||
namespace ILIAS\Filesystem\Util\Archive; | ||
|
||
use ILIAS\ResourceStorage\Identification\ResourceIdentification; | ||
use ILIAS\ResourceStorage\StorageHandler\StorageHandler; | ||
use ILIAS\ResourceStorage\StorageHandler\StorageHandlerFactory; | ||
use ILIAS\ResourceStorage\Revision\Revision; | ||
use ILIAS\ResourceStorage\Resource\StorableResource; | ||
use ILIAS\Filesystem\Stream\Stream; | ||
use ILIAS\Filesystem\Stream\Streams; | ||
use ILIAS\Filesystem\Stream\FileStream; | ||
use ILIAS\Filesystem\Util\Archive\BaseZip; | ||
|
||
/** | ||
* @author Fabian Schmid <[email protected]> | ||
|
@@ -36,6 +31,7 @@ class Zip | |
{ | ||
use PathHelper; | ||
|
||
public const DOT_EMPTY = '.empty'; | ||
private string $zip_output_file = ''; | ||
protected \ZipArchive $zip; | ||
private int $iteration_limit; | ||
|
@@ -51,9 +47,7 @@ public function __construct( | |
protected ZipOptions $options, | ||
...$streams | ||
) { | ||
$this->streams = array_filter($streams, function ($stream): bool { | ||
return $stream instanceof FileStream; | ||
}); | ||
$this->streams = array_filter($streams, fn($stream): bool => $stream instanceof FileStream); | ||
|
||
if ($options->getZipOutputPath() !== null && $options->getZipOutputName() !== null) { | ||
$this->zip_output_file = $this->ensureDirectorySeperator( | ||
|
@@ -69,14 +63,10 @@ public function __construct( | |
} | ||
$system_limit = (int) shell_exec('ulimit -n') ?: 0; | ||
|
||
if ($system_limit < 10) { // aka we cannot determine the system limit properly | ||
$this->iteration_limit = 100; | ||
} else { | ||
$this->iteration_limit = min( | ||
$system_limit / 2, | ||
5000 | ||
); | ||
} | ||
$this->iteration_limit = $system_limit < 10 ? 100 : min( | ||
$system_limit / 2, | ||
5000 | ||
); | ||
|
||
$this->zip = new \ZipArchive(); | ||
if (!file_exists($this->zip_output_file)) { | ||
|
@@ -113,7 +103,7 @@ private function storeZIPtoFilesystem(): void | |
$this->zip->open($this->zip_output_file); | ||
} | ||
if (is_int($path_inside_zip)) { | ||
$path_inside_zip = basename($path); | ||
$path_inside_zip = basename((string) $path); | ||
} | ||
|
||
if ($path === 'php://memory') { | ||
|
@@ -138,7 +128,7 @@ private function storeZIPtoFilesystem(): void | |
} | ||
} | ||
|
||
public function get(): \ILIAS\Filesystem\Stream\Stream | ||
public function get(): Stream | ||
{ | ||
$this->storeZIPtoFilesystem(); | ||
|
||
|
@@ -155,14 +145,24 @@ public function get(): \ILIAS\Filesystem\Stream\Stream | |
*/ | ||
public function addPath(string $path, ?string $path_inside_zip = null): void | ||
{ | ||
$path_inside_zip = $path_inside_zip ?? basename($path); | ||
|
||
// create directory if it does not exist | ||
$this->zip->addEmptyDir(rtrim(dirname($path_inside_zip), '/') . '/'); | ||
|
||
$this->addStream( | ||
Streams::ofResource(fopen($path, 'r')), | ||
$path_inside_zip ?? basename($path) | ||
Streams::ofResource(fopen($path, 'rb')), | ||
$path_inside_zip | ||
); | ||
} | ||
|
||
public function addStream(FileStream $stream, string $path_inside_zip): void | ||
{ | ||
// we remove the "empty zip file" now if possible | ||
if (count($this->streams) === 1 && isset($this->streams[self::DOT_EMPTY])) { | ||
unset($this->streams[self::DOT_EMPTY]); | ||
} | ||
|
||
// we must store the ZIP to e temporary files every 1000 files, otherwise we will get a Too Many Open Files error | ||
$this->streams[$path_inside_zip] = $stream; | ||
|
||
|
@@ -193,7 +193,6 @@ public function addDirectory(string $directory_to_zip): void | |
\RecursiveIteratorIterator::SELF_FIRST | ||
); | ||
|
||
|
||
switch ($this->options->getDirectoryHandling()) { | ||
case ZipDirectoryHandling::KEEP_STRUCTURE: | ||
$pattern = null; | ||
|
@@ -205,7 +204,6 @@ public function addDirectory(string $directory_to_zip): void | |
break; | ||
} | ||
|
||
|
||
foreach ($files as $file) { | ||
$pathname = $file->getPathname(); | ||
$path_inside_zip = str_replace($directory_to_zip . '/', '', $pathname); | ||
|
@@ -216,7 +214,8 @@ public function addDirectory(string $directory_to_zip): void | |
/** @var $file \SplFileInfo */ | ||
if ($file->isDir()) { | ||
// add directory to zip if it's empty | ||
if (count(scandir($pathname)) === 2) { | ||
$sub_items = array_filter(scandir($pathname), static fn($d): bool => !str_contains((string) $d, '.DS_Store')); | ||
if (count($sub_items) === 2) { | ||
$this->zip->addEmptyDir($path_inside_zip); | ||
} | ||
continue; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.