如何使用 Gulp 将多个文件解压缩到同一文件夹中
How to unzip multiple files in the same folder with Gulp
我想解压缩单个文件夹中的多个 zip 文件。每个解压缩的文件将被解压到一个与原始 zip 文件同名的文件夹中,并作为子文件夹添加到包含原始 zip 的原始文件夹中。
像这样:
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 文件一起使用,效果很好。
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 文件中。
感谢帮助
你的问题出在这里:
.pipe(tap(function (file, t) {
currentZipFileName = path.basename(file.path);
}))
您正试图在一个管道中设置一个变量,以便在以后的管道中使用。这是行不通的,这里有一些关于它的问题,但它只是行不通 - 当 gulp.dests 开始触发或未定义时,你的变量可能会有最后一个值 - 我认为它是基于在不可预测的时间。
在任何情况下您都不需要设置该变量 - 您已经在 zips.map(zip) {}
和 zip
项目中拥有所需文件夹名称的值。你可以在 gulp.dest
中使用它就好了。
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
,原因如下:gulpjs docs on glob separators:
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.
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.
我想解压缩单个文件夹中的多个 zip 文件。每个解压缩的文件将被解压到一个与原始 zip 文件同名的文件夹中,并作为子文件夹添加到包含原始 zip 的原始文件夹中。
像这样:
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 文件一起使用,效果很好。
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 文件中。 感谢帮助
你的问题出在这里:
.pipe(tap(function (file, t) {
currentZipFileName = path.basename(file.path);
}))
您正试图在一个管道中设置一个变量,以便在以后的管道中使用。这是行不通的,这里有一些关于它的问题,但它只是行不通 - 当 gulp.dests 开始触发或未定义时,你的变量可能会有最后一个值 - 我认为它是基于在不可预测的时间。
在任何情况下您都不需要设置该变量 - 您已经在 zips.map(zip) {}
和 zip
项目中拥有所需文件夹名称的值。你可以在 gulp.dest
中使用它就好了。
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
,原因如下:gulpjs docs on glob separators:
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.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.