ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
LegacyArchives.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
35 final class LegacyArchives
36 {
37  use PathHelper;
38 
42 
43  public function __construct()
44  {
45  $this->archives = new Archives();
46  $this->zip_options = $this->archives->zipOptions();
47  $this->unzip_options = $this->archives->unzipOptions();
48  }
49 
53  public function zip(
54  string $directory_to_zip,
55  string $path_to_output_zip,
56  bool $ensure_top_directory = false
57  ): bool {
58  $directory_to_zip = $this->normalizePath($directory_to_zip);
59  $path_to_output_zip = $this->normalizePath($path_to_output_zip);
60 
61  $zip = $this->archives->zip(
62  [],
63  $this->zip_options
64  ->withZipOutputPath(dirname($path_to_output_zip))
65  ->withZipOutputName(basename($path_to_output_zip))
66  ->withDirectoryHandling($ensure_top_directory ? ZipDirectoryHandling::ENSURE_SINGLE_TOP_DIR : ZipDirectoryHandling::KEEP_STRUCTURE)
67  );
68 
69  $zip->addDirectory($directory_to_zip);
70  $zip_stream = $zip->get();
71 
72  return $zip_stream->getSize() > 0;
73  }
74 
78  public function unzip(
79  string $path_to_zip,
80  string $extract_to_path = null,
81  bool $overwrite = false,
82  bool $flat = false,
83  bool $ensure_top_directory = false
84  ): bool {
85  $extract_to_path ??= dirname($path_to_zip);
86  if ($flat) {
88  } else {
89  $dir_handling = $ensure_top_directory ? ZipDirectoryHandling::ENSURE_SINGLE_TOP_DIR : ZipDirectoryHandling::KEEP_STRUCTURE;
90  }
91 
92  $unzip = $this->archives->unzip(
93  Streams::ofResource(fopen($path_to_zip, 'rb')),
94  $this->unzip_options
95  ->withZipOutputPath($extract_to_path)
96  ->withOverwrite($overwrite)
97  ->withDirectoryHandling($dir_handling)
98  );
99  return $unzip->extract();
100  }
101 }
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)