Stuk / jszip

Create, read and edit .zip files with Javascript
https://stuk.github.io/jszip/
Other
9.72k stars 1.3k forks source link

how to compress .xlsx files to .zip using jszip ?? #411

Open sravanisomepalli6 opened 7 years ago

sravanisomepalli6 commented 7 years ago

Hi i am new to this i have a requirement to compress .xlsx files into .zip and sending this to mail. .xlsx file sending to email is done but i don't know how to compress this as .zip file. thanks plz help.... pls lookinto this;;;;

attachements.push(...[report.emailParts.XLSX].filter(a=>a).map((xlsx) => {
    let out_file_name = "";
    let a=[];
    try {
        out_file_name = out_file_pattern_to_name(
            report.parts[0].outfile_pattern || outfile_pattern,
            {Name: report.parts[0].Name || name});
    } catch (e)  {
        console.dir(e);
        process.exit(1);
    }
    return {
        filename:out_file_name + '.xlsx' ,
        content: new Buffer(xlsx, 'base64') 
    }
}));
dduponchel commented 7 years ago

Once you have your list of objects {filename, content}, you need to add them to a zip instance:

either

let zip = new JSZip();
parts.forEach((p) => zip.file(p.filename, p.content));

or

let zip = parts.reduce((zip, p) => zip.file(p.filename, p.content), new JSZip());

Then, call generateAsync to generate a promise of the result:

zip.generateAsync({type:"nodebuffer"}).then(/* success callback */).catch(/* error callback */)
sravanisomepalli6 commented 7 years ago

Hi, .zip file is comming to folder path[C:\Users\abc\Desktop\study\nodeExs\attachements.zip] but i need it to send as eamil how to get zipped file name ..... kindly help using this i got zip file[attachements.zip}:

zip.file(out_file_name + '.xlsx',new Buffer(xlsx, 'base64')); zip.writeToFile(out_file_name+'.zip');

try { let Email = new Emailer(); let str = await Email.send({ to , cc , bcc , from , subject: out_file_pattern_to_name(subject, {Name: name}) , html , attachments: attachements =======>[need to send here] }); console.log(str) return str; } catch(err) { throw err; }