PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.23
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.23
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / vendor / openspout / openspout / src / Writer / Common / Helper / ZipHelper.php

ZipHelper.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.23, at vendor/openspout/openspout/src/Writer/Common/Helper/ZipHelper.php

181 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\OpenSpout\Writer\Common\Helper;
4
5 use FluentCart\OpenSpout\Writer\Common\Creator\InternalEntityFactory;
6 /**
7 * This class provides helper functions to create zip files.
8 */
9 class ZipHelper
10 {
11 public const ZIP_EXTENSION = '.zip';
12 /** Controls what to do when trying to add an existing file */
13 public const EXISTING_FILES_SKIP = 'skip';
14 public const EXISTING_FILES_OVERWRITE = 'overwrite';
15 /** @var InternalEntityFactory Factory to create entities */
16 private $entityFactory;
17 /**
18 * @param InternalEntityFactory $entityFactory Factory to create entities
19 */
20 public function __construct($entityFactory)
21 {
22 $this->entityFactory = $entityFactory;
23 }
24 /**
25 * Returns a new ZipArchive instance pointing at the given path.
26 *
27 * @param string $tmpFolderPath Path of the temp folder where the zip file will be created
28 *
29 * @return \ZipArchive
30 */
31 public function createZip($tmpFolderPath)
32 {
33 $zip = $this->entityFactory->createZipArchive();
34 $zipFilePath = $tmpFolderPath . self::ZIP_EXTENSION;
35 $zip->open($zipFilePath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
36 return $zip;
37 }
38 /**
39 * @param \ZipArchive $zip An opened zip archive object
40 *
41 * @return string Path where the zip file of the given folder will be created
42 */
43 public function getZipFilePath(\ZipArchive $zip)
44 {
45 return $zip->filename;
46 }
47 /**
48 * Adds the given file, located under the given root folder to the archive.
49 * The file will be compressed.
50 *
51 * Example of use:
52 * addFileToArchive($zip, '/tmp/xlsx/foo', 'bar/baz.xml');
53 * => will add the file located at '/tmp/xlsx/foo/bar/baz.xml' in the archive, but only as 'bar/baz.xml'
54 *
55 * @param \ZipArchive $zip An opened zip archive object
56 * @param string $rootFolderPath path of the root folder that will be ignored in the archive tree
57 * @param string $localFilePath Path of the file to be added, under the root folder
58 * @param string $existingFileMode Controls what to do when trying to add an existing file
59 */
60 public function addFileToArchive($zip, $rootFolderPath, $localFilePath, $existingFileMode = self::EXISTING_FILES_OVERWRITE)
61 {
62 $this->addFileToArchiveWithCompressionMethod($zip, $rootFolderPath, $localFilePath, $existingFileMode, \ZipArchive::CM_DEFAULT);
63 }
64 /**
65 * Adds the given file, located under the given root folder to the archive.
66 * The file will NOT be compressed.
67 *
68 * Example of use:
69 * addUncompressedFileToArchive($zip, '/tmp/xlsx/foo', 'bar/baz.xml');
70 * => will add the file located at '/tmp/xlsx/foo/bar/baz.xml' in the archive, but only as 'bar/baz.xml'
71 *
72 * @param \ZipArchive $zip An opened zip archive object
73 * @param string $rootFolderPath path of the root folder that will be ignored in the archive tree
74 * @param string $localFilePath Path of the file to be added, under the root folder
75 * @param string $existingFileMode Controls what to do when trying to add an existing file
76 */
77 public function addUncompressedFileToArchive($zip, $rootFolderPath, $localFilePath, $existingFileMode = self::EXISTING_FILES_OVERWRITE)
78 {
79 $this->addFileToArchiveWithCompressionMethod($zip, $rootFolderPath, $localFilePath, $existingFileMode, \ZipArchive::CM_STORE);
80 }
81 /**
82 * @return bool Whether it is possible to choose the desired compression method to be used
83 */
84 public static function canChooseCompressionMethod()
85 {
86 // setCompressionName() is a PHP7+ method...
87 return \method_exists(new \ZipArchive(), 'setCompressionName');
88 }
89 /**
90 * @param \ZipArchive $zip An opened zip archive object
91 * @param string $folderPath Path to the folder to be zipped
92 * @param string $existingFileMode Controls what to do when trying to add an existing file
93 */
94 public function addFolderToArchive($zip, $folderPath, $existingFileMode = self::EXISTING_FILES_OVERWRITE)
95 {
96 $folderRealPath = $this->getNormalizedRealPath($folderPath) . '/';
97 $itemIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folderPath, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
98 foreach ($itemIterator as $itemInfo) {
99 $itemRealPath = $this->getNormalizedRealPath($itemInfo->getPathname());
100 $itemLocalPath = \str_replace($folderRealPath, '', $itemRealPath);
101 if ($itemInfo->isFile() && !$this->shouldSkipFile($zip, $itemLocalPath, $existingFileMode)) {
102 $zip->addFile($itemRealPath, $itemLocalPath);
103 }
104 }
105 }
106 /**
107 * Closes the archive and copies it into the given stream.
108 *
109 * @param \ZipArchive $zip An opened zip archive object
110 * @param resource $streamPointer Pointer to the stream to copy the zip
111 */
112 public function closeArchiveAndCopyToStream($zip, $streamPointer)
113 {
114 $zipFilePath = $zip->filename;
115 $zip->close();
116 $this->copyZipToStream($zipFilePath, $streamPointer);
117 }
118 /**
119 * Adds the given file, located under the given root folder to the archive.
120 * The file will NOT be compressed.
121 *
122 * Example of use:
123 * addUncompressedFileToArchive($zip, '/tmp/xlsx/foo', 'bar/baz.xml');
124 * => will add the file located at '/tmp/xlsx/foo/bar/baz.xml' in the archive, but only as 'bar/baz.xml'
125 *
126 * @param \ZipArchive $zip An opened zip archive object
127 * @param string $rootFolderPath path of the root folder that will be ignored in the archive tree
128 * @param string $localFilePath Path of the file to be added, under the root folder
129 * @param string $existingFileMode Controls what to do when trying to add an existing file
130 * @param int $compressionMethod The compression method
131 */
132 protected function addFileToArchiveWithCompressionMethod($zip, $rootFolderPath, $localFilePath, $existingFileMode, $compressionMethod)
133 {
134 if (!$this->shouldSkipFile($zip, $localFilePath, $existingFileMode)) {
135 $normalizedFullFilePath = $this->getNormalizedRealPath($rootFolderPath . '/' . $localFilePath);
136 $zip->addFile($normalizedFullFilePath, $localFilePath);
137 if (self::canChooseCompressionMethod()) {
138 $zip->setCompressionName($localFilePath, $compressionMethod);
139 }
140 }
141 }
142 /**
143 * @param \ZipArchive $zip
144 * @param string $itemLocalPath
145 * @param string $existingFileMode
146 *
147 * @return bool Whether the file should be added to the archive or skipped
148 */
149 protected function shouldSkipFile($zip, $itemLocalPath, $existingFileMode)
150 {
151 // Skip files if:
152 // - EXISTING_FILES_SKIP mode chosen
153 // - File already exists in the archive
154 return self::EXISTING_FILES_SKIP === $existingFileMode && \false !== $zip->locateName($itemLocalPath);
155 }
156 /**
157 * Returns canonicalized absolute pathname, containing only forward slashes.
158 *
159 * @param string $path Path to normalize
160 *
161 * @return string Normalized and canonicalized path
162 */
163 protected function getNormalizedRealPath($path)
164 {
165 $realPath = \realpath($path);
166 return \str_replace(\DIRECTORY_SEPARATOR, '/', $realPath);
167 }
168 /**
169 * Streams the contents of the zip file into the given stream.
170 *
171 * @param string $zipFilePath Path of the zip file
172 * @param resource $pointer Pointer to the stream to copy the zip
173 */
174 protected function copyZipToStream($zipFilePath, $pointer)
175 {
176 $zipFilePointer = \fopen($zipFilePath, 'r');
177 \stream_copy_to_stream($zipFilePointer, $pointer);
178 \fclose($zipFilePointer);
179 }
180 }
181