Search code examples
javascriptgzipfetch-apipako

Decompress a GZIP CSV file on client side with Pako.js


Problem

I want to load a large gzipped CSV file using the Fetch API and Pako.js with client-side code. This is the code I was using:

const res = await fetch('www.example.com/large.csv.gzip');
let raw = await res.text();
raw = pako.inflate(raw);
console.log(raw);

I got an error without any stack trace:

Uncaught (in promise) unknown compression method

Attempted Research

I found some examples, but they didn't relate to CSV files, the Fetch API, or both:

Environment

Firefox 108.0.2 (64-bit)

Pako.js version 2.1.0


Solution

  • I came across a Codepen that was using XHR requests and setting the response type to an array buffer.

    So I tried that with the Fetch API and the code worked!

    const res = await fetch('www.example.com/large.csv.gzip');
    let raw = await res.arrayBuffer();
    raw = pako.inflate(raw);
    console.log(raw); // Prints CSV file content successfully