Preview 2.0 is now in Public Beta!
Read the Announcement

@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 | Optional: The directory to zip. | | archive | string | Optional: The path to zip directory to. | | onProgress | (progress: number) => void | Optional: Zip request progress listener. | | keepParent | boolean | Optional: | | password | string | Optional: The password used to protect the zip file. |


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 | Optional: The path to extract the files to. | | archive | string | Optional: The path of the zip file. | | onProgress | (progress: number) => void | Optional: Unzip request progress listener. | | overwrite | boolean | Optional: (iOS-only) | | password | string | Optional: The password used to protect the zip file. |

License

Apache License Version 2.0