Skip to content

Instantly share code, notes, and snippets.

@yukin01
Last active October 12, 2019 18:33
Show Gist options
  • Save yukin01/f2580856b3875cf9b763541cb3ae2225 to your computer and use it in GitHub Desktop.
Save yukin01/f2580856b3875cf9b763541cb3ae2225 to your computer and use it in GitHub Desktop.
Remove root directory from zip file
import { readFile, writeFile } from 'fs'
import { promisify } from 'util'
import * as JSZip from 'jszip'
const main = async () => {
const oldPath = 'old.zip'
const oldBuf = await promisify(readFile)(oldPath)
const oldZip = await JSZip.loadAsync(oldBuf)
const files = oldZip.files
const newZip = new JSZip()
const promises = Object.keys(files)
.filter(key => !files[key].dir)
.map(async key => {
const file = files[key]
const buffer = await file.async('nodebuffer')
const path = file.name
.split('/')
.slice(1)
.join('/')
newZip.file(path, buffer)
})
await Promise.all(promises)
const newBuf = await newZip.generateAsync({ type: 'nodebuffer' })
const newPath = 'new.zip'
await promisify(writeFile)(newPath, newBuf)
}
main()
.then(console.log)
.catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment