adm-zip icon indicating copy to clipboard operation
adm-zip copied to clipboard

Create a zip with the files without any parent directory

Open mparpaillon opened this issue 10 years ago • 5 comments

Hi, I'd like to have all my files directly into the zip without any folder.

So If I do that...

zip.addLocalFile('asset1.png');
zip.addLocalFile('asset2.png');
zip.addLocalFile('asset3.png');

zip.writeZip('files.zip');

... then extract files.zip, I'd like to have directly the 3 assets and not

files/asset1.png
files/asset2.png
files/asset3.png

Thanks a lot

mparpaillon avatar Aug 19 '15 21:08 mparpaillon

I know a workaround for this: read each file content as a buffer. Example with async :

          async.each(files, function (file, next) {
            fs.readFile(templateFilePath + '/' + file, function (err, buffer) {
              if (err) return next(err);
              zip.addFile(file, buffer);
              next();
            })
          }, function (err) {
            if (err) return done(err);
               zip.writeZip(filePath, function (err, file) {
                  if (err) return done(err);
                    .
                    .
                  return done();
           });

alain75007 avatar Sep 03 '15 17:09 alain75007

Thanks, but that doesn't fix it for me. I still get a root folder containing my files :s

mparpaillon avatar Sep 05 '15 12:09 mparpaillon

Late to the party, but you just need a zipPath.

zip.addLocalFile('asset1.png', './');
zip.addLocalFile('asset2.png', './');
zip.addLocalFile('asset3.png', './');

zip.writeZip('files.zip');

https://github.com/cthackers/adm-zip/wiki/ADM-ZIP#void-addlocalfilestring-localpath-string-zippath

jeffwesson avatar Oct 31 '16 02:10 jeffwesson

Was there ever a resolution to this? I need to use 'addLocalFolder' and I need all of the files to be at the root of the zip.

The following code puts all the files in a root directory "my-zip". Im using version 0.4.11.

const AdmZip = require("adm-zip");
const zip = new AdmZip();
zip.addLocalFolder("./sandbox/src/lib/", "./");

zip.writeZip("./spec/my-zip.zip");

corybill avatar Jun 17 '18 20:06 corybill

Realized this is a difference between mac and linux. On my Mac, it automatically unzips into a directory the same name as zip file. When I unzip on linux, it works as expected.

Hope this saves someone else some time.

corybill avatar Jun 17 '18 21:06 corybill