Question

I would like to compress a directory using 7-Zip and then download it to a web browser.

However, instead of compressing to a file, then having PHP feed the contents of that file to the browser... I would ideally like to Have 7-Zip write the contents of the new archive to standard output as the archive is being created... While simultaneously having PHP flush its buffer and and send the data to the browser.

You see, the archive I'm creating is quite large. From a user's perspective, using the normal method, when they click "Download," the archive is first created and saved to a file (which can take ~ 10 minutes), and only then is the file then fed up by PHP to be downloaded. What I am trying to do is combine both the archive and output steps into one single process.

Does anyone have any input on this?

Here is someone else on SourceForge who is trying to do the same thing...

http://sourceforge.net/projects/p7zip/forums/forum/383043/topic/3828566

=======

Thank you for all help. I am going to close this thread, as I have recently discovered an excellent discussion on this topic already exists on StackOverflow. See:

LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing

=======

This is the final solution I went with, and it is working fantastic for me. Note that I am using the "InfoZip" program, which you can find for pretty much any platform.

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename="file.zip"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

passthru("zip -8 -q -r - ./some_directory | cat");
Was it helpful?

Solution

You could try running 7z using passthru with the -so (write data to stdout) option.

As long as you are sending download headers and content-type headers for 7zip mime types, I believe it will work.

Example

<?php
$filename = "somefile.7z";
$contentType = "application/x-7z-compressed";
header("Content-type: ".$contentType); // tell the browser your sending a 7zip file
header("Content-Disposition: attachment; filename=\"".$filename."\""); // use 'attachment' to force a download
passthru('/path/to/7z a -so <fileOrDirToBe7Zipped>');
?>

Hope that helps!

OTHER TIPS

This is the final solution I went with, and it is working fantastic for me. Note that I am using the "InfoZip" program, which you can find for pretty much any platform.

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename="file.zip"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

passthru("zip -8 -q -r - ./some_directory | cat");

This is either not going to work or it will be horribly buggy. The web doesn't really work like this -- many browsers won't be able to correctly deal with an incomplete file (which is what they're going to be getting). Why not display a "Your file is being generated! We will email you a download link when it is finished." message, or a "Please wait, we are generating your file. This could take several minutes." message, along with a progress meter?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top