1010class ArchiveZipCreate
1111{
1212 /**
13- * @param SplFileInfo[] $files
14- * @param array<string, string> $strings
13+ * @param ArchiveFile[] $files
1514 */
1615 protected function __construct (
1716 protected string $ path ,
1817 protected string $ name ,
1918 protected array $ files = [],
20- protected array $ strings = [],
2119 protected int $ count = 0 ,
2220 ) {
2321 }
@@ -64,85 +62,82 @@ public function getCount(): int
6462 }
6563
6664 /**
67- * @return SplFileInfo []
65+ * @return ArchiveFile []
6866 */
6967 public function getFiles (): array
7068 {
7169 return $ this ->files ;
7270 }
7371
7472 /**
75- * @return array<string, string>
73+ * Add a new file to the archive from existing file.
74+ *
75+ * @param string $outputPath Path to the file inside the archive
76+ * @param string $pathToFile Path to the file to add
7677 */
77- public function getStrings (): array
78- {
79- return $ this ->strings ;
80- }
81-
82- public function addFile (string $ path ): self
78+ public function addFile (string $ outputPath , string $ pathToFile ): self
8379 {
84- $ this ->files [] = new SplFileInfo ($ path );
80+ $ this ->files [] = new ArchiveFile ( $ outputPath , new SplFileInfo ($ pathToFile ) );
8581 $ this ->count ++;
8682
8783 return $ this ;
8884 }
8985
90- public function addFromString (string $ filename , string $ content ): self
86+ /**
87+ * Add a new file to the archive from string.
88+ *
89+ * @param string $outputPath Path to the file inside the archive
90+ * @param string $content Content of the file to add
91+ */
92+ public function addFromString (string $ outputPath , string $ content ): self
9193 {
92- $ this ->strings [ $ filename ] = $ content ;
94+ $ this ->files [ ] = new ArchiveFile ( $ outputPath , null , $ content) ;
9395 $ this ->count ++;
9496
9597 return $ this ;
9698 }
9799
98- public function addFiles (array $ paths ): self
99- {
100- foreach ($ paths as $ path ) {
101- $ this ->addFile ($ path );
102- }
103-
104- return $ this ;
105- }
106-
107- public function addDirectory (string $ path ): self
100+ /**
101+ * Add a full directory to the archive, including subdirectories.
102+ *
103+ * @param string $relativeTo Relative path to the directory inside the archive
104+ * @param string $path Path to the directory to add
105+ *
106+ * ```php
107+ * $archive->addDirectory('./to/directory', '/path/to/directory');
108+ * ```
109+ */
110+ public function addDirectory (string $ relativeTo , string $ path ): self
108111 {
109- $ files = $ this ->pathsToSplFiles ($ this ->directoryToPaths ($ path ));
112+ $ files = $ this ->pathsToSplFiles ($ this ->directoryToPaths ($ path, $ relativeTo ));
110113 $ this ->files = [...$ this ->files , ...$ files ];
111114 $ this ->count = count ($ this ->files );
112115
113116 return $ this ;
114117 }
115118
116- public function addDirectories (array $ paths ): self
117- {
118- foreach ($ paths as $ path ) {
119- $ this ->addDirectory ($ path );
120- }
121-
122- return $ this ;
123- }
124-
125- public function save (): self
119+ /**
120+ * Save the archive.
121+ */
122+ public function save (): bool
126123 {
127124 $ zip = new ZipArchive ();
128125 $ zip ->open ($ this ->path , ZipArchive::CREATE );
129126
130127 foreach ($ this ->files as $ file ) {
131- $ zip -> addFile ( $ file -> getRealPath (), $ file ->getFilename ()) ;
132- }
133-
134- foreach ( $ this -> strings as $ filename => $ content ) {
135- $ zip ->addFromString ($ filename , $ content );
128+ $ content = $ file ->content ;
129+ if (! $ file -> isString ) {
130+ $ content = file_get_contents ( $ file -> file -> getPathname ());
131+ }
132+ $ zip ->addFromString ($ file -> outputPath , $ content );
136133 }
137134
138135 $ this ->count = $ zip ->numFiles ;
139136
140- $ zip ->close ();
141-
142- return $ this ;
137+ return $ zip ->close ();
143138 }
144139
145- protected function directoryToPaths (string $ path ): array
140+ protected function directoryToPaths (string $ path, string $ relativeTo ): array
146141 {
147142 $ files = [];
148143 $ directory = new RecursiveDirectoryIterator ($ path );
@@ -152,7 +147,9 @@ protected function directoryToPaths(string $path): array
152147 if ($ file ->isDir ()) {
153148 continue ;
154149 }
155- $ files [] = $ file ->getPathname ();
150+
151+ $ outputPath = str_replace ($ path , $ relativeTo , $ file ->getPathname ());
152+ $ files [] = $ this ->addFile ($ outputPath , $ file ->getPathname ());
156153 }
157154
158155 return $ files ;
@@ -176,3 +173,17 @@ protected function pathsToSplFiles(array $paths): array
176173 return $ files ;
177174 }
178175}
176+
177+ class ArchiveFile
178+ {
179+ public function __construct (
180+ public string $ outputPath ,
181+ public ?SplFileInfo $ file = null ,
182+ public ?string $ content = null ,
183+ public bool $ isString = false ,
184+ ) {
185+ if (! $ this ->file ) {
186+ $ this ->isString = true ;
187+ }
188+ }
189+ }
0 commit comments