Создайте zip из папок, сохраняя файловую структуру, включая родительские папки (и отдельные файлы)
Я пытаюсь создать ZIP-файл из следующей файловой структуры:
-dist/bundle.js
-assets/[several subfolders with files]
-config.json
-bootstrap.js
Я использовал gulp-zip:
gulp.task('zip', ()=>{
return gulp.src(['dist/**/*.*', 'assets/**/*.*','config.json', 'bootstrap.js'])
.pipe(zip('game.zip'))
.pipe(gulp.dest('deploy'))
})
что приводит к: game.zip с этой структурой:
-game
--[some assets subfolder]
--[other assets subfolder]
--[third assets subfolder]
--bundle.js
--bootstrap.js
--config.json
Файлы / папки не должны находиться в папке (игре), но должны сохранять структуру, которую они изначально имеют, также должны быть в структуре папки ресурсов и дистрибутивы. Любое решение, которое я могу запустить из моего узла скриптов package.json, будет приветствоваться. (Глоток / WebPack/ пехотинец / все)
Спасибо!
2 ответа
Я попробовал это:
gulp.task('default', ()=>{
return gulp.src(['dist/**/*.*', 'assets/**/*.*','config.json', 'bootstrap.js'], {base: '.'})
.pipe(zip('game.zip'))
.pipe(gulp.dest('deploy'))
})
Просто добавив {base: '.'}
опция gulp.src делает то, что вы хотите. Смотрите опцию gulp base. С помощью {base: '.'}
в основном говорит глотку использовать все каталоги в dest
место нахождения. В противном случае по умолчанию выполняется удаление каталогов перед глобусами. Итак, вdist/**/*.*
' dist
папка не будет сохранена без базовой опции.
Я не знаю, где вы получите game
Папка, я никогда не делаю.
Просто хотел опубликовать другое решение, которое я нашел при поиске в Интернете (все еще принимая решение Марка, поскольку оно намного короче / проще:
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();