我想解压缩单个文件夹中的多个zip文件.每个解压缩的文件都将解压缩到与原始zip文件同名的文件夹中,并作为子文件夹添加到包含原始zip的原始文件夹中.
I'd like to unzip multiple zip files that are inside a single folder. Every unzipped file will be unpacked into a folder with the same name as the original zip file and added as a sub folder to the original folder containing the original zips.
类似这样的东西:
parent(folder) -a.zip -b.zip -c.zip将成为:
parent(folder) -a(folder) --a.zip contents here -b(folder) --b.zip contents here -c(folder) --c.zip contents here我相信到目前为止,我的代码是一个不错的尝试,但是似乎它正在管道中异步执行(我显然不是Gulp专家).正在查看所有zip文件,但似乎只有最后一个会获取所有内容,然后从其他zip中获取一些内容.使用文件夹中的一个zip文件运行它,效果很好.
I believe the code i have so far is a nice try but seems like it's executing asynchronously in the pipeline (i'm obviously not a Gulp expert). All the zip files are being looked at but only the last one seems to get all the content, and then some from other zips. Run it with one zip file in the folder and it works perfectly.
var zipsPath = 'src/'; var currentZipFileName; function getZips(dir) { return fs.readdirSync(dir) .filter(function (file) { return file.indexOf(".zip") > 0; }); } gulp.task('init', function (done) { var zips = getZips(zipsPath); var tasks = zips.map(function (zip) { console.log("zip", zip, path.join(zipsPath, zip)); return gulp.src(path.join(zipsPath, zip), { base: '.' }) .pipe(tap(function (file, t) { currentZipFileName = path.basename(file.path); })) .pipe(unzip({ keepEmpty : true })) .pipe(gulp.dest(function (path) { var folderName = currentZipFileName.replace(".zip", ""); var destination = "./src/" + folderName; //console.log("destination", destination); return destination; })) .on('end', function() { console.log('done'); done(); }); }); return tasks; });预期结果:所有zip文件均应解压缩. 实际结果:大多数内容都被转储到最后查看的zip文件中. 感谢您的帮助
Expected results: all the zip files should be unpacked. Actual results: most of the content is being dumped into the last zip file looked at. Thanks for the help
推荐答案您的问题出在这里:
.pipe(tap(function (file, t) { currentZipFileName = path.basename(file.path); }))您正试图在一个管道中设置一个变量,以在以后的管道中使用.那是行不通的,这里有一些问题,但这只是行不通-当gulp.dests开始触发或未定义时,您的变量可能具有最后一个值-我认为它是基于不可预测的时间.
You are trying to set a variable in one pipe to use in a later pipe. That doesn't work, there are a few questions here about it, but it just doesn't work - your variable will probably have the last value in it when the gulp.dests start firing or undefined - I think it is based on unpredictable timing.
无论如何,您都不需要设置该变量-您在zips.map(zip) {}和zip项目中已经具有所需文件夹名称的值.您可以在gulp.dest中使用它.
In any case you don't need to set that variable - you already have the value of the desired folder name in your zips.map(zip) {} the zip item. You can use that in the gulp.dest just fine.
gulp.task('init', function (done) { var zips = getZips(zipsPath); var tasks = zips.map(function (zip) { return gulp.src(zipsPath + "/" + zip) // .pipe(tap(function (file, t) { // currentZipFileName = path.basename(file.path); // })) .pipe(unzip({ keepEmpty: true })) .pipe(gulp.dest(path.join("src", path.basename(zip, ".zip")))) .on('end', function() { done(); }); }); return tasks; });
出于以下原因,还应避免在gulp.src中使用path.join:关于glob分隔符的gulpjs文档:
Also avoid using path.join in your gulp.src for the reasons stated here: gulpjs docs on glob separators:
全局变量中的分隔符始终为/字符-与操作系统无关-即使在路径分隔符为\\的Windows中也是如此.在全局范围内,\\被保留为转义字符.
The separator in a glob is always the / character - regardless of the operating system - even in Windows where the path separator is \\. In a glob, \\ is reserved as the escape character.
避免使用Node的path方法(例如path.join)来创建glob.在Windows上,由于Node使用\\作为分隔符,因此它会生成无效的glob.出于相同的原因,也请避免使用__dirname全局,__filename全局或process.cwd().
Avoid using Node's path methods, like path.join, to create globs. On Windows, it produces an invalid glob because Node uses \\ as the separator. Also avoid the __dirname global, __filename global, or process.cwd() for the same reasons.
更多推荐
如何使用Gulp解压缩同一文件夹中的多个文件
发布评论