1.0.0 • Published 5 years ago

unzip-js v1.0.0

Weekly downloads
65
License
MIT
Repository
github
Last release
5 years ago

unzip-js

Build Status Coverage Status npm JavaScript Style Guide

unzip .ZIP files in the browser

Install

npm install unzip-js

Usage

var unzip = require('unzip-js')

unzip(getBlobOrFileSomehow(), function (err, zipFile) {
  if (err) {
    return console.error(err)
  }

  zipFile.readEntries(function (err, entries) {
    if (err) {
      return console.error(err)
    }

    entries.forEach(function (entry) {
      zipFile.readEntryData(entry, false, function (err, readStream) {
        if (err) {
          return console.error(err)
        }

        readStream.on('data', function (chunk) { ... })
        readStream.on('error', function (err) { ... })
        readStream.on('end', function () { ... })
      })
    })
  })
})

API

unzip(source, callback)

  • source: must be a Blob, File or string that represents a valid url.
  • callback: function(err: Error, zipFile: ZipFile)

Class: ZipFile

zipFile.readEntries(callback)\ Read metadata of all entries (parse Central Dircotroy Records).

  • callback: function(err: Error, entries: Entry[])

zipFile.readEntryData(entry, checkCrc, callback)\ Read data of a specific entry.

  • entry: Entry
  • callback: function(err: Error, readStream: ReadStream)
  • checkCrc: boolean if true, performs a crc32 check on the extracted data.

Class: Entry\ A representation of a zip entry (a Central Dircotroy Record).

see the zip spec section 4.3.12 Central directory structure for more informations.

entry.versionMadeBy: number\ entry.versionNeededToExtract: number\ entry.generalPurposeBitFlag: number\ entry.compressionMethod: number\ entry.lastModTime: number\ entry.lastModDate: number\ entry.crc32: number\ entry.compressedSize: number\ entry.uncompressedSize: number\ entry.internalAttributes: number\ entry.externalAttributes: number\ entry.localHeaderOffset: number\ entry.name: string\ entry.commment: string\ entry.extraFields: object[]

  • object: { headerId: number, dataSize: number, data: Buffer }

entry.encrypted: boolean\ retrun true if the data is encrypted.

entry.lastModTimeDate: Date\ Return a Date object representation of entry.lastModTime and entry.lastModDate.

Class: ReadStream\ see blob-slicer.

Limitations

Only files stored with no compression or compressed with deflate algorithm are supported, zipFile.readEntryData callback will recieve an error for any entry with compression methode other than 0 (stored) or 8 (deflated), or if the file is encrypted.

Zip files spanned across multiple removable media are not supported.

Supported browsers

FirefoxChromeInternet ExplorerEdgeSafariiosAndroid
YESYESYES (10 - 11)YESYESYESYES

License

MIT. Copyright (c) Merzouqi.