Narayanan
Narayanan

Reputation: 115

Angular 5 Content Upload GZip

I am using Angular 5 on the client-side. I have some large text content that I need to upload to my server using my API. I am trying to pass the Content-Encoding: gzip header and send compressed contents to the server from Angular 5.

I tried researching few gzip libraries like pako, zlib, etc and none of the play well (missing types) with TypeScript. Are there any good gzip libraries that I can use with TypeScript?

Thanks

Upvotes: 3

Views: 2162

Answers (2)

HODL RUN
HODL RUN

Reputation: 1

With Angular / Typescript, you also have to run the npm i --save-dev @types/pako command to use Pako.

npm install pako

npm i --save-dev @types/pako

Upvotes: 0

Zvi Redler
Zvi Redler

Reputation: 1768

For using pako do this list of actions:

  1. Install pako -
npm install pako

Or

bower install pako

  1. Add this line in your ts file

import { inflate, deflate, gzip, ungzip } from 'pako';

And where you want to compress your array:

const options = {
  level: 9,
  name: 'hello-world.txt',
  timestamp: parseInt(Date.now() / 1000, 10)
};
const binaryString = gzip(JSON.stringify(options), { to: 'string' });
console.log(binaryString.length);

//
// Here you can do base64 encode, make xhr requests and so on.
//
const restored = JSON.parse(ungzip(binaryString, { to: 'string' }));
console.log(restored);

Site of pako:

https://nodeca.github.io/pako/

Important information for sending gzip to server:

Compress Outgoing Requests in Angular 2+

Good luck!

Upvotes: 1

Related Questions