Skip to content

Commit 1c71700

Browse files
committed
Added preliminary gzip support based on jsxgraph zip.js
1 parent 02d0191 commit 1c71700

4 files changed

Lines changed: 805 additions & 23 deletions

File tree

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ What is done
2222
* Load progress event
2323
* Get file as Base64
2424
* Get file as HTML Data URI (for hide from Network Requests)
25+
* GZIP Decompress Support, forked from jsxgraph/jsxgraph
2526

2627
TODO
2728
========
2829
* Load files from JPAK only when needed. A.K.A. - Fetch only the necessary parts from jpak file.
29-
30+
* Add option to compress files on packing.
3031

3132
How to use it
3233
========
@@ -179,6 +180,26 @@ JPAK Loader Error Codes:
179180
* 100->600 - HTML Status Code. Consult **RFC-2616** for details.
180181
* 8000 - Wrong file magic
181182

183+
184+
GZIP Support:
185+
========
186+
187+
The GZIP Decompress support is a modified version of **zip.js** from jsxgraph/jsxgraph project. For now, the default behaviour of `jpakloader` is to check the `compressed` flag at file entry. If its true, it tryes to decompress the file.
188+
189+
For compatible GZIPPing a file on Python you can do:
190+
```python
191+
import zlib
192+
f = open("uncompressed", "rb")
193+
data = f.read()
194+
f.close()
195+
data = zlib.compress(data, 9) # Level 9 is important
196+
f = open("compressed", "wb")
197+
f.write(data)
198+
f.close()
199+
```
200+
201+
* TODO on Tools: Add option to compress files on packer.py
202+
182203
Example
183204
========
184205

js/jpak.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ JPAK.Uint8ArrayToString = function(uintArray) {
5252
return o;
5353
}
5454

55+
JPAK.String2ArrayBuffer = function(str) {
56+
var buf = new ArrayBuffer(str.length);
57+
var bufView = new Uint8Array(buf);
58+
for (var i=0, strLen=str.length; i<strLen; i++)
59+
bufView[i] = str.charCodeAt(i) & 0xFF;
60+
return buf;
61+
};
62+
5563
var u8as = JPAK.Uint8ArrayToString; // Provided for compatibility
5664

5765
JPAK.Base64_Encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
@@ -252,7 +260,10 @@ JPAK.jpakloader.prototype.GetFile = function(path, type) {
252260

253261
if(file != undefined && cache == undefined) {
254262
// Add it to file cache
255-
var blob = new Blob([new Uint8Array(this.jpakdata.slice(file.offset,file.offset+file.size)).buffer], { "type":type});
263+
var dataslice = this.jpakdata.slice(file.offset,file.offset+file.size);
264+
if(file.compressed !== undefined && file.compressed)
265+
dataslice = JPAK.GZIP.decompress(dataslice);
266+
var blob = new Blob([new Uint8Array(dataslice).buffer], { "type":type});
256267
this.filecache.push({"path":path,"type":type,"blob":blob,"url":URL.createObjectURL(blob), "arraybuffer" : this.jpakdata.slice(file.offset,file.offset+file.size)} );
257268
return blob;
258269
}else if(cache != undefined)
@@ -284,33 +295,18 @@ JPAK.jpakloader.prototype.GetFileArrayBuffer = function(path, type) {
284295

285296
if(file != undefined && cache == undefined) {
286297
// Add it to file cache
287-
var blob = new Blob([new Uint8Array(this.jpakdata.slice(file.offset,file.offset+file.size)).buffer], { "type":type});
288-
var arraybuffer = this.jpakdata.slice(file.offset,file.offset+file.size);
298+
var dataslice = this.jpakdata.slice(file.offset,file.offset+file.size);
299+
if(file.compressed !== undefined && file.compressed)
300+
dataslice = JPAK.GZIP.decompress(dataslice);
301+
var blob = new Blob([new Uint8Array(dataslice).buffer], { "type":type});
289302
this.filecache.push({"path":path,"type":type,"blob":blob,"url":URL.createObjectURL(blob), "arraybuffer" : arraybuffer});
290-
return arraybuffer;
303+
return dataslice;
291304
}else if(cache != undefined)
292305
return cache.arraybuffer;
293306

294307
JPAK.log("Error: Cannot find file \""+path+"\"");
295308
return undefined;
296309
};
297-
// Returns an arraybuffer with file content. It looks in the cache for already loaded files.
298-
JPAK.jpakloader.prototype.GetFileArrayBuffer = function(path, type) {
299-
var file = this.FindFileEntry(path);
300-
type = type || 'application/octet-binary';
301-
var cache = this.CacheLoad(path);
302-
303-
if(file != undefined && cache == undefined) {
304-
// Add it to file cache
305-
var blob = new Blob([new Uint8Array(this.jpakdata.slice(file.offset,file.offset+file.size)).buffer], { "type":type});
306-
var arraybuffer = this.jpakdata.slice(file.offset,file.offset+file.size);
307-
this.filecache.push({"path":path,"type":type,"blob":blob,"url":URL.createObjectURL(blob), "arraybuffer" : arraybuffer});
308-
return arraybuffer;
309-
}else if(cache != undefined)
310-
return cache.arraybuffer;
311-
312-
return undefined;
313-
};
314310

315311
// Returns an Base64 Encoded File Content. It looks in the cache for already loaded files.
316312
JPAK.jpakloader.prototype.GetBase64File = function(path, type) {

0 commit comments

Comments
 (0)