ZipStream-PHP icon indicating copy to clipboard operation
ZipStream-PHP copied to clipboard

Create Directories

Open dotriz opened this issue 7 years ago • 16 comments

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.

dotriz avatar Jul 19 '18 12:07 dotriz

Hello @dotriz,

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

$zip->addFile('directory/', '');

NicolasCARPi avatar Jul 30 '18 20:07 NicolasCARPi

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

dotriz avatar Aug 08 '18 07:08 dotriz

It doesn't work in previous versions?

NicolasCARPi avatar Aug 08 '18 08:08 NicolasCARPi

@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.

maennchen avatar Aug 08 '18 10:08 maennchen

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

dotriz avatar Sep 13 '18 21:09 dotriz

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

claytron5000 avatar May 17 '19 00:05 claytron5000

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

walkingCommiter avatar Jan 10 '21 18:01 walkingCommiter

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

christian-kolb avatar Jun 10 '21 11:06 christian-kolb

@christian-kolb a PR is very welcome 🙂

maennchen avatar Jun 10 '21 11:06 maennchen

@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 🙂

christian-kolb avatar Jun 10 '21 12:06 christian-kolb

@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...

maennchen avatar Jun 10 '21 12:06 maennchen

Hello, what's about this feature ?

bastien70 avatar Jan 06 '22 15:01 bastien70

@bastien70 A PR would be welcome.

maennchen avatar Jan 06 '22 16:01 maennchen

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

bastien70 avatar Jan 06 '22 17:01 bastien70

@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.

maennchen avatar Jan 06 '22 17:01 maennchen

@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).

walkingCommiter avatar Jan 06 '22 20:01 walkingCommiter

Tracked in #224

maennchen avatar Nov 25 '22 04:11 maennchen

@maennchen any new about this feature ?

ahmed-bhs avatar Feb 07 '23 14:02 ahmed-bhs

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

maennchen avatar Feb 07 '23 15:02 maennchen

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

ahmed-bhs avatar Feb 07 '23 15:02 ahmed-bhs

@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

maennchen avatar Feb 07 '23 16:02 maennchen