Using grunt-contrib-compress to zip multiple folders into multiple zip files

847 views Asked by At

I have this dir structure:

asdf
 |---a
 |   |---files
 |   |---folders
 |
 |---b
     |---files
     |---folders

I wanted to use grunt-contrib-compress (I suppose is the one I want), to end up with 2 zip files a.zip and b.zip

 a.zip
 |---files
 |---folders

 b.zip
 |---files
 |---folders

Is there any way to do this? I can't find anywhere something about having more than one output (it says nowhere whether it's possible or not). And the only other questions about compress are about making the zip without the root folder (which I would also like for this)

Any help would be appreciated :)

2

There are 2 answers

0
sergio On

It looks like grunt-contrib-compress is not the best solution here. The task that addresses your problem is grunt-zip-directories

For your particular case the task configuration should be:

zip_directories: {
    asdf: {
        files: [{
            filter: 'isDirectory',
            expand: true,
            cwd: './asdf',
            dest: './zip',
            src: '*'
        }]
    }
}

What this task does is, it takes all sub-directories of './asdf' and zips them into separate zip archives and puts the archives to './zip' folder. So after execution you should have two files in './zip' directory: a.zip and b.zip as you asked in your question.

0
Henry Ruhs On

The compress task does not provide that feature out of the box, but there is great workaround using a dynamic config:

grunt.dynamicCompress = function (path)
{
    var target = grunt.file.expand(path);

    for (var i in target)
    {
        grunt.config.set('compress.' + target[i],
        {
            src:
            [
                target[i] + '/**'
            ],
            options:
            {
                archive: 'your-dist-folder/' + target[i].toLowerCase() + '.zip'
            }
        });
    }
};

Run it with your target path:

grunt.dynamicCompress('your-target-folder/*');