Skip to content

Instantly share code, notes, and snippets.

@tlbdk
Created May 23, 2019 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlbdk/c67e95b29b2d2bbbea0e8fd2dff729c9 to your computer and use it in GitHub Desktop.
Save tlbdk/c67e95b29b2d2bbbea0e8fd2dff729c9 to your computer and use it in GitHub Desktop.
const zlib = require('zlib')
const fs = require('fs')
if (process.argv.length < 3) {
console.error('./unpack.js filename')
process.exit(255)
}
let filePath = process.argv[2]
async function main() {
let fd = fs.openSync(filePath, 'r')
let header = Buffer.alloc(1024)
fs.readSync(fd, header, 0, 1024, 0)
let fileNameLength = header.readUInt16LE(26)
let extraFieldLength = header.readUInt16LE(28)
let compressedSize = header.readUInt32LE(18)
let unCompressedSize = header.readUInt32LE(22)
console.log(`${compressedSize}, ${unCompressedSize}`)
let fileName = header.slice(30, 30 + fileNameLength)
console.log(`fileNameLength: ${fileNameLength}: ${fileName}`)
console.log(`extraFieldLength: ${extraFieldLength}`)
let extractedSize = 0
let inflater = zlib.createInflateRaw()
let lastChunk = null
inflater.on('data', chunk => {
lastChunk = chunk
extractedSize += chunk.length
if (extractedSize % (1024 * 1024 * 1024) === 0) {
console.log(`${(extractedSize / 1024 / 1024).toFixed(2)} MB`)
}
})
inflater.on('error', err => {
console.error(err)
})
inflater.on('end', () => {
console.log(lastChunk.toString('utf8'))
console.log(`done after: ${extractedSize}`)
})
// Write the first part of the stream that we read with the header
let data = header.slice(30 + fileNameLength + extraFieldLength)
inflater.write(data)
let zipStream = fs.createReadStream(filePath, { start: header.length })
zipStream.pipe(inflater)
inflater.writableHighWaterMark
}
main().catch(err => {
console.log(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment