0

I'm creating some kind of file-sharing application. In this application you should be able to up-& download files and structure these files.

I've opted to not keep on manipulating the file-system. But I Upload the files into folders based on timestamps, and store the important information in the database and structure them inside the DB. So there are no REAL subfolders, just relations in the database to structure thes files into folders.

No I want to let users download folders (including subfolders) by zipping it. But I want to recreate this folder-structure(that the user sees in the front-end) inside the zip-archive that will be downloaded. I've managed to do this using the ZipArchive class of PHP: http://php.net/manual/en/class.ziparchive.php But there's 1 big issue with this. It uses a lot of memory & cpu when compressing big files. And the system must be able to handle large files (>1GB) I can't possibly allow PHP to use >1GB of memory?!

Now I've found a Stackoverflow question to zip large files using less memory in php: https://stackoverflow.com/a/4357904/383731

This seems to use the Unix Zip command, But is it possible to create folders inside these zips without them existing inside the file-system?

Community
  • 1
  • 1
Stijn_d
  • 1,058
  • 9
  • 20
  • hmm... create a temporary directory, create a archive file and delete directories like this: `mkdir a; mkdir b; zip -r arch.zip .; rm -rf a b;`. – mkjasinski Apr 15 '13 at 12:08
  • @mkjasinski I didn't want to use real File-system. I've tackeled this problem with Oliver A.'s solution, creating symlinks ... – Stijn_d Apr 15 '13 at 13:59

1 Answers1

1
  • create a folder in your temp directory
  • add subfolders and links to real files
  • zip it
  • send it
  • kill temp files

The part about the links is important. If you just use links you do not actually have to copy the file contents. All you do is give the zip tool a hint where it can find the contents you want to include in the archive

EDIT:

Both symbolic links and hard links work, The link above explains how to do it with symlinks.

Oliver A.
  • 2,819
  • 1
  • 16
  • 21
  • Neat solution (although hard links wil work just as well as symbolic links on the same filesystem - symbolic links are the wy to go, but not for the reason you suggest) – symcbean Apr 15 '13 at 12:27
  • @symcbean true. The import part is "link don´t copy". – Oliver A. Apr 15 '13 at 12:43
  • That seems to like a neat solution, I'm going to try this one out & let you know! – Stijn_d Apr 15 '13 at 12:56
  • Thanks man this did the job! Is there a need to clean up the symlinks? Or are they automatically removed when I remove the folder? – Stijn_d Apr 15 '13 at 13:58