Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Unzip files with JavaScript

branch: master
README.textile

JSUnzip

A javascript library for reading the contents of zip files.

NOTE: Does not uncompress/inflate files. Zip is a package format that can use many different compression methods. See “Working with JSUnzip.ZipEntry” below.

var myZip = ... // Get it with an XHR request, HTML5 files, etc.
var unzipper = new JSUnzip(myZip);
unzipper.isZipFile();      // true or false

unzipper.readEntries();    // Creates "entries"
unzipper.entries;          // Array of JSUnzip.ZipEntry objects.

The test suite runs on Chrome 4, FireFox 3.6, IE7, Opera 10 and Safari 4.0.4. [TODO: Run tests on more browsers.]

Download

http://github.com/downloads/augustl/js-unzip/js-unzip.min.js

Working with JSUnzip.ZipEntry objects

After readEntries is called, an array of JSUnzip.ZipEntry objects is created, one per file in the Zip archive.

entry = myZip.entries[0];

// Attributes
entry.fileName;          // The file name of the entry. Contains the full path.
                         // Examples:
                         //   "foo.txt"
                         //   "directory/bar.jpg"
entry.data;              // The raw uncompressed data
entry.compressionMethod; // Number representing compression method.
                         //   1: No compression. File can be used as-is.
                         //   8: DEFLATE. The most common compression method.
                         //      Use a inflate algorithm to uncompress, such
                         //      as http://github.com/augustl/js-inflate/
entry.compressedSize;    // The size of the commpressed data
entry.uncompressedSize;  // The size of the data when it's uncompressed
entry.signature;         // The magic number used to determine if it is in fact
                         // a zip file.
entry.versionNeeded;     // Zip specification version needed to work with the file.
entry.bitFlag;           // Flag for various states

// Functions (mostly for internal use)
entry.isEncrypted();
entry.isUsingUtf8();
entry.isUzingZip64();    // Zip64 is for 4gb+ files. Not supported by this lib.

See http://www.pkware.com/documents/casestudies/APPNOTE.TXT for more information about the Zip format, such as all the compression methods.

Uncompressing with JSInflate

Almost all Zip files are compressed with the deflate algorithm. You can use JSInflate to uncompress these Zips.

var blob = ...; // A HTML5 binary file, for example.
var unzipper = new JSUnzip(blob);
if (unzipper.isZipFile()) {

  unzipper.readEntries();

  for (var i = 0; i < unzipper.entries.length; i++) {
    var entry = unzipper.entries[i];
    if (entry.compressionMethod === 0) {
      // Uncompressed
      var uncompressed = entry.data; 
    } else if (entry.compressionMethod === 8) {
      // Deflated
      var uncompressed = JSInflate.inflate(entry.data);
    }
  }
}

Credits

About

Written by August Lilleaas <august.lilleaas@gmail.com>. Licensed under the MIT license.

Something went wrong with that request. Please try again.