jszip icon indicating copy to clipboard operation
jszip copied to clipboard

cannot add multiple file onto zip

Open onelazyugy opened this issue 11 years ago • 2 comments

I am using JSZip to generate a zip containing multiple files. The data is coming from the server in JSON format. I am only getting 2 files inside the for loop instead of multiple files. The size of the array of JSON is more than 2 so I expect multiple files not just 2.

    var zip = new JSZip();
    var root = data.list.dataList;
    var length = root.length;
    var i;
    for(i = 0; i < length; i++){                                                    
    var aFileName = "a_"+ root[i].type + "_" + root[i].orderNumber + ".xml";//just the name of the file                         
    var aContent = root[i].content; //content of file                               

    var bFileName = "b_"+ root[i].type + "_" + root[i].orderNumber + ".xml";//just the name of the file         
    var bContent = root[i].content; //content of file   

    //put files onto a folder
    zip.folder("Folder Name").file(aFileName, aContent).file(bFileName, bContent); 
   }
   //generate the zip with all files
   var content = zip.generate();
   location.href="data:application/zip;base64," + content; 

can someone please suggest a solution or an example similar to this since this code only take the content of the last iterations. This is why I can only get 2 files instead of multiple files

onelazyugy avatar Sep 25 '14 13:09 onelazyugy

You probably should use different folder names:

var zip = JSZip();

data.list.dataList.forEach(function(dataItem, idx){
   // here a folder's name is generated:
   var folder = zip.folder('Folder number ' + idx);
   folder.file(
      'a_' + dataItem.type + '_' + dataItem.orderNumber + '.xml',
      dataItem.content
   );
   folder.file(
      'b_' + dataItem.type + '_' + dataItem.orderNumber + '.xml',
      dataItem.content
   );
});

var base64content = zip.generate();
location = 'data:application/zip;base64,' + base64content;

Mithgol avatar Sep 26 '14 11:09 Mithgol

Hi @Mithgol ,

Do we have any solution to add multiple files in single folder for zip?

satyamurthy515 avatar May 25 '21 13:05 satyamurthy515