unzip .ZIP files in the browser
npm install unzip-js
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 () { ... })
})
})
})
})
unzip(source, callback)
source
: must be aBlob
,File
orstring
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
iftrue
, 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.
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.
Firefox | Chrome | Internet Explorer | Edge | Safari | ios | Android |
---|---|---|---|---|---|---|
YES | YES | YES (10 - 11) | YES | YES | YES | YES |
MIT. Copyright (c) Merzouqi.