Open3

npm packages memo

erukitierukiti

tar を展開する

import zlib from 'zlib'

import tar from 'tar-stream'

const extractTar = (tarball: NodeJS.ReadableStream) => {
  return new Promise((resolve, reject) => {
    const files: { [props: string]: string } = {}
    const gunzip = zlib.createGunzip({})
    tarball
      .pipe(gunzip)
      .on('error', err => reject(err))
      .pipe(tar.extract())
      .on('error', err => reject(err))
      .on('finish', () => resolve(files))
      .on('entry', (headers, stream, next) => {
        const { name } = headers
        if (!(name in files)) {
          files[name] = ''
        }
        stream.on('data', data => {
          files[name] += data.toString()
        })
        stream.on('error', err => reject(err))
        stream.on('end', () => next())
      })
  })
}
erukitierukiti

zipを展開する

import JSZip from 'jszip'

const extractZip = async (buf: ArrayBuffer) => {
  const tasks: Promise<{ buf: Buffer; name: string }>[] = []
  const zip = new JSZip()
  await zip.loadAsync(buf)
  zip.forEach((relPath, file) => {
    tasks.push(
      new Promise((resolve, reject) => {
        let buf = new Buffer('')
        const st = file.nodeStream()
        st.on('data', (data: any) => (buf = Buffer.concat(data)))
        st.on('error', err => reject(err))
        st.on('end', () => resolve({ buf, name: relPath }))
      }),
    )
  })
  return Promise.all(tasks)
}