Comments (7)
Hmm, try making a Uint8Array before making a buffer?
new buffer.Buffer(new Uint8Array(xhr.response))
from browserify-zlib.
You shouldn't convert the file data to utf8 before unzipping it. That will mess with the compressed data. Try the following (I removed line 5 of your snippet). Weird that this works in Node. Could be a difference in buffer implementations between browserify and node or something else. Let me know if this works.
zlib = require('zlib')
fs = require('fs')
data = fs.readFileSync('data.json.gz')
zlib.gunzip(data, function(err, res){
console.log(err, res.toString("utf8"))
})
from browserify-zlib.
Oh maybe I misunderstood. Was decompress.js
only used in Node, not the browser?
from browserify-zlib.
decompress.js
was only used in Node, where it worked. The logic for decompressing in the browser is in index.html itself.
$(function(){
console.log("Send request")
$.ajax({url: "/data.json.gz"})
.success(function(data, status, req){
console.log(status)
window.COMPRESSED_DATA = data
window.BUFFERED_COMPRESSED_DATA = new buffer.Buffer(data)
console.log(data)
window.DECOMPRESSED_DATA = zlib.gunzipSync(BUFFERED_COMPRESSED_DATA)
}).error(function(error, message, splat){
console.log(arguments)
})
})
I had no encoding parameter to new buffer.Buffer(...), but there is no change when I do include 'base64'.
from browserify-zlib.
Ah. jQuery's ajax function loads the data as text, which is not what you want. You need to set the ajax responseType to 'arraybuffer' so you can get binary data out. Try this:
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var data = new buffer.Buffer(xhr.response);
var decompressed = zlib.gunzipSync(data);
};
xhr.open('GET', '/data.json.gz', true);
xhr.responseType = 'arraybuffer';
xhr.send();
from browserify-zlib.
Thank you for the suggestion. I've never dealt with this part of Ajax before. I tried what you suggested, but got a different error this time.
errno: -5,
code: "Z_BUF_ERROR",
message: "buffer error: "
Error: buffer error:
at Zlib._binding.onerror (http://localhost:9090/main.js:6359:17)
at Zlib._error (http://localhost:9090/main.js:5987:8)
at Zlib._write (http://localhost:9090/main.js:5945:10)
at Zlib.writeSync (http://localhost:9090/main.js:5894:15)
at Gunzip.Zlib._processChunk (http://localhost:9090/main.js:6515:31)
at zlibBufferSync (http://localhost:9090/main.js:6233:17)
at Object.exports.gunzipSync (http://localhost:9090/main.js:6177:10)
at XMLHttpRequest.xhr.onload (http://localhost:9090/index.html:16:38)
xhr.response
is of type ArrayBuffer
, but it is empty. Its length is set to 34 though, which is the length of the gzipped file.
from browserify-zlib.
That did the trick. The console.log() value of the Buffer and the Uint8Array were the same, but these are areas of JavaScript where I haven't developed a good understanding of the APIs.
Now that I've done this consistently, I need to find out if I can embed the compressed data in an HTML file somehow.
If you care about Stack Overflow badges, I asked this question over three months ago there, http://stackoverflow.com/questions/24898650/zlib-browserify-throws-data-error-incorrect-header-check, this would be good case for Revival.
from browserify-zlib.
Related Issues (20)
- Bug: Buffer error is not fatal HOT 1
- Keeping browserify-zlib up to date HOT 2
- can i maintain this library?
- sync api does not work HOT 2
- package.json contains the key "main" twice HOT 1
- Use pako 2.0 HOT 2
- yarnlock file in npm distribution
- browser tests failing on OS X HOT 1
- fails to decompress data with trailing garbage
- travis build failing on master
- fails to decompress files created with `bgzip` HOT 2
- Angular 6 support (build error) HOT 1
- Zlib's brotli methods are not supported HOT 8
- Switch to faster compression library
- installation guide?
- Error
- Node zlib inflateSync will take UInt8Array, browserify-zlib won't HOT 1
- version 0.1.4 has two "main" entries. HOT 1
- zlib.constants missing in browserify
- zlib.unzip, "Z_DATA_ERROR" - "incorrect header check" HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from browserify-zlib.