从保留文件结构的文件夹创建 zip,包括父文件夹(和单个文件)



我正在尝试从以下文件结构创建一个zip文件:

-dist/bundle.js
-assets/[several subfolders with files]
-config.json
-bootstrap.js

我用过大口大口的拉链:

gulp.task('zip', ()=>{
return gulp.src(['dist/**/*.*', 'assets/**/*.*','config.json', 'bootstrap.js'])
.pipe(zip('game.zip'))
.pipe(gulp.dest('deploy'))
})

这导致: 游戏.zip具有以下结构:

-game
--[some assets subfolder]
--[other assets subfolder]
--[third assets subfolder]
--bundle.js
--bootstrap.js
--config.json

文件/文件夹不应位于文件夹(游戏(中,而应保留它们最初具有的结构,资产和 dist 文件夹也应位于结构中。 欢迎我从我的package.json脚本节点运行的任何解决方案。(gulp/webpack/grunt/whatever(

谢谢!

我试过这个:

gulp.task('default', ()=>{
return gulp.src(['dist/**/*.*', 'assets/**/*.*','config.json', 'bootstrap.js'], {base: '.'})
.pipe(zip('game.zip'))
.pipe(gulp.dest('deploy'))
})

只需将{base: '.'}选项添加到gulp.src即可完成您想要的操作。 请参阅吞咽底座选项。使用{base: '.'}基本上告诉 gulp 使用dest位置中的所有目录。 否则,默认操作是删除 glob 之前的目录。 因此,在 'dist/**/*.*' 中,如果没有 base 选项,dist文件夹将不会保留。

我不知道你从哪里得到game文件夹,我从来没有。

只是想发布我在搜索网络时发现的另一个解决方案(仍然接受 Mark 的解决方案,因为它更短/更简单:

const fs = require('fs');
const archiver = require('archiver');
const output = fs.createWriteStream(__dirname + '/deploy/rosa.zip');
const archive = archiver('zip', {
store: true
//zlib: { level: 9 } // Sets the compression level.
});
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has 
closed.');
});
// This event is fired when the data source is drained no matter what was the 
data source.
// It is not part of this library but rather from the NodeJS Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
output.on('end', function() {
console.log('Data has been drained');
});
// good practice to catch warnings (ie stat failures and other non-blocking 
errors)
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
// log warning
} else {
// throw error
throw err;
}
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
archive.directory('assets/', 'assets');
archive.directory('dist/', 'dist');
archive.file('bootstrap.js', {name: 'bootstrap.js'});
archive.file('config.json', {name: 'config.json'});
archive.finalize();

来自: 存档器 js 文档

相关内容

最新更新