Closed PopovMP closed 3 years ago
UZIP.js expects an ArrayBuffer (= a finite array of Bytes, a byte is 8 bits = a number between 0 and 255).
Set the XMLHttpRequest responseType to ArrayBuffer instead of a string.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data
Or convert your string into an ArrayBuffer with a for-loop.
I receive the res.response as a string
typeof res.response ; => 'string'
Then try to convert it in Uint8Array
getData_ready(response) {
const byteArray = this.strToByteArray(response.response)
const outBuff = UZIP.parse( byteArray )
console.log( this.strFromUtf8Ab(outBuff) )
}
strToByteArray(str) {
const byteArray = new Uint8Array(str.length)
for (let i = 0; i < str.length; i++) {
byteArray[i] = str.charCodeAt(i) & 0xff
}
return byteArray
}
strFromUtf8Ab(ab) {
return String.fromCharCode.apply(null, ab)
}
In the above example, nothing works: inflate
, inflateRow
, parse
.
Most probably my problem is coming from the way I'm trying to convert the response to an array buffer. Tried various ways, but cannot make it work.
const byteArray = new TextEncoder().encode(res.response)
const outBuff = UZIP.inflateRaw( byteArray )
Any ideas?
Can you look at your byte array with console.log() ? Is it what it should be?
Why dont you set the responseType to ArrayBuffer instead of String?
Why dont you set the responseType to ArrayBuffer instead of String?
Excellent!! It solved the problem.
Thank you!
I am glad it helped! Typed arrays are a very useful feature in Javascript.
Please show an example of how to unzip a file received with XMLHttpRequest in the Browser.
Here is an example zipped file: https://data.binance.vision/data/spot/monthly/klines/BTCUSDT/1h/BTCUSDT-1h-2021-04.zip I GET it in the Browser with XMLHttpRequest.
How to unzip it and to have the result in a string?
Thank you!