如何压缩多个文件夹在gulp中生成多个.zip文件? [英] How to zip multiple folders generating multiple .zip files in gulp?
问题描述
- / projects
- / projects / proj1
- / projects / proj2
- / projects / proj3
- / zips
对于 以下示例函数根据 但是我怎么能为项目中的每个文件夹执行这样的任务?我可以通过 老问题我知道,而且我很新,所以这可能被认为是黑客攻击,但我一直在努力做你正在做的事情,并最终与此相关。 / p> 我的文件结构如下: 我的任务最终如下所示: 它抓取 最后我设置了一个新的 结果是这样的结构: 再次,我是一百万m我不是专家,但是这对我很有用,如果我明白这个问题可能对你有用,或者至少给你一些想法。 My folder structure looks like this: For each folder in Following example function generates single zip file from But how I can execute such task for each folder in projects? I can get all folders to zip by 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: And my task ended up like this: It grabs all the first level contents of Finally I set a new The result is a structure that looks like this: 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屋!项目
(proj1,proj2和proj3)中的每个文件夹,我想压缩每个文件夹的内容并生成 proj1.zip
, proj2.zip
和 proj3.zip
位于 / zips $ c
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/ *')
来压缩所有文件夹,但是那又如何?
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 $ c的所有第一级内容$ c>作为其来源,然后将其管理为
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
- /projects
- /projects/proj1
- /projects/proj2
- /projects/proj3
- /zips
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.proj1
folderzip = require('gulp-zip');
gulp.task('default', function () {
return gulp.src('./projects/proj1/*')
.pipe(zip('proj1.zip'))
.pipe(gulp.dest('./zips'));
});
gulp.src('./projects/*')
but what then?proj
proj/dist
proj/dist/sub01
proj/dist/sub02
proj/dist/sub03
proj/zipped
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;
}));
});
./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.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.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