0

I need a way to download a lot of files and save them to the file system with PHP. This is for a deployment tool, and I was planning on using a Zip file, since I just call some unzip code and it's simple. However, the ZIP functionality is an extension that not everyone has.

My only other idea right now is to compress everything into one file as Base64 encoded, decode it and write it back out. This isn't ideal (Or is it?). Suggestions?

Brandon
  • 16,382
  • 12
  • 55
  • 88
  • Will this website have access to bash? Would it be possible to use tar? – Prisoner Mar 21 '11 at 00:55
  • Doubtful as this will be distributed to a large number of people with many using shared hosting. Some may even be on Windows. – Brandon Mar 21 '11 at 00:59

2 Answers2

2

Zip is really not difficult to parse, if you can follow a rather simple file format specification and are willing to count on the zlib extension to be installed to handle the decompression.

Otherwise, invent your own custom archive file format, it's not hard. It could be something a dead simple as this:

  • First 4 bytes: Number of files in the archive
  • Next 4 bytes: Number of bytes in the filename
  • Next N bytes: Filename
  • Next 10 bytes: Number of bytes in the file
  • Next N bytes: File contents
  • Repeat the above 4 lines as necessary

So a very simple archive file might look like

00020007foo.txt0000000012Hello world!0007bar.txt0000000010It worked!

You could of course improve it by storing lengths in binary format (e.g. using pack with the 'N' format), adding checksums, and so on. But it was easier to explain this way.

Anomie
  • 92,546
  • 13
  • 126
  • 145
0

base64 just translates a chunk of data into a format that's "safe" to transmit via email, which can then be decode back into its native 8bit format. It doesn't allow multiple files to be archived/compressed into a single file. You could embed multiple seperate base64-encoded files in something like a MIME message, which does allow multiple different attachments. But then you're back to the same problem - not everything will natively handle MIME messages, since that's the domain of email.

Zip is your best choice. There's versions of it available for every major OS under the sun, and is built into the file managers for the big 2 - MacOS and Windows. The other alternatives (rar, 7z, etc..) are far less common and definitely have no chance of out-of-the-box support.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • base64 allows the file to be stored as plaintext basically, which means you could create a parser that could decode a file containing multiple base64 encoded files. – Brandon Mar 21 '11 at 01:00
  • at which point you might as well just use a MIME framework to store those files. THe whole point is that it has to be something that's available on client machines without installing more stuff, and a custom-built base64 container handler definitely doesn't fit the bill – Marc B Mar 21 '11 at 01:01
  • @Marc B I guess I didn't explain myself properly sorry. This is for an auto-upgrade process, meaning my code is already installed on the machine. So I could easy have my base64 container :) – Brandon Mar 21 '11 at 01:05
  • Ah, well, in that case, you'd want to avoid base64. It significantly increases the size of anything you run through it, because it's basically turning 8bit data into 6bits strings, which are padded back out to 8bit ascii chars. Basically a 33% increase. You can trivially embed a zip library into most any program these days. – Marc B Mar 21 '11 at 01:07
  • @Marc B I am aware of the size problems, however I wasn't able to find a Pure PHP implementation of an unzip libary. Do you have any links? – Brandon Mar 21 '11 at 01:09
  • I ran the code using Base64 and then gzdeflate (More common than the zip lib) and it is the same filesize as a zip file. Any problems with this? – Brandon Mar 21 '11 at 01:13
  • Should work ok. gzdeflate's a standard PHP component, so most places with PHP should also have the zlib functions. – Marc B Mar 21 '11 at 01:17