Pako not able to deflate gzip files generated in python

2024/10/5 21:21:55

I'm generating gzip files from python using the following code: (using python 3)

    file = gzip.open('output.json.gzip', 'wb')dataToWrite = json.dumps(data).encode('utf-8')file.write(dataToWrite)file.close()

However, I'm trying to read this file now in Javascript using the Pako library (I'm using Angular 2):

this.http.get("output.json.gzip").map((res:Response) => {var resText:any = new Uint8Array(res.arrayBuffer());var result = "";try {result = pako.inflate(resText, {"to": "string"});} catch (err) {console.log("Error " + err);}return result;});

But I'm getting this error in the console: unknown compression method. Should I be doing something else to properly inflate gzip files?

Answer

Turns out that I needed to use the res.blob() function to get the true binary data, not res.arrayBuffer(); and then convert the blob to the array buffer:

return this.http.get("output.json.gzip", new RequestOptions({ responseType: ResponseContentType.Blob })).map((res:Response) => {var blob = res.blob();var arrayBuffer;var fileReader = new FileReader();fileReader.onload = function() {arrayBuffer = this.result;try {let result:any = pako.ungzip(new Uint8Array(arrayBuffer), {"to": "string"});let obj = JSON.parse(result);console.log(obj);} catch (err) {console.log("Error " + err);}};fileReader.readAsArrayBuffer(blob);return "abc";});
https://en.xdnf.cn/q/70439.html

Related Q&A

Best way to have a python script copy itself?

I am using python for scientific applications. I run simulations with various parameters, my script outputs the data to an appropriate directory for that parameter set. Later I use that data. However s…

How to convert dictionary to matrix in python?

I have a dictionary like this:{device1 : (news1, news2, ...), device2 : (news 2, news 4, ...)...}How to convert them into a 2-D 0-1 matrix in python? Looks like this:news1 news2 news3 news4 device1 …

Ubuntu 16.04 - Why I cannot install libtiff4-dev?

Following this tutorial, I am trying to install the OpenCV 3 with Python on Ubuntu 16.04.At the step of entering $ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-devI got this me…

Histogram update in a for loop with matplotlib.pylab

I am trying to update in for loop a histogram data. but I dont know how to make it. I tried with set_data but it is not working. here is the code:plt.ion() ax=plt.subplot(111) [n,X, V]=ax.hist(range(MA…

How to remove a section from an ini file using Python ConfigParser?

I am attempting to remove a [section] from an ini file using Pythons ConfigParser library.>>> import os >>> import ConfigParser >>> os.system("cat a.ini") [a] b = c…

why does this script not work with threading python

so ive been trying to ifnd a way to access task manager. Ive tried a few methods including the wmi module and the windows tasklist but neither suit my need. wmi is way too slow and tasklist becomes to…

django-admin command not working in Mac OS

I started Django in Mac OS and after installing Django using pip, I tried to initiated a new project using the command django-admin startproject mysite. I get the error -bash: django-admin: command not…

HeartBleed python test script

I came across this Python script that tests the server for the HeartBleed vulnerability: Would someone be able to explain the content of the "hello", what is being sent and how was this cont…

How to convert a wand image object to numpy array (without OpenCV)?

I am converting pdf files to image using Wand. Then, I do further image processing using ndimage. I would like to directly convert the Wand image into a ndarray... I have seen the answer here, but it u…

Import error running unittest in Python3

I have a problem importing files in Python 3.6. My directories tree is as given below:project/app/├── __init__.py├── a.py└── b.pytest/├── __init__.py├── test_a.py└── test_b.pyIt works…