Tech Blog

Programming, Technology

Extract Gzip data in Javascript

By Yaswanth Bellamkonda2 min read
← Back to blog

Hello seekers! Hope you are having a great day. In this blog, let me explain how to effectively extract gzip data using Javascript.

What is a Gzip file ?

Gzip is a file format that is used to compress and decompress files. It is based on the deflate algorithm, which compresses data in order to pace up network transfers. For Linux systems, it is the standard file compression format and it is much faster than ZIP.

Gzip extraction in Javascript

Gzip aggressively compresses large amounts of data into smaller files. Extracting this data in other platforms such as Node JS is comparatively easier. However, you cannot use any default methods and libraries in javascript. No matter, the pako library just serves the purpose. Let’s take a look at how to use this extraction procedure in depth.

First of all, we need to link the pako library’s CDN with the script tag in the html file as follows:

<script type="text/javascript" src="../js/pako.min.js"></script>

Write a JS method that retrieves the gzip file from the server and converts it into a blob. You can use Ajax, fetch or JSZip for fetching and loading the file. Once the file is loaded, we will transform the file into a blob and the blob’s array buffer has to be converted into a Uint8Array.

After that, we will pass this result to pako’s ungzip method which returns the data in its original format. In the below sample code, the variable parsed will store the extracted data.

JSZip.loadAsync(data)then(function (zip) {

   var file = zip.file(/zip/);

   return file.async("blob");

}}).then(async function success(text) {

var fileReader = await text.arrayBuffer();

      const binData = new Uint8Array(fileReader);

      var parsed = pako.ungzip(binData,{ 'to': 'string' });

});

That’s it, you can extract uncompressed data from gzip files in pure JS with these simple steps. Now that you can extract gzip easily, you can compress your large downloadable files in gzip format to reduce download time and data usage. Word of caution, make sure the compression level of your downloadable gzip matches with the compression level of your pako code.

You can change the compression level as follows:

pako.ungzip(data, {level: 4, to: 'string'});

To explore more about pako library and it’s usage, visit this documentation page.

Keep reading

You may be interested in

Browse all articles →
How Angular is Different from AngularJS
September 27, 20227 min read

How Angular is Different from AngularJS

What is Angular? Angular is a popular open-source Typescript framework and free to use. Considered to be a JavaScript-based framework,TypeScript.is its primary programming language. TypeScript is Developed by Microsoft and its superset of JavaScript, which simply means that TypeScript has all the capabilities that JS has and more additional features. Angular is majorly used for […]

By Sachin DevadigaRead More →
Terraform As a DevOps Tool (IaC) And Benefits (Part 2)
September 21, 20222 min read

Terraform As a DevOps Tool (IaC) And Benefits (Part 2)

In this article, we will explore terminologies and basic commands that can be used to interact with Terraform. Using Terraform init, an existing or new Terraform configuration can be initialized. As a beginner, you may get syntax errors or indentation errors. To validate theYAML data, use the Terraform “validate” command. The “Terraform apply“ command changes […]

By Utsav SolankiRead More →
Terraform as a DevOps tool (IaC) and benefits (part 1)
September 21, 20222 min read

Terraform as a DevOps tool (IaC) and benefits (part 1)

Nowadays, the term “Infrastructure as Code (IaC) is very popular. This code is written in the YAML or JSON format and directly deployed in cloud services. One of the IaC tools is Terraform. It is used for creating and managing existing infrastructure, including versions of stats. Terraform supports many cloud providers, such as Amazon Web […]

By Utsav SolankiRead More →