如何压缩多个文件夹在gulp中生成多个.zip文件? [英] How to zip multiple folders generating multiple .zip files in gulp?

查看:983
本文介绍了如何压缩多个文件夹在gulp中生成多个.zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件夹结构如下所示:

   -  / projects 
- / projects / proj1
- / projects / proj2
- / projects / proj3
- / zips

对于项目(proj1,proj2和proj3)中的每个文件夹,我想压缩每个文件夹的内容并生成 proj1.zip proj2.zip proj3.zip 位于 / zips

以下示例函数根据 proj1 文件夹生成单个zip文件

  zip = require('gulp-zip'); 
gulp.task('default',function(){
return gulp.src('./ projects / proj1 / *')
.pipe(zip('proj1.zip')) )
.pipe(gulp.dest('./ zips'));
});

但是我怎么能为项目中的每个文件夹执行这样的任务?我可以通过 gulp.src('./projects/ *')来压缩所有文件夹,但是那又如何?

解决方案

老问题我知道,而且我很新,所以这可能被认为是黑客攻击,但我一直在努力做你正在做的事情,并最终与此相关。 / p>

我的文件结构如下:

  proj 
proj / dist
proj / dist / sub01
proj / dist / sub02
proj / dist / sub03
proj / zip

我的任务最终如下所示:

  var gulp =要求( 吞掉); 
var foreach = require(gulp-foreach);
var zip = require(gulp-zip);
$ b gulp.task(zip-dist,function(){
return gulp.src(./ dist / *)
.pipe(foreach(function流文件){
var fileName = file.path.substr(file.path.lastIndexOf(/)+ 1);
gulp.src(./ dist /+ fileName +/ ** / *)
.pipe(zip(fileName +。zip))
.pipe(gulp.dest(./zipped));

return流;
}));
});

它抓取 ./ dist 作为其来源,然后将其管理为 gulp-foreach

gulp-foreach 查看每个项目,我使用普通的javascript substr()来获取我存储为变量的当前项目的名称。



最后我设置了一个新的 src 使用存储的fileName var并将结果传递给 gulp-zip 再次使用存储的变量作为压缩文件的名称。



结果是这样的结构:

  proj 
proj / dist
proj / dist / sub01
proj / dist / sub02
proj / dist / sub03
proj / zipped
proj / zipped / sub01.zip
proj / zipped / sub02.zip
proj / zipped / sub03.zip

再次,我是一百万m我不是专家,但是这对我很有用,如果我明白这个问题可能对你有用,或者至少给你一些想法。


My folder structure looks like this:

- /projects
- /projects/proj1
- /projects/proj2
- /projects/proj3
- /zips

For each folder in projects (proj1, proj2 and proj3) I want to zip contents of each of those folders and generate proj1.zip, proj2.zip and proj3.zip in /zips folder.

Following example function generates single zip file from proj1 folder

zip = require('gulp-zip');
gulp.task('default', function () {
    return gulp.src('./projects/proj1/*')
        .pipe(zip('proj1.zip'))
        .pipe(gulp.dest('./zips'));
});

But how I can execute such task for each folder in projects? I can get all folders to zip by gulp.src('./projects/*') but what then?

解决方案

Old question I know and I am pretty new to gulp so this might be considered a hack but I have just been trying to do what you are after and I ended up with this.

My file structure is this:

proj
proj/dist
proj/dist/sub01
proj/dist/sub02
proj/dist/sub03
proj/zipped

And my task ended up like this:

var gulp = require("gulp");
var foreach = require("gulp-foreach");
var zip = require("gulp-zip");

gulp.task("zip-dist", function(){
   return gulp.src("./dist/*")
       .pipe(foreach(function(stream, file){
          var fileName = file.path.substr(file.path.lastIndexOf("/")+1);
          gulp.src("./dist/"+fileName+"/**/*")
              .pipe(zip(fileName+".zip"))
              .pipe(gulp.dest("./zipped"));

          return stream;
       }));
});

It grabs all the first level contents of ./dist as its source and then pipes it to gulp-foreach.

gulp-foreach looks at each item and I use a plain javascript substr() to get the name of the current item which I store as a variable.

Finally I set a new src using the stored fileName var and pipe the result to gulp-zip using the stored var again as the name of the zipped file.

The result is a structure that looks like this:

proj
proj/dist
proj/dist/sub01
proj/dist/sub02
proj/dist/sub03
proj/zipped
proj/zipped/sub01.zip
proj/zipped/sub02.zip
proj/zipped/sub03.zip

Again, I am a million miles from being an expert but this worked for me and if I understand the question might work for you as well or at least give you some ideas.

这篇关于如何压缩多个文件夹在gulp中生成多个.zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆