archiver
archiver是一个用于压缩文件和文件夹的Node.js模块,可以将文件和文件夹打包成常见的压缩格式(如ZIP、TAR、GZIP等),也可以将已经压缩的文件解压缩。
压缩文件
// 将dist文件夹下的spa文件夹压缩成war文件
const fs = require('fs')
const archiver = require('archiver')
function zipFolderToWar(folderPath, warPath, callback) {
const output = fs.createWriteStream(warPath)
const archive = archiver('zip', { zlib: { level: 9 } })
output.on('close', () => {
console.log('WAR archive created successfully!');
callback(null);
})
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.warn('Warning:', err)
} else {
callback(err)
}
})
archive.on('error', (err) => {
callback(err)
})
archive.pipe(output)
archive.directory(folderPath, false)
archive.finalize()
}
const folderPath = './dist/spa'
const warPath = 'D:/test-web.war'
zipFolderToWar(folderPath, warPath, (err) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Folder zipped to WAR format successfully!')
}
})