Git Product home page Git Product logo

ziploader's Introduction

ZipLoader

ZipLoader is a light weight zip file loader and unzipper for Web browsers. (only 25KB (gzipped 9KB) )

Plus, It makes easy to make loading progress bar.

Latest NPM release MIT License dependencies Status devDependencies Status

Demos can be seen here.

Usage

Both standalone version and NPM module version are available.

Standalone

Copy zip-loader.js from /dist/zip-loader.js and place it in your project.

<script src="./js/zip-loader.js"></script>

NPM

$ npm install --save zip-loader

then

import ZipLoader from 'zip-loader';

To load and unzip

Make a loader instance with a target zip url. Then, load() it.

var loader = new ZipLoader( './foldername.zip' );
loader.load();

You can get loading progress in 'progress' event while loading.

When it's done, the zip file is automatically unzipped and 'load' event will be triggered.

As the result, you will get files property under the instance, that consists of filename and binary buffer.

var loader = new ZipLoader( './foldername.zip' );

loader.on( 'progress', function ( event ) {

  console.log( 'loading', event.loaded, event.total );

} );

loader.on( 'load', function ( event ) {

  console.log( 'loaded!' );
  console.log( loader.files );

  var json = loader.extractAsJSON( 'foldername/data.json' );
  console.log( json );

} );

loader.on( 'error', function ( event ) {

  console.log( 'error', event.error );

} );

loader.load();

It returns Promise as well.

var loader = new ZipLoader( './foldername.zip' );

loader.load().then( function () {

  console.log( 'loaded!' );
  console.log( loader.files );

  var json = loader.extractAsJSON( 'foldername/data.json' );
  console.log( json );

} );

unzip Blob/File directly

ZipLoader.unzip( blobOrFile ).then( function ( ZipLoaderInstance ) {

  console.log( ZipLoaderInstance.files );

} );

Pick up unzipped files

There are 3 (+1) ways to pick up unzipped files.

  • as a text.
  • as a JSON.
  • as an URL (for <img>, <audio>, <video> etc).

The 1st augment is key of loader.files object, that represents "path + filename" in zipped folder.

As a text

var string = loader.extractAsText( 'foldername/text.txt' );

As a JSON

var json = loader.extractAsJSON( 'foldername/data.json' );

As an URL

The 2nd arguments is its MIMEType.

var url = loader.extractAsBlobUrl( 'foldername/pict.jpg', 'image/jpeg' );

with three.js

ZipLoader can provide altanative JSONLoader for zipped JSON.

// At first, prepare to use THREE.js in ZipLoader
ZipLoader.install( { THREE: THREE } );

var loader = new ZipLoader( './assets.zip' );

loader.on( 'load', function ( e ) {

  var result = loader.loadThreeJSON( 'assets/threejs-model.json' );

  var mesh = new THREE.Mesh(
    result.geometry,
    new THREE.MultiMaterial( result.materials )
  );

  scene.add( mesh );

} );

Clear cache

After unzipped, loader instance will store the data. When you don't need the data, you can clear stored cache.

To clear single cache

myImg.onload = function () {

  loader.clear( 'foldername/pict.jpg' );

}

To clear all cache (sort of its destructor)

loader.clear();

ziploader's People

Contributors

yomotsu avatar wernerson avatar ngokevin avatar asiekierka avatar

Watchers

James Cloos avatar  avatar

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.