It's a utility of zlib, gzip and zip format binary data.
chrome, firefox, Edge, safari, opera.
@type {string | number[] | ArrayBuffer | Uint8Array | Int8Array | Uint8ClampedArray}
- @param {BufferLike} buffer
- @return {Uint8Array}
- @param {Blob} blob
- @return {Promise<ArrayBuffer>}
jz.common.readFileAsArrayBuffer(blob).then(buffer => {
// ...
});
- @param {Blob} blob
- @param {string} encoding - optional (default is
"UTF-8"
) - @return {Promise<string>}
- @param {Blob} blob
- @return {Promise<string>}
- @param {BufferLike} buffer
- @return {Promise<string>}
jz.common.bytesToString(bytes).then(str => {
// ...
});
- @param {Array<BufferLike>} buffers
- @return {Uint8Array}
let concated = jz.common.concatBytes([bytes1, bytes2]);
// or
let concated = jz.common.concatBytes(bytes1, bytes2);
It loads files as Uint8Array
.
- @param {Array<string>} urls
- @return {Promise<Uint8Array[]>}
jz.common.load(['foo.png', 'bar.jpg']).then(([foo, bar]) => {
// ...
});
// or
jz.common.load('foo.png', 'bar.jpg').then(([foo, bar]) => {
// ...
});
- @param {BufferLike} buffer
- @param {number} level - optional (default is
6
, range is 0-9) - @param {number} chunkSize - optional (default is
0x8000
) - @return {Uint8Array}
- @param {BufferLike} buffer
- @param {number} chunkSize - optional (default is
0x8000
) - @return {Uint8Array}
- @param {BufferLike} buffer
- @return {number}
- @param {BufferLike} buffer
- @param {number}
- @return {number}
- @param {BufferLike} 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.core.deflate({
buffer: buffer,
streamFn: chunk => {
// ...
},
shareMemory: false,
});
- @param {BufferLike} buffer
- @param {function(chunk: Uint8Array)} streamFn
- @param {boolean} shareMemory - optional (default is
false
) - @param {number} chunkSize - optional (default is
0x8000
)
- @param {BufferLike} buffer
- @param {number} level - optional (default is
6
) - @param {number} chunkSize - optional (default is
0x8000
) - @return {Uint8Array}
- @param {BufferLike} buffer
- @param {number} chunkSize - optional (default is
0x8000
) - @return {Uint8Array}
- @param {BufferLike} 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 {BufferLike} buffer
- @param {function(chunk: Uint8Array)} streamFn
- @param {boolean} shareMemory - optional (default is
false
) - @param {number} chunkSize - optional (default is
0x8000
)
- @param {BufferLike} buffer
- @param {number} level - optional (default is
6
) - @param {number} chunkSize - optional (default is
0x8000
) - @return {Uint8Array}
- @param {BufferLike} buffer
- @param {number} chunkSize - optional (default is
0x8000
) - @return {Uint8Array}
- @param {BufferLike} 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
- @param {BufferLike} buffer
- @param {function(chunk: Uint8Array)} streamFn
- @param {boolean} shareMemory - optional (default is
false
) - @param {number} chunkSize - optional (default is
0x8000
)
- @param {Array.} files
- @param {number} level - optional (default is
6
) - @param {number} chunkSize - optional (default is
0x8000
) - @return {Promise}
- @param {BufferLike | Blob} buffer
- @param {string} encoding - optional (default is
"UTF-8"
) - @param {number} chunkSize - optional (default is
0x8000
) - @return {Promise}
- @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}
- @return {Array<string>}
- @param {string} filename
- @return {Promise<ArrayBuffer>}
- @param {string} filename
- @return {Promise<Blob>}
- @param {string} filename
- @param {string} encoding
- @return {Promise<string>}
- @param {string} filename
- @return {Promise<string>}
- @param {string} filename
- @return {ArrayBuffer}
- @param {string} filename
- @return {Blob}
- @param {string} filename
- @param {string} encoding
- @return {string}
- @param {string} filename
- @return {string}
- @param {string} filename
- @return {string}
- @param {boolean} shareMemory - optional (default is
false
) - @param {number} chunkSize - optional (default is
0x8000
) - @param {string} name
- @param {function} callback
- @return {jz.zip.ZipArchiveWriter} this
- @param {string} path
- @param {Uint8Array} buffer
- @param {number} level - optional (default is
6
) - @return {jz.zip.ZipArchiveWriter} this
- @param {string} path
- @return {jz.zip.ZipArchiveWriter} this
- @param {string} path
- @param {Uint8Array} buffer
- @param {number} level - optional (default is
6
) - @return {jz.zip.ZipArchiveWriter} this
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(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(buffer => {
// ...
});
jz.zip
.unpack({
buffer: buffer,
encoding: 'Shift_JIS', // encoding of filenames
})
.then(reader => {
// reader is ZipArchiveReader. See below.
// get file names.
reader.getFileNames();
reader.readFileAsText(reader.getFileNames[0]).then(text => {
// ...
});
});
jz.stream.zip
.pack({
files: files,
streamFn: chunk => {
// ...
},
})
.then(() => {
// no args
});
It gets filenames in the zip archive.
Low level zip archive writer.
const writer = new jz.zip.ZipArchiveWriter({ shareMemory: true, chunkSize: 0xf000 });
writer
.on('data', chunk => {
// chunk is Uint8Array.
})
.on('end', () => {
// ...
})
.write('foo/bar/baz.txt', buffer)
.write('a.mp3', mp3Buff)
.writeEnd();
Write the file. Directories are created automatically.
writer.write('a/b/c/d/foo.txt', buffer, 7);
writer.writeDir('foo/');
// or
writer.writeDir('bar');
writer.writeDir('a/');
writer.writeDir('a/b/');
writer.writeDir('a/b/c/');
writer.writeFile('a/b/c/foo.txt', buffer, 7);
Write central directory headers and the end central dirctory header.
writer.writeEnd();
You can import modules from subdirectries.
import { deflate } from "jsziptools/core";
import * as zip from "jsziptools/zip";
deflate(...);
zip.pack(...).then();