ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
LegacyArchives.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 
33 final class LegacyArchives
34 {
35  use PathHelper;
36 
40 
41  public function __construct()
42  {
43  $this->archives = new Archives();
44  $this->zip_options = $this->archives->zipOptions();
45  $this->unzip_options = $this->archives->unzipOptions();
46  }
47 
51  public function zip(
52  string $directory_to_zip,
53  string $path_to_output_zip,
54  bool $ensure_top_directory = false
55  ): bool {
56  $directory_to_zip = $this->normalizePath($directory_to_zip);
57  $path_to_output_zip = $this->normalizePath($path_to_output_zip);
58 
59  $zip = $this->archives->zip(
60  [],
61  $this->zip_options
62  ->withZipOutputPath(dirname($path_to_output_zip))
63  ->withZipOutputName(basename($path_to_output_zip))
64  ->withDirectoryHandling($ensure_top_directory ? ZipDirectoryHandling::ENSURE_SINGLE_TOP_DIR : ZipDirectoryHandling::KEEP_STRUCTURE)
65  );
66 
67  $zip->addDirectory($directory_to_zip);
68  $zip_stream = $zip->get();
69 
70  return $zip_stream->getSize() > 0;
71  }
72 
76  public function unzip(
77  string $path_to_zip,
78  ?string $extract_to_path = null,
79  bool $overwrite = false,
80  bool $flat = false,
81  bool $ensure_top_directory = false
82  ): bool {
83  $extract_to_path ??= dirname($path_to_zip);
84  if ($flat) {
86  } else {
87  $dir_handling = $ensure_top_directory ? ZipDirectoryHandling::ENSURE_SINGLE_TOP_DIR : ZipDirectoryHandling::KEEP_STRUCTURE;
88  }
89 
90  $unzip = $this->archives->unzip(
91  Streams::ofResource(fopen($path_to_zip, 'rb')),
92  $this->unzip_options
93  ->withZipOutputPath($extract_to_path)
94  ->withOverwrite($overwrite)
95  ->withDirectoryHandling($dir_handling)
96  );
97  return $unzip->extract();
98  }
99 }
Will keep the top directory of the ZIP file if there is one (simple unzip).
zip(string $directory_to_zip, string $path_to_output_zip, bool $ensure_top_directory=false)
unzip(string $path_to_zip, ?string $extract_to_path=null, bool $overwrite=false, bool $flat=false, bool $ensure_top_directory=false)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null