Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Crazy JavaScript hack: unzipping a file

DZone's Guide to

Crazy JavaScript hack: unzipping a file

· Web Dev Zone
Free Resource

Try RAD Studio for FREE!  It’s the fastest way to develop cross-platform Native Apps with flexible Cloud services and broad IoT connectivity. Start Your Trial Today!

Handling binary data is slowly getting better in JavaScript, but in most browsers, people still have to resort to amazingly clever hacks when it comes to things like unzipping a file stored on a server. Here are the steps that are involved (slide 38). I suspect that you will never need to do this, but it is still fun to see what is possible.

  • Read the binary data of the ZIP file from the server via XMLHttpRequest. You need to specify a charset x-user-defined to ensure that each byte stays a single character.
  • Translate the binary data to base 64 and turn it into a PNG data URI. PNG is zipped internally, so if you get the header right, the web browser will unzip the data as soon as the image is drawn somewhere.
  • Draw the image into a Canvas object, with a height of 1 pixel. Read the uncompressed data from the Canvas, pixel by pixel.

Get Your Apps to Customers 5X Faster with RAD Studio. Brought to you in partnership with Embarcadero.

Topics:

Published at DZone with permission of Axel Rauschmayer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}