ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ILIAS\Filesystem\Util\Archive\Zip Class Reference
+ Collaboration diagram for ILIAS\Filesystem\Util\Archive\Zip:

Public Member Functions

 __construct (protected ZipOptions $options,... $streams)
 
 get ()
 
 destroy ()
 Explicitly close the zip file and remove the file from the filesystem. More...
 
 addPath (string $path, ?string $path_inside_zip=null)
 
 addStream (FileStream $stream, string $path_inside_zip)
 

Data Fields

const DOT_EMPTY = '.empty'
 

Protected Attributes

ZipArchive $zip
 

Private Member Functions

 buildTempPath ()
 
 registerShutdownFunction (\Closure $c)
 
 storeZIPtoFilesystem ()
 

Private Attributes

string $zip_output_file = ''
 
int $iteration_limit
 
int $store_counter = 1
 
int $path_counter = 1
 
array $streams
 

Detailed Description

Author
Fabian Schmid fabia.nosp@m.n@sr.nosp@m..solu.nosp@m.tion.nosp@m.s

Definition at line 30 of file Zip.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Filesystem\Util\Archive\Zip::__construct ( protected ZipOptions  $options,
  $streams 
)

Definition at line 46 of file Zip.php.

References ILIAS\Filesystem\Util\Archive\Zip\buildTempPath(), ILIAS\Filesystem\Util\Archive\Zip\destroy(), ILIAS\Filesystem\Util\Archive\ZipOptions\getZipOutputName(), ILIAS\Filesystem\Util\Archive\ZipOptions\getZipOutputPath(), ILIAS\Repository\int(), null, and ILIAS\Filesystem\Util\Archive\Zip\registerShutdownFunction().

49  {
50  $this->streams = array_filter($streams, fn($stream): bool => $stream instanceof FileStream);
51 
52  if ($options->getZipOutputPath() !== null && $options->getZipOutputName() !== null) {
53  $this->zip_output_file = $this->ensureDirectorySeperator(
54  $options->getZipOutputPath()
55  ) . $options->getZipOutputName();
56  } else {
57  $this->zip_output_file = $this->buildTempPath();
58  $this->registerShutdownFunction(function (): void {
59  $this->destroy();
60  });
61  }
62  $system_limit = (int) shell_exec('ulimit -n') ?: 0;
63 
64  $this->iteration_limit = $system_limit < 10 ? 100 : min(
65  $system_limit / 2,
66  5000
67  );
68 
69  $this->zip = new \ZipArchive();
70  if (!file_exists($this->zip_output_file)) {
71  touch($this->zip_output_file);
72  }
73  if ($this->zip->open($this->zip_output_file, \ZipArchive::OVERWRITE) !== true) {
74  throw new \Exception("cannot open <$this->zip_output_file>\n");
75  }
76  }
destroy()
Explicitly close the zip file and remove the file from the filesystem.
Definition: Zip.php:144
registerShutdownFunction(\Closure $c)
Definition: Zip.php:92
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the call graph for this function:

Member Function Documentation

◆ addPath()

ILIAS\Filesystem\Util\Archive\Zip::addPath ( string  $path,
?string  $path_inside_zip = null 
)
Deprecated:
in general, it should be avoided to operate with correct paths in the file system.

it is also usually not necessary to zip whole directories, as a ZIP can be seen as an "on-the-fly" compilation of different streams. However, since ILIAS still relies on zipping entire directories in many places, this method is still offered for the moment.

Definition at line 157 of file Zip.php.

References ILIAS\Filesystem\Util\Archive\Zip\addStream().

Referenced by ILIAS\Filesystem\Util\Archive\Zip\addStream().

157  : void
158  {
159  $path_inside_zip ??= basename($path);
160 
161  // create directory if it does not exist
162  $this->zip->addEmptyDir(rtrim(dirname($path_inside_zip), '/') . '/');
163 
164  $this->addStream(
165  Streams::ofResource(fopen($path, 'rb')),
166  $path_inside_zip
167  );
168  }
addStream(FileStream $stream, string $path_inside_zip)
Definition: Zip.php:170
$path
Definition: ltiservices.php:29
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addStream()

ILIAS\Filesystem\Util\Archive\Zip::addStream ( FileStream  $stream,
string  $path_inside_zip 
)

Definition at line 170 of file Zip.php.

References Vendor\Package\$d, ILIAS\Filesystem\Util\Archive\Zip\addPath(), null, and ILIAS\Filesystem\Util\Archive\Zip\storeZIPtoFilesystem().

Referenced by ILIAS\Filesystem\Util\Archive\Zip\addPath().

170  : void
171  {
172  // we remove the "empty zip file" now if possible
173  if (count($this->streams) === 1 && isset($this->streams[self::DOT_EMPTY])) {
174  unset($this->streams[self::DOT_EMPTY]);
175  }
176 
177  // we must store the ZIP to e temporary files every 1000 files, otherwise we will get a Too Many Open Files error
178  $this->streams[$path_inside_zip] = $stream;
179 
180  if (
181  $this->path_counter === $this->iteration_limit
182  || count(get_resources('stream')) > ($this->iteration_limit * 0.9)
183  ) {
184  $this->storeZIPtoFilesystem();
185  $this->streams = [];
186  $this->path_counter = 0;
187  } else {
188  $this->path_counter++;
189  }
190  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buildTempPath()

ILIAS\Filesystem\Util\Archive\Zip::buildTempPath ( )
private

Definition at line 78 of file Zip.php.

References CLIENT_DATA_DIR.

Referenced by ILIAS\Filesystem\Util\Archive\Zip\__construct().

78  : string
79  {
80  $directory = defined('CLIENT_DATA_DIR') ? \CLIENT_DATA_DIR . '/temp' : sys_get_temp_dir();
81  $tempnam = tempnam($directory, 'zip');
82  if (is_file($tempnam)) {
83  return $tempnam;
84  }
85  if (is_dir($tempnam)) {
86  rmdir($tempnam);
87  touch($tempnam);
88  }
89  return $tempnam;
90  }
const CLIENT_DATA_DIR
Definition: constants.php:46
+ Here is the caller graph for this function:

◆ destroy()

ILIAS\Filesystem\Util\Archive\Zip::destroy ( )

Explicitly close the zip file and remove the file from the filesystem.

In general, temp files are deleted whyle destroying the object. but in cases like migrations, you should call this method explicitly. Please note that also explicitly set paths (non-temp) are deleted if you call this method.

Definition at line 144 of file Zip.php.

Referenced by ILIAS\Filesystem\Util\Archive\Zip\__construct().

144  : void
145  {
146  if (file_exists($this->zip_output_file)) {
147  unlink($this->zip_output_file);
148  }
149  }
+ Here is the caller graph for this function:

◆ get()

ILIAS\Filesystem\Util\Archive\Zip::get ( )

Definition at line 130 of file Zip.php.

References ILIAS\Filesystem\Util\Archive\Zip\storeZIPtoFilesystem().

130  : Stream
131  {
132  $this->storeZIPtoFilesystem();
133 
134  $this->zip->close();
135 
136  return Streams::ofResource(fopen($this->zip_output_file, 'rb'));
137  }
+ Here is the call graph for this function:

◆ registerShutdownFunction()

ILIAS\Filesystem\Util\Archive\Zip::registerShutdownFunction ( \Closure  $c)
private

Definition at line 92 of file Zip.php.

Referenced by ILIAS\Filesystem\Util\Archive\Zip\__construct().

92  : void
93  {
94  register_shutdown_function($c);
95  }
$c
Definition: deliver.php:25
+ Here is the caller graph for this function:

◆ storeZIPtoFilesystem()

ILIAS\Filesystem\Util\Archive\Zip::storeZIPtoFilesystem ( )
private

Definition at line 97 of file Zip.php.

References $path.

Referenced by ILIAS\Filesystem\Util\Archive\Zip\addStream(), and ILIAS\Filesystem\Util\Archive\Zip\get().

97  : void
98  {
99  foreach ($this->streams as $path_inside_zip => $stream) {
100  $path = $stream->getMetadata('uri');
101  if ($this->store_counter === 0) {
102  $this->zip->open($this->zip_output_file);
103  }
104  if (is_int($path_inside_zip)) {
105  $path_inside_zip = basename((string) $path);
106  }
107 
108  if ($path === 'php://memory') {
109  $this->zip->addFromString($path_inside_zip, (string) $stream);
110  $stream->close();
111  } elseif (is_file($path)) {
112  $this->zip->addFile($path, $path_inside_zip);
113  $stream->close();
114  } else {
115  continue;
116  }
117 
118  if (
119  $this->store_counter === $this->iteration_limit
120  || count(get_resources('stream')) > ($this->iteration_limit * 0.9)
121  ) {
122  $this->zip->close();
123  $this->store_counter = 0;
124  } else {
125  $this->store_counter++;
126  }
127  }
128  }
$path
Definition: ltiservices.php:29
+ Here is the caller graph for this function:

Field Documentation

◆ $iteration_limit

int ILIAS\Filesystem\Util\Archive\Zip::$iteration_limit
private

Definition at line 37 of file Zip.php.

◆ $path_counter

int ILIAS\Filesystem\Util\Archive\Zip::$path_counter = 1
private

Definition at line 39 of file Zip.php.

◆ $store_counter

int ILIAS\Filesystem\Util\Archive\Zip::$store_counter = 1
private

Definition at line 38 of file Zip.php.

◆ $streams

array ILIAS\Filesystem\Util\Archive\Zip::$streams
private

Definition at line 44 of file Zip.php.

◆ $zip

ZipArchive ILIAS\Filesystem\Util\Archive\Zip::$zip
protected

Definition at line 36 of file Zip.php.

◆ $zip_output_file

string ILIAS\Filesystem\Util\Archive\Zip::$zip_output_file = ''
private

Definition at line 35 of file Zip.php.

◆ DOT_EMPTY

const ILIAS\Filesystem\Util\Archive\Zip::DOT_EMPTY = '.empty'

Definition at line 34 of file Zip.php.

Referenced by ILIAS\Filesystem\Util\Archive\Archives\zip().


The documentation for this class was generated from the following file: