【问题标题】:Create a zip from folders retaining file structure including parent folders (and single files)从保留文件结构的文件夹创建一个 zip,包括父文件夹(和单个文件)
【发布时间】:2018-09-24 07:23:31
【问题描述】:

我正在尝试从以下文件结构创建一个 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

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

谢谢!

【问题讨论】:

    标签: gulp zip glob directory-structure gulp-zip


    【解决方案1】:

    我试过了:

    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 option。使用{base: '.'} 基本上告诉gulp 使用dest 位置中的所有目录。否则,默认操作是删除 glob 之前的目录。因此,在 'dist/**/*.*' 中,如果没有 base 选项,dist 文件夹将不会保留。

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

    【讨论】:

    • 有效!谢谢!关于游戏文件夹,我认为将 zip 文件夹解压缩到具有 zip 文件本身名称的文件夹是默认行为。事实证明,我通常通过选择文件和文件夹并右键单击手动创建的 zip 文件具有相同的行为。无论如何,它在我的 Mac 上也是如此。
    【解决方案2】:

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

    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();
    

    发件人:Archiver js Docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多