stechstudio / laravel-zipstream

Easily create Zip files on-the-fly and provide a streaming download
MIT License
417 stars 59 forks source link

Cannot add directory #62

Open Ancyker opened 2 years ago

Ancyker commented 2 years ago

Adding a directory fails with (vendor\stechstudio\laravel-zipstream\src\Models\LocalFile.php:23):

fopen(/the/path/): failed to open stream: No such file or directory

I'm trying to add a directory without adding any files in it, i.e.

$zip->add('/path/to/dir/', 'dir/');

I just want an empty directory (no files, unless I later add files into it via add()). The idea is something like this:

$zip = new Zip('archive.zip');
$zip->add('/path/to/dir/', 'dir/');
if ($something) $zip->add('/path/to/dir/fle.jpg', 'dir/file.jpg');

I want the dir to exist even if no conditions are met. There doesn't seem to be an obvious way to accomplish this.

jszobody commented 2 years ago

Correct, there is no current support in this package for adding empty directories to a zip.

Ancyker commented 2 years ago

Correct, there is no current support in this package for adding empty directories to a zip.

Could it be added?

jszobody commented 2 years ago

I don't think so. There is no option to add empty folders in the underlying ZipStream-PHP library that this package uses, and no plans to add it either.

ozonerik commented 2 years ago

$zip = new Zip('archive.zip'); $zip->add('/path/to/dir/', 'dir/'); if ($something) $zip->add('/path/to/dir/fle.jpg', 'dir/file.jpg');

how add directory like that: maybe this reference can be next update like this: https://github.com/zanysoft/laravel-zip

jszobody commented 2 years ago

Skip that middle line, don't try to add the directory. Just $zip->add('/path/to/dir/fle.jpg', 'dir/file.jpg'); and it will create the zip directory for you.

kiraware commented 2 weeks ago

I don't think so. There is no option to add empty folders in the underlying ZipStream-PHP library that this package uses, and no plans to add it either.

It looks like ZipStream-PHP has support for it, take a look at this issue https://github.com/maennchen/ZipStream-PHP/issues/78 .

Here is the docs https://maennchen.dev/ZipStream-PHP/classes/ZipStream-ZipStream.html#method_addDirectory

kiraware commented 2 weeks ago

Honestly, i need to create blank directory to reflect the real view in the web where folders is empty. And if the user download the parent folder, it include the empty directory. Think like Google Drive, what if some user download a directory and they expect the empty folder in the zip downloaded, but there is not the empty folder? Seems wrong to me.

Hopefully you add a support for it.

jszobody commented 1 week ago

I'm glad to see the underlying ZipStream-PHP library has added support. This seems reasonable now. I'm not sure when I'll have time to add this, and would welcome a pull request if someone wants to take a stab at it.