Gulp - Zip the contents of subfolders & name these archives after them
I would like to use Gulp & gulp-zip to:
- Zip each subfolder of
./lessons/folder - Name each archive after the original folder name. So archived folder
./lessons/1-hermit-crab/should be named1-hermit-crab.zip. - Move all these archives into the
./lessons/folder.
I have started with this, but got stuck on not being able to retrieve the name of the ziped directory, so that I can use it for the archive name. So I keep the archives in the subfolder now.
Thanks for any help.
gulp.task('zip-lessons', function() {
// Get an array of subdirectories under ./lessons/
var subDirectories = glob.sync('./lessons/*/');
// For each directory…
subDirectories.forEach(function (subDirectory) {
return gulp.src(subDirectory + '**')
.pipe(zip('lesson.zip'))
.pipe(gulp.dest(subDirectory));
});
});
gulp gulp-zip
add a comment |
I would like to use Gulp & gulp-zip to:
- Zip each subfolder of
./lessons/folder - Name each archive after the original folder name. So archived folder
./lessons/1-hermit-crab/should be named1-hermit-crab.zip. - Move all these archives into the
./lessons/folder.
I have started with this, but got stuck on not being able to retrieve the name of the ziped directory, so that I can use it for the archive name. So I keep the archives in the subfolder now.
Thanks for any help.
gulp.task('zip-lessons', function() {
// Get an array of subdirectories under ./lessons/
var subDirectories = glob.sync('./lessons/*/');
// For each directory…
subDirectories.forEach(function (subDirectory) {
return gulp.src(subDirectory + '**')
.pipe(zip('lesson.zip'))
.pipe(gulp.dest(subDirectory));
});
});
gulp gulp-zip
add a comment |
I would like to use Gulp & gulp-zip to:
- Zip each subfolder of
./lessons/folder - Name each archive after the original folder name. So archived folder
./lessons/1-hermit-crab/should be named1-hermit-crab.zip. - Move all these archives into the
./lessons/folder.
I have started with this, but got stuck on not being able to retrieve the name of the ziped directory, so that I can use it for the archive name. So I keep the archives in the subfolder now.
Thanks for any help.
gulp.task('zip-lessons', function() {
// Get an array of subdirectories under ./lessons/
var subDirectories = glob.sync('./lessons/*/');
// For each directory…
subDirectories.forEach(function (subDirectory) {
return gulp.src(subDirectory + '**')
.pipe(zip('lesson.zip'))
.pipe(gulp.dest(subDirectory));
});
});
gulp gulp-zip
I would like to use Gulp & gulp-zip to:
- Zip each subfolder of
./lessons/folder - Name each archive after the original folder name. So archived folder
./lessons/1-hermit-crab/should be named1-hermit-crab.zip. - Move all these archives into the
./lessons/folder.
I have started with this, but got stuck on not being able to retrieve the name of the ziped directory, so that I can use it for the archive name. So I keep the archives in the subfolder now.
Thanks for any help.
gulp.task('zip-lessons', function() {
// Get an array of subdirectories under ./lessons/
var subDirectories = glob.sync('./lessons/*/');
// For each directory…
subDirectories.forEach(function (subDirectory) {
return gulp.src(subDirectory + '**')
.pipe(zip('lesson.zip'))
.pipe(gulp.dest(subDirectory));
});
});
gulp gulp-zip
gulp gulp-zip
asked Jan 18 at 2:56
Petr ChutnyPetr Chutny
186
186
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use node's path module to get the name of the subdirectory like so:
const path = require('path');
const dirName = path.parse('./folderA/folderB').base // -> 'folderB'
And pass the dirName to zip():
const { task, src, dest } = require('gulp');
const path = require('path');
const zip = require('gulp-zip');
const glob = require('glob');
const subDirs = glob.sync('./lessons/*');
task('zipLessions', (done) => {
subDirs.forEach(subDir => {
const dirName = path.parse(subDir).base;
src(subDir + '/*')
.pipe(zip(`${dirName}.zip`))
.pipe(dest('./lessons'))
})
done()
})
add a comment |
My colleague has offered me this solution that works, so now I am using it.
const gulp = require('gulp');
const zip = require('gulp-zip');
var fs = require('fs');
var path = require('path');
// Functions
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// Tasks
// Zip each folder in /lessons and place the archive in the /lessons root
gulp.task('zip-lessons', () => {
let folders = getFolders('lessons/');
console.log(folders)
var tasks = folders.map(function(folder) {
gulp.src(`lessons/${folder}/**`)
.pipe(zip(`${folder}.zip`))
.pipe(gulp.dest('lessons'))
})
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54247172%2fgulp-zip-the-contents-of-subfolders-name-these-archives-after-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use node's path module to get the name of the subdirectory like so:
const path = require('path');
const dirName = path.parse('./folderA/folderB').base // -> 'folderB'
And pass the dirName to zip():
const { task, src, dest } = require('gulp');
const path = require('path');
const zip = require('gulp-zip');
const glob = require('glob');
const subDirs = glob.sync('./lessons/*');
task('zipLessions', (done) => {
subDirs.forEach(subDir => {
const dirName = path.parse(subDir).base;
src(subDir + '/*')
.pipe(zip(`${dirName}.zip`))
.pipe(dest('./lessons'))
})
done()
})
add a comment |
You can use node's path module to get the name of the subdirectory like so:
const path = require('path');
const dirName = path.parse('./folderA/folderB').base // -> 'folderB'
And pass the dirName to zip():
const { task, src, dest } = require('gulp');
const path = require('path');
const zip = require('gulp-zip');
const glob = require('glob');
const subDirs = glob.sync('./lessons/*');
task('zipLessions', (done) => {
subDirs.forEach(subDir => {
const dirName = path.parse(subDir).base;
src(subDir + '/*')
.pipe(zip(`${dirName}.zip`))
.pipe(dest('./lessons'))
})
done()
})
add a comment |
You can use node's path module to get the name of the subdirectory like so:
const path = require('path');
const dirName = path.parse('./folderA/folderB').base // -> 'folderB'
And pass the dirName to zip():
const { task, src, dest } = require('gulp');
const path = require('path');
const zip = require('gulp-zip');
const glob = require('glob');
const subDirs = glob.sync('./lessons/*');
task('zipLessions', (done) => {
subDirs.forEach(subDir => {
const dirName = path.parse(subDir).base;
src(subDir + '/*')
.pipe(zip(`${dirName}.zip`))
.pipe(dest('./lessons'))
})
done()
})
You can use node's path module to get the name of the subdirectory like so:
const path = require('path');
const dirName = path.parse('./folderA/folderB').base // -> 'folderB'
And pass the dirName to zip():
const { task, src, dest } = require('gulp');
const path = require('path');
const zip = require('gulp-zip');
const glob = require('glob');
const subDirs = glob.sync('./lessons/*');
task('zipLessions', (done) => {
subDirs.forEach(subDir => {
const dirName = path.parse(subDir).base;
src(subDir + '/*')
.pipe(zip(`${dirName}.zip`))
.pipe(dest('./lessons'))
})
done()
})
edited Jan 18 at 16:00
Mark
11.8k33351
11.8k33351
answered Jan 18 at 6:05
Derek NguyenDerek Nguyen
736111
736111
add a comment |
add a comment |
My colleague has offered me this solution that works, so now I am using it.
const gulp = require('gulp');
const zip = require('gulp-zip');
var fs = require('fs');
var path = require('path');
// Functions
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// Tasks
// Zip each folder in /lessons and place the archive in the /lessons root
gulp.task('zip-lessons', () => {
let folders = getFolders('lessons/');
console.log(folders)
var tasks = folders.map(function(folder) {
gulp.src(`lessons/${folder}/**`)
.pipe(zip(`${folder}.zip`))
.pipe(gulp.dest('lessons'))
})
});
add a comment |
My colleague has offered me this solution that works, so now I am using it.
const gulp = require('gulp');
const zip = require('gulp-zip');
var fs = require('fs');
var path = require('path');
// Functions
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// Tasks
// Zip each folder in /lessons and place the archive in the /lessons root
gulp.task('zip-lessons', () => {
let folders = getFolders('lessons/');
console.log(folders)
var tasks = folders.map(function(folder) {
gulp.src(`lessons/${folder}/**`)
.pipe(zip(`${folder}.zip`))
.pipe(gulp.dest('lessons'))
})
});
add a comment |
My colleague has offered me this solution that works, so now I am using it.
const gulp = require('gulp');
const zip = require('gulp-zip');
var fs = require('fs');
var path = require('path');
// Functions
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// Tasks
// Zip each folder in /lessons and place the archive in the /lessons root
gulp.task('zip-lessons', () => {
let folders = getFolders('lessons/');
console.log(folders)
var tasks = folders.map(function(folder) {
gulp.src(`lessons/${folder}/**`)
.pipe(zip(`${folder}.zip`))
.pipe(gulp.dest('lessons'))
})
});
My colleague has offered me this solution that works, so now I am using it.
const gulp = require('gulp');
const zip = require('gulp-zip');
var fs = require('fs');
var path = require('path');
// Functions
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// Tasks
// Zip each folder in /lessons and place the archive in the /lessons root
gulp.task('zip-lessons', () => {
let folders = getFolders('lessons/');
console.log(folders)
var tasks = folders.map(function(folder) {
gulp.src(`lessons/${folder}/**`)
.pipe(zip(`${folder}.zip`))
.pipe(gulp.dest('lessons'))
})
});
answered Jan 20 at 3:07
Petr ChutnyPetr Chutny
186
186
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54247172%2fgulp-zip-the-contents-of-subfolders-name-these-archives-after-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown