Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Zipping files before download...

Status
Not open for further replies.

Elliott3

Programmer
Sep 5, 2002
347
0
0
CA
Hi,
Is there a simple way to zip a couple files on the server before the user downloads them? Can anyone point me in the right direction?

CES
 
You can zip individual files (not multiple files together) with this method:

These 2 extensions allow you to write "Zip"'d content to either a file, or to the output stream in a browser so the user could download that "file". Basically, you would read in your normal uncompressed files one at a time using fread() or something like that, and then run the compression, and output that data, along with the appropriate header() for Content-Type, etc, and the user would get the dynamically created file which they could "save" to their computer.



--> use bzcompress(), etc


--> use gzencode(), etc

NOTE: neither of these creates an actual .ZIP file... but they are both zip file formats which can be read by most popular zip programs, like WinZip.


Otherwise, if you must ZIP multiple files together, preserving the files, then the only option I see is to have your PHP script execute (via system(), exec() or the `` backtick operators) the server's zip command (in unix, something like "tar -zcvf stuff.tar.gz stuff.txt stuff2.doc"). This would create a temporary file, which you could then read in via the above zip libraries (using the xxread() functions) and then dump that output straight out to the browser (again with appropriate content-type headers). Then, when the script was done executing, you could have your PHP delete the temp file.
 
DRJ478--

I started to suggest that ob-gzhandler to Elliott3 but if you read the question closer, they are asking for the ability to zip multiple FILES (not content) together, and then presumably present that zip'd archive file to the user for download.

Perhaps it's to present downloads of certain sets of source files or fonts or other things of that nature, where the application needs to determine what to zip at run time, rather than pre-zipping certain archives and then the user just downloading them directly.
 
I am coming back to post an update to what I suggested earlier, based on a great article I just read in php|Architect magazine (I would HIGHLY recommend getting a subscription, it's WELL worth the money).

They have a whole article about using the PHP Streams API to generate .ZIP files on the fly, exactly for the types of things you mention in this question.

Since it's a whole article, and it's copyrighted, I'm not gonna present all the info here. BUT, you're in luck... because this article happens to be the "free" sample article they've released from this magazine publicly on their website.


I would highly suggest, in all fairness to PHP Architect, that you at least purchase the single Oct-2004 issue, especially if you find this article helpful. But nevertheless, you should be able to peruse that link and see the description of how to accomplish what you're looking for.

Happy coding!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top