maennchen / ZipStream-PHP

:floppy_disk: PHP ZIP Streaming Library
https://maennchen.dev/ZipStream-PHP/
MIT License
1.76k stars 105 forks source link

Create Directories #78

Closed dotriz closed 1 year ago

dotriz commented 6 years ago

I know how we can add files in folders and sub-folders in a zip. But is it possible to add blank folders?

I want to allow users to download their data in zip and want to match exactly same structure as they have on web, so creating lank folders is important.

NicolasCARPi commented 6 years ago

Hello @dotriz,

You can do like that to create an empty directory (with version 1.0.0-alpha):

$zip->addFile('directory/', '');
dotriz commented 6 years ago

@NicolasCARPi Any plan to add this support in previous versions?

NicolasCARPi commented 6 years ago

It doesn't work in previous versions?

maennchen commented 6 years ago

@NicolasCARPi We should probably add a new function createDirectory that does this. This seems like a hack to me.

I would add the function to the v1 versions only.

dotriz commented 6 years ago

@NicolasCARPi that's correct, it doesn't work on previous versions.

claytron5000 commented 5 years ago

I came here looking for this. I basically want to replace the PHP standard ZipArchive with this so that I can build and stream directly to s3. Our current implementation using ZipArchive uses the addEmptyDir.

I don't know if this is too ambitious, but something like a drop-in replacement for ZipArchive would be awesome. Maybe this should be a different issue.

https://www.php.net/manual/en/class.ziparchive.php

walkingCommiter commented 3 years ago

I have faced the same issue creating empty folders fails except if using what @NicolasCARPi suggested:

$zip->addFile($directory . DIRECTORY_SEPARATOR, '');

However, testing the download of a zipStream causes unit test failure since it counts empty folders created this way as a file:

$zip = new ZipArchive(); $zip->open($tempName); // open the downloaded stream self::assertEquals($expectedFilesCount, $zip->count); // count is incorrect -- it counts folders as file

this counts extra for each folder which is totally wrong also happens for $zip

christian-kolb commented 3 years ago

@maennchen Help wanted, means a pull request for it would be merged? 🙂

maennchen commented 3 years ago

@christian-kolb a PR is very welcome 🙂

christian-kolb commented 3 years ago

@maennchen I started and saw pretty quickly that I severely underestimated the complexity of this library and what is needed to make this change. The whole directory construct doesn't seem to exist in the package yet. My approach would be to create a Directory class according to the structure of the File class. But I'm not sure if the concepts match here. A directory doesn't have a content but is more of a marker. I'm pretty much lost as soon as it comes to the headers and the V... format. Can you point me to relevant documentation? Then I would try to pickup from there 🙂

maennchen commented 3 years ago

@christian-kolb Yeah, it's not that approachable.

For every change, I also have to read the specifications.

https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT

Unfortunately, I also do not know about resources that are a lot better...

bastien70 commented 2 years ago

Hello, what's about this feature ?

maennchen commented 2 years ago

@bastien70 A PR would be welcome.

bastien70 commented 2 years ago

I don't have the capacity to do that kind of PR sorry.

maennchen commented 2 years ago

@bastien70 And I do not have the time. If this is important to anyone, please open a PR. Otherwise there will be no solution to this anytime soon.

walkingCommiter commented 2 years ago

@bastien70 If it is okay for your customers I would suggest a simple solution to just putting an empty file in all empty folders by default to keep the structure (kind of like how .gitignore works for git folders) and just waiting for maintainers to see what we can do. (Also check last comment of me maybe it fails on unit test but works on runtime).

maennchen commented 1 year ago

Tracked in #224

ahmed-bhs commented 1 year ago

@maennchen any new about this feature ?

maennchen commented 1 year ago

@ahmed-bhs This feature is part of the current beta release.

ahmed-bhs commented 1 year ago

Hey @maennchen 3.0.0@beta ? Does there any documentation behind the feature?

maennchen commented 1 year ago

@ahmed-bhs The docs are not published right now.

You can checkout the project and generate the docs. (composer run docs:generate)

Otherwise everything is documented in the code comments.

https://github.com/maennchen/ZipStream-PHP/blob/815cf35e8f5285ee91ff97403953519903b3d6f4/src/ZipStream.php#L475