Gulp - Zip the contents of subfolders & name these archives after them












0















I would like to use Gulp & gulp-zip to:




  1. Zip each subfolder of ./lessons/ folder

  2. Name each archive after the original folder name. So archived folder ./lessons/1-hermit-crab/ should be named 1-hermit-crab.zip.

  3. 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));
});
});









share|improve this question



























    0















    I would like to use Gulp & gulp-zip to:




    1. Zip each subfolder of ./lessons/ folder

    2. Name each archive after the original folder name. So archived folder ./lessons/1-hermit-crab/ should be named 1-hermit-crab.zip.

    3. 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));
    });
    });









    share|improve this question

























      0












      0








      0








      I would like to use Gulp & gulp-zip to:




      1. Zip each subfolder of ./lessons/ folder

      2. Name each archive after the original folder name. So archived folder ./lessons/1-hermit-crab/ should be named 1-hermit-crab.zip.

      3. 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));
      });
      });









      share|improve this question














      I would like to use Gulp & gulp-zip to:




      1. Zip each subfolder of ./lessons/ folder

      2. Name each archive after the original folder name. So archived folder ./lessons/1-hermit-crab/ should be named 1-hermit-crab.zip.

      3. 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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 2:56









      Petr ChutnyPetr Chutny

      186




      186
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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()
          })





          share|improve this answer

































            -1














            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'))
            })
            });





            share|improve this answer























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              2














              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()
              })





              share|improve this answer






























                2














                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()
                })





                share|improve this answer




























                  2












                  2








                  2







                  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()
                  })





                  share|improve this answer















                  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()
                  })






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 18 at 16:00









                  Mark

                  11.8k33351




                  11.8k33351










                  answered Jan 18 at 6:05









                  Derek NguyenDerek Nguyen

                  736111




                  736111

























                      -1














                      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'))
                      })
                      });





                      share|improve this answer




























                        -1














                        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'))
                        })
                        });





                        share|improve this answer


























                          -1












                          -1








                          -1







                          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'))
                          })
                          });





                          share|improve this answer













                          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'))
                          })
                          });






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 20 at 3:07









                          Petr ChutnyPetr Chutny

                          186




                          186






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Callistus III

                              Ostreoida

                              Plistias Cous