How to gulp zip multiple folders and exclude some files?

Refresh

April 2019

Views

1.6k time

1

I am a beginner at using Gulp and I don't know a lot about what is possible and what not. I would like to achieve the following:

my setup for 1 project:

.dist
├── client-1
│ ├── campaign-1
│ │ ├── project-a
│ │ │ ├── css (folder)
│ │ │ ├── js (folder)
│ │ │ ├── images (folder)
│ │ │ ├── index.html
│ │ │ └── preview.html

(Every project has the same structure. I have multiple clients with different names with different campaigns.)

!! I would like to make a zip file for each project with all files and folders included, except the file preview.html. If the name can be the name of the project, that would be even better.

.dist
├── client-1
│ ├── campaign-1
│ │ ├── project-a
│ │ │ ├── css (folder)
│ │ │ ├── js (folder)
│ │ │ ├── images (folder)
│ │ │ ├── index.html
│ │ │ ├── preview.html
│ │ │ └── project-a.zip (new)

If I unzip the zipfile, I would like to see the following (not inside a folder):

  • css (folder and contents)
  • js (folder and contents)
  • images (folder and contents)
  • index.html

How can I set up Gulp task(s) to do this zipping for me?

1 answers

1

You can do this with the help of gulp-zip.

If you want to exclude specific files or folders from zipping, you can amke use of regular expressions. Check this once, Excluding files/directories from Gulp task

See below:

const gulp = require('gulp');
const zip = require('gulp-zip');

gulp.task('default', () => {
  return gulp.src([ 'js/**/*.js', '!js/**/*.min.js' ]) // Here I'm excluding .min.js files
    .pipe(zip('archive.zip'))
    .pipe(gulp.dest('dist'));
});

If you want exclude or include any specific folders or files, you can do that with regex also . Let's say, If you want to exclude folders which starts with underscore('_') symbol from zip, you do like this,

gulp.src('js/[^_]*/*.js'); // Here I'm excluding folders that start with '_' symbol

Just like, what we write in javascript, same regex will works here. Hope this helps :)