Git Product home page Git Product logo

js-unzip's Introduction

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 <[email protected]>. Licensed under the MIT license.

js-unzip's People

Contributors

augustl avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.