-1

I am trying to get several resource files from the server using one http request. The set of resource files is different on every http request. So the zip file needs to be created dynamically.

The best idea I have so far is to:

step1: Create a zip file containing the desired resource files using php. Similar to this http://davidwalsh.name/create-zip-php

step2: send the .zip file to the client in the browser and parse the files.

Is this the best way to do this? How do I pass a zip file to javascript and how can I store the contents of the zip file?

Jose Calderon
  • 551
  • 2
  • 5
  • 11
  • i think zip might even be the only way to do in one request, beside other compressed formats ;) – Dukeatcoding Feb 11 '13 at 01:11
  • any idea how to handle a zip file on client side? – Jose Calderon Feb 11 '13 at 01:13
  • Creating these files dynamically will put some serious stress on your server, it will be better to prepare then. However, Javascript has no built in support for zipfiles, look [here](http://stackoverflow.com/questions/2095697/unzip-files-using-javascript) for a pure javascript solution, but check the entire post, it's tremendously slow. There must be better solutions for your problem. – fvu Feb 11 '13 at 01:14
  • What kind of resources are we talking about? – fvu Feb 11 '13 at 01:14
  • Looks like a duplicate of: http://stackoverflow.com/questions/1041542/how-to-download-multiple-files-with-one-http-request – Boundless Feb 11 '13 at 01:15
  • You are rolling in the deep my friend, maybe this would help: http://stackoverflow.com/questions/2095697/unzip-files-using-javascript – Tomas Ramirez Sarduy Feb 11 '13 at 01:15
  • I need to get 30 .jpgs from the server on each request. Each request is for a completely different set of 30 .jpgs. – Jose Calderon Feb 11 '13 at 01:18
  • I want to display the .jpgs in the html – Jose Calderon Feb 11 '13 at 01:18
  • a zip or tarball would work fine. unzip into memory, then use *data urls* to display them. Although, if you don't mind me asking, why must you retrieve them all in one http request? if they're always just images, consider using sprites(combine into 1 big image, use cropping/positioning on the client side to isolate the individual images). – goat Feb 11 '13 at 01:23
  • I thought it would be less work for the server to do it this way. – Jose Calderon Feb 11 '13 at 01:25
  • 1
    it would be less work for the server if the zips are pregenerated. otherwise, you will introduce huge overhead. you're pretty much guaranteed to destroy browser performance in either case. you also forfeit http caching. you would be wise to drop this idea since you have no good reason to implement it. if you feel like doing something random to help your server, study the topic of http caching. – goat Feb 11 '13 at 01:30

1 Answers1

0

HTTP already supports gzip compression:

GZIP is usually more effective at compressing redundancies in files, which produces better compression when you have files that contain similar or repetitive information.

GZIP Support: http://www.vervestudios.co/projects/compression-tests/results

Since you need multiple files, you should tar them together before gzipping them, which you can do with PHP and PEAR's tar archive support. Then you can un-tar the files with javascript and the browser automagically handles gzip decompression.

Here's an article on the difference between zip and gzip:

Summary:

  1. GZIP can achieve better compression compared to ZIP.

  2. ZIP is capable of archiving and compressing multiple files, while GZIP is only capable of compression.

  3. You can easily extract individual files from a large ZIP file, but not from a GZIP tarball.

  4. ZIP is fairly popular on Windows, while GZIP is more popular on UNIX-like operating systems.

http://www.differencebetween.net/technology/difference-between-zip-and-gzip/

Alex W
  • 37,233
  • 13
  • 109
  • 109