File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ class Builder implements Responsable
37
37
38
38
protected Closure $ afterProcessing ;
39
39
40
+ protected array $ directories = [];
41
+
40
42
public function __construct (array $ files = [])
41
43
{
42
44
$ this ->queue = new Queue ();
@@ -88,6 +90,13 @@ public function addRaw($content, string $zipPath): self
88
90
return $ this ->add (new TempFile ($ content , $ zipPath ));
89
91
}
90
92
93
+ public function addDirectory (string $ name ): self
94
+ {
95
+ $ this ->directories [] = $ name ;
96
+
97
+ return $ this ;
98
+ }
99
+
91
100
public function setMeta (array $ meta ): self
92
101
{
93
102
$ this ->meta = collect ($ meta );
@@ -243,6 +252,10 @@ protected function prepare(): ZipStream
243
252
244
253
$ this ->queue ->each ->prepare ($ zip );
245
254
255
+ foreach ($ this ->directories as $ directory ) {
256
+ $ zip ->addDirectory ($ directory );
257
+ }
258
+
246
259
return $ zip ;
247
260
}
248
261
Original file line number Diff line number Diff line change @@ -136,4 +136,32 @@ public function testSaveToDisk()
136
136
$ this ->assertTrue (file_exists ("/tmp/my-prefix/ $ folder/test.zip " ));
137
137
unlink ("/tmp/my-prefix/ $ folder/test.zip " );
138
138
}
139
+
140
+ public function testEmptyDirectory ()
141
+ {
142
+ $ testrun = microtime ();
143
+ file_put_contents ("/tmp/test1.txt " , "this is the first test file for test run $ testrun " );
144
+
145
+ /** @var Builder $zip */
146
+ $ zip = Zip::create ("test.zip " , ["/tmp/test1.txt " ])->addDirectory ($ directory = Str::random ());
147
+
148
+ // Create a random folder path that doesn't exist, so we know it was created
149
+ $ dir = "/tmp/ " . Str::random ();
150
+ $ zip ->saveTo ($ dir );
151
+
152
+ $ this ->assertTrue (file_exists ("$ dir/test.zip " ));
153
+
154
+ $ z = new ZipArchive ();
155
+ $ z ->open ("$ dir/test.zip " );
156
+ $ this ->assertEquals (2 , $ z ->numFiles );
157
+ $ this ->assertNotFalse ($ z ->getFromName ($ directory . "/ " ));
158
+
159
+ $ z ->extractTo ($ dir . "/extracted " );
160
+ $ this ->assertTrue (file_exists ("$ dir/extracted/ $ directory/ " ));
161
+ $ this ->assertTrue (is_dir ("$ dir/extracted/ $ directory/ " ));
162
+
163
+ rmdir ("$ dir/extracted/ $ directory/ " );
164
+ unlink ("$ dir/extracted/test1.txt " );
165
+ unlink ("$ dir/test.zip " );
166
+ }
139
167
}
You can’t perform that action at this time.
0 commit comments