Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create folder in zip file archiver - node.js

I want to create a zip file and new folder in it. I created a zip file but couldn't create folders.

Here is my node.js code;

var archiver = require('archiver');
var zip = archiver('zip');

for(var i=0; i < files.length; i++){
     zip.append(new Buffer(files[i].data.buffer), { name: files[i].name } );    
}

For example I want to create folder as /first/second and add file in it.

.zip
  first(folder)
     second(folder)
        file

How can I do it?

like image 638
mmu36478 Avatar asked Nov 15 '25 16:11

mmu36478


1 Answers

Actually, I just realize that it was quite simple by editing the name field in the following line;

zip.append(new Buffer(files[i].data.buffer), { name: files[i].name } );    

as

zip.append(new Buffer(files[i].data.buffer), { name: "/folderName/" + files[i].name } );    

You can replace /folderName/ with any filename that you want to create.

like image 138
mmu36478 Avatar answered Nov 17 '25 08:11

mmu36478



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!