sindresorhus / gulp-zip

ZIP compress files
MIT License
270 stars 47 forks source link

How to exclude the parent folder from zip package! #80

Closed warengonzaga closed 8 years ago

warengonzaga commented 8 years ago

Hello there,

I need some help on how I can achieve this output:

My dir paths:

   tools/zipMaker/input/
   tools/zipMaker/output/

FROM:

input/
   |_ package01
   |      |_ index.html
   |      |_ css/style.css
   |      |_ js/script.js
   |_ package02
          |_ index.html
          |_ css/style.css
          |_ js/script.js

INTO: when unzip I want to see this, without the parent folder (package01/) each folder going to zip exclude the parent folder so when I zip theres no "package01" folder.

output/
   |_ index.html
   |_ css/style.css
   |_ js/script.js

heres my code:

return gulp.src(paths.zipMakerToolInput)
       .pipe(foreach(function(stream, file){
          var fileName = file.path.substr(file.path.lastIndexOf("/")+1);
          gulp.src(paths.zipMakerToolInput+fileName+'/**/*')
              .pipe(zip(fileName+".zip"))
              .pipe(gulp.dest('./'+paths.zipMakerToolOutput));

          return stream;

Please response as soon as possible thanks!

Regards, Waren

warengonzaga commented 8 years ago

I manage to fix my own issue! here's my quick fix for my gulp-zip problem!

if you want to exclude the parent/root folder of your files to be zip. Use the BASE!

here's my fix...

return gulp.src(paths.zipMakerToolInput)
       .pipe(foreach(function(stream, file){
          var fileName = file.path.substr(file.path.lastIndexOf("/")+1);
          var packageName = ('./tools/zipMaker/input/'+fileName);
          gulp.src(paths.zipMakerToolInput+fileName+'/**/*', {base: packageName})
              .pipe(zip(fileName+".zip"))
              .pipe(gulp.dest(paths.zipMakerToolOutput));

          return stream;
       }));

I add {base:} and define/locate the path of parent folder to be exclude in zip! and that's so easy! I didn't realize that haha.