Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Compressing hexadecimal string using GZIP on the web (Javascript)

$
0
0

I have a textarea where the entered value is converted to hexadecimal and concatenated, as shown below:

4f43 5441 1d00 0000 2400 0000 0004 0000
0200 0000 0000 0000 0000 0000 0000 0000
0200 0000 0206 0073 6b79 626f 780e 0073
6b79 626f 7865 732f 7768 6974 6502 0800
6d61 7074 6974 6c65 1900 4f47 5a20 4564
6974 6f72 2066 6972 7374 2065 7665 7220
6d61 7003 6670 7300 0000 0000 0500 0200
0400 0300 0500 0700 0000 0044 0000 0044
0000 0044 0000 0000 0000 00ff 0000 0500
0000 0544 0000 0544 0000 0044 6e00 3200
0000 0000 0000 0200 0207 0007 0007 0001
0001 0001 0000 0202 0003 0004 0005 0006
0007 0000 0202 0003 0004 0005 0006 0007
0000 0202 0003 0004 0005 0006 0007 0000
0200 0000 0000 0007 0000 0000 0000 0100
0000 0000 0000 0000 0000 0000 0100 0000
0000 0000 0000 0000 0000 0100 0000 0000
0000 0000 0000 0000

My goal is to compress this hexadecimal using gzip "online" (i.e without the zlib command line/nodejs) and make the output available for download using a blob.

This is my attempt for now using "PAKO":

html

<script src="https://cdn.jsdelivr.net/pako/1.0.3/pako.min.js"></script>
<textarea id="input"></textarea>
<button onclick="toHex()">Convert to hex</button>
<a id="downloadbtn">Download as gzip</button>

javascript

var pako = window.pako;
function toHex(){
    input = document.getElementById("input").value;
    hexresult = input.split("").reduce((hex,c)=>hex+=c.charCodeAt(0).toString(16).padStart(2,"0"),"");

    gzipresult = pako.gzip(hexresult, { level: 6 });
    download(gzipresult);
}

function download(data){
    downloadbtn = document.getElementById("downloadbtn");
    var blob = new Blob([ data ], {type : "application/gzip",});

    if (window.navigator.msSaveBlob) {
        navigator.msSaveBlob(blob, "output.gz");
    } else {
        var csvUrl = URL.createObjectURL(blob);
        $('#downloadbtn').attr({
            'download': "output.gz",
            'href': csvUrl
        });
    };
}

However the output generated by PAKO (gzip) does not match the output generated by zlib, they are not the same...

Is there a way to make the output of both be identical? or how can I compress a hexadecimal string correctly for gzip using JavaScript?

Update: @Blex had mentioned the use of Buffer.from, I think it is something native to nodejs (and I need it in the browser) so I tried this standalone script from this repo, however the generated file is still the same (still different from zlib), but anyway, I appreciate the attempt to help!


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>