Search code examples
gulpzipglobdirectory-structuregulp-zip

Create a zip from folders retaining file structure including parent folders (and single files)


I am trying to create a zip file from the following file structure:

-dist/bundle.js
-assets/[several subfolders with files]
-config.json
-bootstrap.js

I have used gulp-zip:

gulp.task('zip', ()=>{
return gulp.src(['dist/**/*.*', 'assets/**/*.*','config.json', 'bootstrap.js'])
    .pipe(zip('game.zip'))
    .pipe(gulp.dest('deploy'))
})

which results in: game.zip with this structure:

-game
--[some assets subfolder]
--[other assets subfolder]
--[third assets subfolder]
--bundle.js
--bootstrap.js
--config.json

The files/folders should not be in a folder (game) but retain the structure they initially have, also assets and dist folders should also be in the structure. Any solution that I can run from my package.json scripts node would be welcomed. (gulp/webpack/grunt/whatever)

Thank you!


Solution

  • I tried this:

    gulp.task('default', ()=>{
      return gulp.src(['dist/**/*.*', 'assets/**/*.*','config.json', 'bootstrap.js'], {base: '.'})
          .pipe(zip('game.zip'))
          .pipe(gulp.dest('deploy'))
    })
    

    Simply adding the {base: '.'} option to gulp.src does what you want. See gulp base option. Using {base: '.'} basically tells gulp to use all the directories in the dest location. Otherwise the default operation is to remove directories before the globs. So, in 'dist/**/*.*' the dist folder would not be retained without the base option.

    I don't know where you get a game folder, I never do.