Last active
May 20, 2022 04:06
-
-
Save Houserqu/8d8dd1c703b8ea999a324934592da92c to your computer and use it in GitHub Desktop.
node zip 解压缩
This file contains hidden or 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 * as archiver from 'archiver'; | |
import * as fs from 'fs' | |
import * as unzipper from 'unzipper' | |
/** | |
* 根据目录压缩 zip 文件 | |
* @param filePath | |
* @param folderPath | |
* @returns | |
*/ | |
export function zipFolder(outputFilePath: string, folderPath: string) { | |
return new Promise((resolve, reject) => { | |
const output = fs.createWriteStream(outputFilePath); | |
const archive = archiver('zip', { | |
zlib: { level: 9 } // Sets the compression level. | |
}); | |
output.on('close', function() { | |
resolve(outputFilePath); | |
}); | |
archive.on('error', function(err) { | |
reject(err) | |
}); | |
archive.pipe(output); | |
archive.directory(folderPath, '/'); | |
archive.finalize(); | |
}) | |
} | |
/** | |
* zip 解压 | |
* @param filePath | |
* @param output | |
*/ | |
export async function unzip(filePath: string, output: string) { | |
await fs.createReadStream(filePath) | |
.pipe(unzipper.Extract({ | |
path: output | |
})) | |
.on('entry', entry => entry.autodrain()) | |
.promise() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment