Giter VIP home page Giter VIP logo

jsziptools's Introduction

jsziptools

It's a utility of zlib, gzip and zip format binary data.

suported browser

chrome, firefox, Edge, safari, opera.

examples

web examples

APIs

common

BufferLike

@type {string | number[] | ArrayBuffer | Uint8Array | Int8Array | Uint8ClampedArray}

jz.common.toBytes(buffer)

  • @param {BufferLike} buffer
  • @return {Uint8Array}

jz.common.readFileAsArrayBuffer(blob)

  • @param {Blob} blob
  • @return {Promise<ArrayBuffer>}
jz.common.readFileAsArrayBuffer(blob).then(buffer => {
  // ...
});

jz.common.readFileAsText(blob, encoding)

  • @param {Blob} blob
  • @param {string} encoding - optional (default is "UTF-8")
  • @return {Promise<string>}

jz.common.readFileAsDataURL(blob)

  • @param {Blob} blob
  • @return {Promise<string>}

jz.common.bytesToString(buffer)

  • @param {BufferLike} buffer
  • @return {Promise<string>}
jz.common.bytesToString(bytes).then(str => {
  // ...
});

jz.common.concatBytes(buffers)

  • @param {Array<BufferLike>} buffers
  • @return {Uint8Array}
let concated = jz.common.concatBytes([bytes1, bytes2]);
// or
let concated = jz.common.concatBytes(bytes1, bytes2);

jz.common.load(urls)

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]) => {
  // ...
});

core

jz.core.deflate({buffer, level, chunkSize})

  • @param {BufferLike} buffer
  • @param {number} level - optional (default is 6, range is 0-9)
  • @param {number} chunkSize - optional (default is 0x8000)
  • @return {Uint8Array}

jz.core.inflate({buffer, chunkSize})

  • @param {BufferLike} buffer
  • @param {number} chunkSize - optional (default is 0x8000)
  • @return {Uint8Array}

jz.core.adler32(buffer)

  • @param {BufferLike} buffer
  • @return {number}

jz.core.crc32({buffer, crc})

  • @param {BufferLike} buffer
  • @param {number}
  • @return {number}

jz.stream.core.deflate({buffer, streamFn, level, shareMemory, chunkSize})

  • @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,
});

jz.steram.core.inflate({buffer, streamFn, shareMemory, chunkSize})

  • @param {BufferLike} 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 {BufferLike} buffer
  • @param {number} level - optional (default is 6)
  • @param {number} chunkSize - optional (default is 0x8000)
  • @return {Uint8Array}

jz.zlib.decompress({buffer, chunkSize})

  • @param {BufferLike} buffer
  • @param {number} chunkSize - optional (default is 0x8000)
  • @return {Uint8Array}

jz.stream.zlib.compress({buffer, streamFn, level, shareMemory, chunkSize})

  • @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.zlib.decompress({buffer, streamFn, shareMemory, chunkSize})

  • @param {BufferLike} 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 {BufferLike} buffer
  • @param {number} level - optional (default is 6)
  • @param {number} chunkSize - optional (default is 0x8000)
  • @return {Uint8Array}

jz.gz.decompress({buffer, chunkSize})

  • @param {BufferLike} buffer
  • @param {number} chunkSize - optional (default is 0x8000)
  • @return {Uint8Array}

jz.stream.gz.compress({buffer, streamFn, level, shareMemory, chunkSize, fname, fcomment})

  • @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

jz.stream.gz.decompress({buffer, streamFn, shareMemory, chunkSize})

  • @param {BufferLike} 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(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, encoding, chunkSize})

    • @param {BufferLike | 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(reader => {
        // reader is ZipArchiveReader. See below.
        // get file names.
        reader.getFileNames();
        reader.readFileAsText(reader.getFileNames[0]).then(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: chunk => {
          // ...
        },
      })
      .then(() => {
        // no args
      });

    ZipArchiveReader

    ZipArchiveReader#getFileNames()

    It gets filenames in the zip archive.

    • @return {Array<string>}

    ZipArchiveReader#readFileAsArrayBuffer(filename)

    • @param {string} filename
    • @return {Promise<ArrayBuffer>}

    ZipArchiveReader#readFileAsBlob(filename)

    • @param {string} filename
    • @return {Promise<Blob>}

    ZipArchiveReader#readFileAsText(filename, encoding)

    • @param {string} filename
    • @param {string} encoding
    • @return {Promise<string>}

    ZipArchiveReader#readFileAsDataURL(filename)

    • @param {string} filename
    • @return {Promise<string>}

    ZipArchiveReader#readFileAsArrayBufferSync(filename)

    • @param {string} filename
    • @return {ArrayBuffer}

    ZipArchiveReader#readFileAsBlobSync(filename)

    • @param {string} filename
    • @return {Blob}

    ZipArchiveReader#readFileAsTextSync(filename, encoding)

    • @param {string} filename
    • @param {string} encoding
    • @return {string}

    ZipArchiveReader#readFileAsBinaryStringSync(filename)

    • @param {string} filename
    • @return {string}

    ZipArchiveReader#readFileAsDataURLSync(filename)

    • @param {string} filename
    • @return {string}

    jz.zip.ZipArchiveWriter({shareMemory, chunkSize})

    Low level zip archive writer.

    • @param {boolean} shareMemory - optional (default is false)
    • @param {number} chunkSize - optional (default is 0x8000)
    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();

    #on(name, callback)

    • @param {string} name
    • @param {function} callback
    • @return {jz.zip.ZipArchiveWriter} this

    #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)

    • @param {string} path
    • @return {jz.zip.ZipArchiveWriter} this
    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.

    writer.writeEnd();

    Import subdirectries

    You can import modules from subdirectries.

    import { deflate } from "jsziptools/core";
    import * as zip from "jsziptools/zip";
    
    deflate(...);
    zip.pack(...).then();

jsziptools's People

Contributors

brentbrownfield avatar masatsuguaida avatar ukyo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jsziptools's Issues

検討依頼: 空ファイルのサポート

これでfailが発生しますが、正しい挙動でしょうか。

jz.zip.pack({files: [{name:"aa.txt", buffer:""}], level: 5).fail(function(error) { alert(error);})

意図的に弾いているようにも見受けられましたが、空ファイルはそれなりに使う機会があるので、サポート出来るならサポートしていただけると助かります。ご検討ください。

Streamに対応する

こんな感じで実装したい。

jz.stream.zip.pack(files)
.on('data', function(data) {
  // データの塊(一部分)を受け取って色々する。
})
.on('error', function(err) {
  // エラーを受け取る
})
.on('end', function(info) {
  // 終わったときの処理。バイト数とかの情報を受け取ると良さそう。
});

どっちにすべきか。

jz.stream.zip.pack
jz.zip.pack.stream

Can you please update your documentation

Hello,

I have tried to use the latest release 2.4.7. My code was using the readFileAsBlob method of the reader returned by js.zip.unpack but this method does not exist anymore apparently although the documentation still mentions it explicitly. It is the same thing in the code example. For now I have reverted to 2.4.1 and it is working fine but could you please maintain your documentation up to date and mention how should the reader be used in the new flavor?

Thank you and best regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.