Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unzipping a 0 compression ZIP which contains another 0 compression ZIP results in the stream being closed prematurely #239

Open
joshuakitchen opened this issue May 18, 2021 · 0 comments

Comments

@joshuakitchen
Copy link

I am currently using unzipper to unzip a ZIP file which has been created using 0 compression and also contains another ZIP which contains 0 compression.

Due to unzipper looking for the EOF (0x08074b50), it finds this prematurely due to the internal ZIP and closes the stream early.

Recreation code using Archiver:

'use strict'

const archiver = require('archiver')
const fs = require('fs')
const unzipper = require('unzipper')

const createArchive = function createArchive() {
  return new Promise(function _createArchivePromise (resolve, reject) {
    try {
      const fileReadStream = fs.createReadStream('./image.png')
      const zipWriteStream = fs.createWriteStream('./test.zip')

      const archive = archiver('zip', {
        zlib: {
          level: 0
        }
      })

      archive.pipe(zipWriteStream)

      archive.append(fileReadStream, { name: 'image.png' })
      archive.append(fs.createReadStream('internal.zip'), { name: 'internal.zip' })
      archive.append(fs.createReadStream('internal.zip'), { name: 'internal (1).zip' })
      archive.append(fs.createReadStream('internal.zip'), { name: 'internal (2).zip' })
      archive.append(fs.createReadStream('internal.zip'), { name: 'internal (3).zip' })
      archive.append(fs.createReadStream('internal.zip'), { name: 'internal (4).zip' })
      archive.append(fs.createReadStream('internal.zip'), { name: 'internal (5).zip' })
      archive.finalize()

      archive.on('finish', function () {
        resolve()
      })
    } catch (err) {
      reject(err)
    }
  })
}

const main = async function main () {
  await createArchive()

  const readStream = fs.createReadStream('./test.zip')
  readStream.pipe(unzipper.Parse({
    verbose: true
  })).on('error', function _onError(err) {
    console.error(err)
  }).on('entry', function _onEntry (entry) {
    const writeStream = fs.createWriteStream(`./unzipped/${entry.path}`)
    writeStream.on('error', function _onError(err) {
      console.error(err)
    })
    entry.pipe(writeStream)
  }).on('finish', function _onFinish () {
    console.log('done')
    // fs.promises.unlink('./test.zip').catch(console.error)
  })
}

main().catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant