Skip to content

Latest commit

 

History

History
171 lines (126 loc) · 4.42 KB

README.md

File metadata and controls

171 lines (126 loc) · 4.42 KB

@nativescript/zip

A plugin to zip and unzip files.

Contents

Installation

npm install @nativescript/zip

Use @nativescript/zip

Zip a folder

To zip a folder, call the zip() method on the Zip class, passing it a ZipOptions object.

async zip() {

        let zipPath = path.join(knownFolders.temp().path, 'stuff.zip');
        let dest = path.join(knownFolders.currentApp().path, '/assets');
        
        const zipped = await Zip.zip({

            directory: dest,
            archive: zipPath,
        }).then((value)=>{
            console.log("zipped",value)

        }).catch(err=>{
            console.error(err)
        });
    }

Get zip request progress

To get the zip request progress, set the onProgress property to a function to handle the progress event.

in ZipOptions object passed to the zip().

import { Zip } from '@nativescript/zip';
import { path, knownFolders } from '@nativescript/core';
let zipPath = path.join(knownFolders.temp().path, 'stuff.zip');
let dest = path.join(knownFolders.documents().path, '/assets');

Zip.zip({
	directory: dest,
	archive: zipPath,
	onProgress: onZipProgress
});
function onZipProgress(percent: number) {

    console.log(`unzip progress: ${percent}`);
}

Unzip a folder

To unzip a folder, call the unzip() method on the Zip class, passing it a UnZipOptions object.

import { Zip } from '@nativescript/zip';
import { path, knownFolders } from '@nativescript/core';

let zipPath = path.join(knownFolders.temp().path, 'stuff.zip');
let dest = path.join(knownFolders.documents().path, '/assets');
Zip.unzip({
	archive: zipPath,
	directory: dest,
});

Get unzip request progress

To get the unzip request progress, set the onProgress property to a function to handle the progress event.

import { Zip } from '@nativescript/zip';
import { path, knownFolders } from '@nativescript/core';
let zipPath = path.join(knownFolders.temp().path, 'stuff.zip');
let dest = path.join(knownFolders.documents().path, '/assets');
async unzip(){
	
const unzipped = await Zip.unzip({
	archive: zipPath,
	directory: dest,
	onProgress: onUnZipProgress,
});

}

function onUnZipProgress(percent: number) {
	console.log(`unzip progress: ${percent}`);
}

API

Zip class

debugEnabled

Zip.debugEnabled = true

zip()

Zip.zip(options).then((zipped: string) => {

}).catch((err) => {

})

Zips the folder at the path specified in the directory property of the options parameter. It saves the zipped folder at the path set via archive property. The options object has the following properties:

ZipOptions interface

Property Type
directory string
archive string
onProgress (progress: number) => void
keepParent boolean
password string

unzip()

Zip.unzip(options).then((unZipped: any) => {

}).catch((err) => {

})

Extracts all the files from the zip file specified in the archive property of the options parameter. The extracted files are stored at the path specified via the directory property. The options object has the following properties:

UnZipOptions interface

Property Type
directory string
archive string
onProgress (progress: number) => void
overwrite boolean
password string

License

Apache License Version 2.0