Search code examples
javascriptxmlcompressionjszip

why the compression ration is 0 using JSZip


I converted my downloadable xml file to zip using the following code but the file size is sill same and the compression ratio shows 0%

    var xmlcontent = "<?xml version='1.0' encoding='UTF-8'?><Body>";
    xmlcontent += json2xml(data);
    xmlcontent += "</Body>";

    var zip = new JSZip();
    zip.file("test1.xml", xmlcontent);
    zip.generateAsync({ type: "blob" })
    .then(function (content) {

        var a = document.createElement("a");
        document.body.appendChild(a);
        a.style = "display: none";
        var url = window.URL.createObjectURL(content);
        a.href = url;
        a.download = "test.zip";
        a.click();
        window.URL.revokeObjectURL(url);

    });

The reason of compression was to decrease the size of the file the client retrieves but apparently it had no effect on it. Kindly suggest a way to decrease the file size.


Solution

  • The default compression setting for file(s) is STORE (= no compression) - as stated in the documentation

    Change it to DEFLATE with the options parameter of .generateAsync(options)

    var zip = new JSZip();
    zip.file("test1.xml", xmlcontent);
    zip.generateAsync({
            type: "blob",
            compression: "DEFLATE"
        })
        .then(function (content) {
                ...
        });