Giter VIP home page Giter VIP logo

unzip-js's Introduction

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

Firefox Chrome Internet Explorer Edge Safari ios Android
YES YES YES (10 - 11) YES YES YES YES

License

MIT. Copyright (c) Merzouqi.

unzip-js's People

Contributors

merzouqi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

nobey match08

unzip-js's Issues

Handle error for error with GET request

It looks like the intention was to call the callback function for error handling if a URL string was passed to unzip. However if there is an error like 404 (Not Found), there is still an error thrown on the xhr.send() line.

try { xhr.open('GET', source) xhr.responseType = 'blob' xhr.send() } catch (err) { callback(err) }

I think that to properly catch the error, you need to add a handler like this: xhr.addEventListener('error', handleEvent); since the error is asynchronous and the try block has already successfully executed the send() command before the error is generate.

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.