Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/decompressors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Decompression of gzipped files using ukyo's jsziptools.js (jz)
* Requires ukyo's jsziptools.js to be in global scope when decompressing gzipped files
* https://github.com/ukyo/jsziptools
* http://ukyo.github.io/jsziptools/jsziptools.js
*/
var gzip = (function () {
return {
decompress: function(arrayBuffer) {
var hexarr = jz.gz.decompress(arrayBuffer);
return _hextostring(hexarr);
},
};
function _hextostring(hexarr){
for (var w = '', i = 0, l = hexarr.length; i < l; i++) {
w += String.fromCharCode(hexarr[i])
}
return decodeURIComponent(escape(w));
}
})();

var DECOMPRESSORS = {
gz: function (x) {return gzip.decompress(x);}
};
27 changes: 23 additions & 4 deletions src/leaflet.filelayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@
gpx: this._convertToGeoJSON,
kml: this._convertToGeoJSON
};

this._decompressors = typeof(DECOMPRESSORS)==='undefined'?{}:DECOMPRESSORS;
},


load: function (file, ext) {
var parser,
reader;
Expand All @@ -84,7 +87,11 @@
var layer;
try {
this.fire('data:loading', { filename: file.name, format: parser.ext });
layer = parser.processor.call(this, e.target.result, parser.ext);
var result = e.target.result;
if (parser.decompressor) {
result = parser.decompressor(result);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we include the decompression in the parser.processor function if a decompressor is defined?

layer = parser.processor.call(this, result, parser.ext);
this.fire('data:loaded', {
layer: layer,
filename: file.name,
Expand All @@ -98,7 +105,11 @@
// but an object with file.testing set to true.
// This object cannot be read by reader, just skip it.
if (!file.testing) {
reader.readAsText(file);
if (parser.decompressor) {
reader.readAsArrayBuffer(file);
} else {
reader.readAsText(file, 'utf-8');
}
}
// We return this to ease testing
return reader;
Expand Down Expand Up @@ -163,8 +174,15 @@
},

_getParser: function (name, ext) {
var parser;
ext = ext || name.split('.').pop();
var parser, decompressor;
if (!ext) {
name = name.split('.');
ext = name.pop();
decompressor = this._decompressors[ext];
if (decompressor) {
ext = name.pop();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this always true? You are assuming that the file will data.geojson.zip

}
}
parser = this._parsers[ext];
if (!parser) {
this.fire('data:error', {
Expand All @@ -174,6 +192,7 @@
}
return {
processor: parser,
decompressor: decompressor,
ext: ext
};
},
Expand Down