Last active
October 12, 2019 18:33
-
-
Save yukin01/f2580856b3875cf9b763541cb3ae2225 to your computer and use it in GitHub Desktop.
Remove root directory from zip file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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