jsziptools
It's a utility of zlib, gzip and zip format binary data.
suported browser
chrome, firefox, IE10, safari, opera.
examples
web examples
APIs
If the api is written as foo({a, b, c}), you can call it as below.
foo(1, 2, 3);
foo({
a: 1,
b: 2,
c: 3
});
Promise
jsziptools uses ES6 Promises and add spread
method.
Promise.prototype.spread(onFulfillment, onRejection)
It spreads a Array argument in a onFulfillment
callback.
// in ES6 Promises then
Promise.all([1, 2, 3]).then(function (args) {
var one = args[0], two = args[1], three = args[2];
});
// in Promise.prototype.spread
Promise.all([1, 2, 3]).spread(function (one, two, three) {
// ...
});
utils
jz.utils.toBytes(buffer)
-
@param {ArrayBuffer|Uint8Array|Array|string} buffer
-
@return {Uint8Array}
jz.utils.readFileAsArrayBuffer(blob)
jz.utils.readFileAsArrayBuffer(blob)
.then(function (buffer) {
// ...
});
jz.utils.readFileAsText(blob, encoding)
jz.utils.readFileAsDataURL(blob)
jz.utils.readFileAsBinaryString(blob)
jz.utils.bytesToString(buffer)
-
@param {Uint8Array|ArrayBuffer|Array|string} buffer
-
@return {Promise}
jz.utils.bytesToString(bytes)
.then(function (str) {
// ...
});
jz.utils.concatBytes(buffers)
-
@param {Array.<Uint8Array|ArrayBuffer>} buffers
-
@return {Uint8Array}
var concated = jz.utils.concatBytes([bytes1, bytes2]);
// or
var concated = jz.utils.concatBytes(bytes1, bytes2);
jz.utils.load(urls)
It loads files as Uint8Array
.
jz.utils.load(['foo.png', 'bar.jpg'])
.spread(function (foo, bar) {
// ...
});
// or
jz.utils.load('foo.png', 'bar.jpg')
.spread(function (foo, bar) {
// ...
});
algos
jz.algos.deflate({buffer, level, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {number} level - optional (default is
6
, range is 0-9)
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Uint8Array}
jz.algos.inflate({buffer, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Uint8Array}
jz.algos.adler32(buffer)
jz.algos.crc32({buffer, crc})
jz.stream.algos.deflate({buffer, streamFn, level, shareMemory, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {function(chunk: Uint8Array)} streamFn
-
@param {number} level - optional (default is
6
)
-
@param {boolean} shareMemory - optional (default is
false
)
-
@param {number} chunkSize - optional (default is
0x8000
)
jz.stream.algos.deflate({
buffer: buffer,
streamFn: function (chunk) {
// ...
},
shareMemory: false
});
jz.steram.algos.inflate({buffer, streamFn, shareMemory, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {function(chunk: Uint8Array)} streamFn
-
@param {boolean} shareMemory - optional (default is
false
)
-
@param {number} chunkSize - optional (default is
0x8000
)
zlib
jz.zlib.compress({buffer, level, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {number} level - optional (default is
6
)
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Uint8Array}
jz.zlib.decompress({buffer, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Uint8Array}
jz.stream.zlib.compress({buffer, streamFn, level, shareMemory, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {function(chunk: Uint8Array)} streamFn
-
@param {number} level - optional (default is
6
)
-
@param {boolean} shareMemory - optional (default is
false
)
-
@param {number} chunkSize - optional (default is
0x8000
)
jz.stream.zlib.decompress({buffer, streamFn, shareMemory, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {function(chunk: Uint8Array)} streamFn
-
@param {boolean} shareMemory - optional (default is
false
)
-
@param {number} chunkSize - optional (default is
0x8000
)
gzip
jz.gz.compress({buffer, level, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {number} level - optional (default is
6
)
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Uint8Array}
jz.gz.decompress({buffer, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Uint8Array}
jz.stream.gz.compress({buffer, streamFn, level, shareMemory, chunkSize, fname, fcomment})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {function(chunk: Uint8Array)} streamFn
-
@param {number} level - optional (default is
6
)
-
@param {boolean} shareMemory - optional (default is
false
)
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@param {string} fname - optional
-
@param {string} fcomment - optional
jz.stream.gz.decompress({buffer, streamFn, shareMemory, chunkSize})
-
@param {Uint8Array|ArrayBuffer} buffer
-
@param {function(chunk: Uint8Array)} streamFn
-
@param {boolean} shareMemory - optional (default is
false
)
-
@param {number} chunkSize - optional (default is
0x8000
)
zip
jz.zip.pack({files, level, chunkSize})
-
@param {Array.} files
-
@param {number} level - optional (default is
6
)
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Promise}
var files = [
{name: "foo", dir: [ // folder
{name: "hello.txt", buffer: "Hello World!"}, // string
{name: "bar.js", buffer: buffer}, // ArrayBuffer
{name: "hoge.mp3", url: "audiodata/hoge.mp3"} // xhr
]}
];
jz.zip.pack({
files: files,
level: 5
})
.then(function (buffer) {
// buffer is Uint8Array
});
// You cat set compression level to each files.
var files = [
{name: "mimetype", buffer: "application/epub+zip", level: 0}, //string
{name: "META-INF", dir: [ //folder
{name: "container.xml", buffer: buffer, level: 0}, //ArrayBuffer
]},
{name: "package.opf", url: "package.opf", level: 6},
{name: "foo.xhtml", url: "foo.xhtml", level: 9} //xhr
];
jz.zip.pack(files)
.then(function (buffer) {
// ...
});
jz.zip.unpack({buffer, encoding, chunkSize})
-
@param {Uint8Array|ArrayBuffer|Blob} buffer
-
@param {string} encoding - optional (default is
"UTF-8"
)
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Promise}
jz.zip.unpack({
buffer: buffer,
encoding: 'Shift_JIS' // encoding of filenames
})
.then(function (reader) {
// reader is ZipArchiveReader. See below.
// get file names.
reader.getFileNames();
reader.readFileAsText(reader.getFileNames[0])
.then(function (text) {
// ...
});
});
jz.stream.zip.pack({files, streamFn, level, shareMemory, chunkSize})
-
@param {Array} files
-
@param {function(chunk: Uint8Array)} streamFn
-
@param {number} level - optional (default is
6
)
-
@param {boolean} shareMemory - optional (default is
false
)
-
@param {number} chunkSize - optional (default is
0x8000
)
-
@return {Promise}
jz.stream.zip.pack({
files: files,
streamFn: function (chunk) {
// ...
}
})
.then(function () {
// no args
});
ZipArchiveReader
ZipArchiveReader#getFileNames()
It gets filenames in the zip archive.
ZipArchiveReader#readFileAsArrayBuffer(filename)
ZipArchiveReader#readFileAsBlob(filename)
ZipArchiveReader#readFileAsText(filename, encoding)
ZipArchiveReader#readFileAsDataURL(filename)
ZipArchiveReader#readFileAsBinaryString(filename)
ZipArchiveReader#readFileAsArrayBufferSync(filename)
ZipArchiveReader#readFileAsBlobSync(filename)
ZipArchiveReader#readFileAsTextSync(filename, encoding)
ZipArchiveReader#readFileAsBinaryStringSync(filename)
ZipArchiveReader#readFileAsDataURLSync(filename)
jz.zip.ZipArchiveWriter({shareMemory, chunkSize})
Low level zip archive writer.
-
@param {boolean} shareMemory - optional (default is
false
)
-
@param {number} chunkSize - optional (default is
0x8000
)
var writer = new jz.zip.ZipArchiveWriter({shareMemory: true, chunkSize: 0xf000});
writer
.on('data', function(chunk) {
// chunk is Uint8Array.
})
.on('end', function() {
// ...
});
.write('foo/bar/baz.txt', buffer)
.write('a.mp3', mp3Buff)
.writeEnd();
#on(name, callback)
#write(path, buffer, level)
Write the file. Directories are created automatically.
-
@param {string} path
-
@param {Uint8Array} buffer
-
@param {number} level - optional (default is
6
)
-
@return {jz.zip.ZipArchiveWriter} this
writer.write('a/b/c/d/foo.txt', buffer, 7);
#writeDir(path)
writer.writeDir('foo/');
// or
writer.writeDir('bar');
#writeFile(path, buffer, level)
-
@param {string} path
-
@param {Uint8Array} buffer
-
@param {number} level - optional (default is
6
)
-
@return {jz.zip.ZipArchiveWriter} this
writer.writeDir('a/');
writer.writeDir('a/b/');
writer.writeDir('a/b/c/');
writer.writeFile('a/b/c/foo.txt', buffer, 7);
#writeEnd()
Write central directory headers and the end central dirctory header.