Zip the Folder/File using NativeScript-Zip plugin


#1

Hello Everyone!

I am trying to zip the folder using nativescript-zip plugin by @triniwiz.

I have followed plugin docs https://www.npmjs.com/package/nativescript-zip#zip to zip the folder,

this is how i tried to zip the folder,

Method - 1 :

zip() {
        let path = fs.path.join(fs.knownFolders.documents().path, "test");
        let desti = fs.path.join(fs.knownFolders.currentApp().path, "assets");
        console.log("desti path ", desti)
        Zip.zip(path, desti, null, null)
            .then(() => {
                console.log(`zip succesfully completed`);
                let ttt = fs.path.join(desti, "test.zip");
                let exists = fs.File.exists(ttt);
                console.log("Does test.zip exists: " + exists);
            })
            .catch(err => {
                console.log(`zip error: ${JSON.stringify(err)}`);
            });
    }

then in console,

JS : desti path  /data/data/org.nativescript.TinyUrl/files/app/assets
JS: zip succesfully completed
JS: Does test.zip exists: false

zip returns promise with ‘zip succesfully completed’ console log. Also i tried to check zip file exists but it returns false. And I cannot able to access the generated .zip file since its an inapp protected folder.

Method - 2 :

If i tried to set destination as other folders like downloads apart from inapp folders. then zip is not working its showing errors like ‘The specified parameter is not a File entity’.

 zip() {
        let androidDownloadsPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();
        console.log("desti path ", androidDownloadsPath)

        Zip.zip(path, androidDownloadsPath, '', '')
            .then(() => {
                console.log(`zip succesfully completed`);
            })
            .catch(err => {
                console.log(`zip error: ${JSON.stringify(err)}`);
            });
    }

console,

JS: desti path  /storage/emulated/0/Download
JS: zip error: {"message":"The specified parameter is not a File entity."}

Method - 3 :

zip() {
        let androidDownloadsPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();

        let path = fs.path.join(fs.knownFolders.documents().path, "api");
        let desti = fs.path.join(androidDownloadsPath , "assets");
        console.log("desti path ", desti)

        Zip.zip(path, desti, '', '')
            .then(() => {
                console.log(`zip succesfully completed`);   
            })
            .catch(err => {
                console.log(`zip error: ${JSON.stringify(err)}`);
            });

    }

console,

JS: desti path  /storage/emulated/0/Download/assets
JS: zip error: {"message":"The specified parameter is not a File entity."}

Can anyone / @triniwiz help me out on this.

.Thanks


#2

With your first method itself it should be successful. Looking at the code I believe the zip file is written in current app path as asset itself as a file not a folder. You will have to mention the destination file not just folder as your second parameter.

To verify same read and log all entities in your destination before and after writing zip to see if at all something changed.


#3

Yes. Thanks @manojdcoder,

As per plugin’s android handler we have to mention the destination file not just folder as second parameter,


#4

FYI, Working Code!

    public androidDownloadsPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();


 zip() {

        //clear the test folder to create new zip folder
        let foldertodelete = fs.knownFolders.documents().getFolder("test");
        if (foldertodelete) {
            foldertodelete.clear()
                .then(res => {
                    console.log("Folder cleared successfully")
                })
                .catch(err => {
                    console.log("Error" + err.stack)
                });
        }
        else {
            console.log("cannot clear already deleted folder")
        }

        //check the documents folder to cross check zip file after zipped
        fs.knownFolders.documents().getEntities()
            .then(entities => {
                entities.forEach(entity => {
                    console.log("****************** before - avail something******************")
                    console.log(entity.name);
                    console.log(entity.path);
                    console.log(entity.lastModified);
                });

            }).catch(err => {
                console.log(err.stack);
            });

        //create dummy files in stuff folder to zip
        let f = fs.Folder.fromPath(fs.knownFolders.temp().path).getFolder("stuff");
        f.getFile("text122.txt");
        f.getFile("image222.jpg");
        f.getFile("doc332.doc");
        f.getFile("pdf442.pdf");
        let path = fs.path.join(fs.knownFolders.temp().path, "stuff");

        //zip the folder
        let desti = fs.path.join(fs.knownFolders.documents().path, "stuff.zip");
        console.log("desti path ", desti);

        Zip.zipWithProgress(path, desti, onZipProgress, false, false)
            .then(() => {
                console.log(`zip succesfully completed`);

                //check zip file exists after zip
                let ttt = fs.path.join(fs.knownFolders.documents().path, "stuff.zip");
                let exists = fs.File.exists(ttt);
                console.log("Does stuff.zip exists: " + exists);

                //copy the zip file to the downloads folder
                let tttfile = fs.File.fromPath(ttt);

                let desti = fs.path.join(this.androidDownloadsPath, "Aud Details");
                let destifolder = fs.Folder.fromPath(desti).getFile("stuff.zip");

                if (destifolder) {
                    let binaryfile = tttfile.readSync(err => {
                        console.log("Error Readsync " + err.stack)
                    });
                    destifolder.writeSync(binaryfile, err => {
                        console.log("Error " + err)
                    });
                }

                //unzip the zip file
                Zip.unzip(ttt, fs.path.join(fs.knownFolders.documents().path, "test"))
                    .then(() => {
                        console.log("unzipped successfully");

                        //check entities after unzip
                        fs.Folder.fromPath(fs.path.join(fs.knownFolders.documents().path, "test", "stuff")).getEntities()
                            .then(entities => {
                                entities.forEach(entity => {
                                    console.log("after zip - avail something")
                                    console.log(entity.name);
                                    console.log(entity.path);
                                    console.log(entity.lastModified);
                                });
                            })
                            .catch(err => {
                                console.log("Error", JSON.stringify(err))
                            });
                    })
                    .catch(err => {
                        console.log("unzip Error " + JSON.stringify(err))
                    })

            })
            .catch(err => {
                console.log(`zip error: ${JSON.stringify(err)}`);
            });

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

Console

JS: desti path  /data/data/org.nativescript.TinyUrl/files/stuff.zip
JS: Folder cleared successfully
JS: ****************** before - avail something******************
JS: app
JS: /data/data/org.nativescript.TinyUrl/files/app
JS: Wed May 09 2018 18:56:48 GMT+0530 (IST)
JS: ****************** before - avail something******************
JS: internal
JS: /data/data/org.nativescript.TinyUrl/files/internal
JS: Wed May 09 2018 18:56:27 GMT+0530 (IST)
JS: ****************** before - avail something******************
JS: metadata
JS: /data/data/org.nativescript.TinyUrl/files/metadata
JS: Wed May 09 2018 18:56:27 GMT+0530 (IST)
JS: ****************** before - avail something******************
JS: assetsThumb
JS: /data/data/org.nativescript.TinyUrl/files/assetsThumb
JS: Wed May 09 2018 19:05:18 GMT+0530 (IST)
JS: ****************** before - avail something******************
JS: test
JS: /data/data/org.nativescript.TinyUrl/files/test
JS: Wed May 09 2018 19:06:01 GMT+0530 (IST)
JS: ****************** before - avail something******************
JS: stuff.zip
JS: /data/data/org.nativescript.TinyUrl/files/stuff.zip
JS: Thu Jan 01 1970 05:30:00 GMT+0530 (IST)
JS: zip succesfully completed
JS: Does stuff.zip exists: true
JS: unzipped successfully
JS: after zip - avail something
JS: text122.txt
JS: /data/data/org.nativescript.TinyUrl/files/test/stuff/text122.txt
JS: Wed May 09 2018 19:06:00 GMT+0530 (IST)
JS: after zip - avail something
JS: image222.jpg
JS: /data/data/org.nativescript.TinyUrl/files/test/stuff/image222.jpg
JS: Wed May 09 2018 19:06:00 GMT+0530 (IST)
JS: after zip - avail something
JS: doc332.doc
JS: /data/data/org.nativescript.TinyUrl/files/test/stuff/doc332.doc
JS: Wed May 09 2018 19:06:00 GMT+0530 (IST)
JS: after zip - avail something
JS: pdf442.pdf
JS: /data/data/org.nativescript.TinyUrl/files/test/stuff/pdf442.pdf
JS: Wed May 09 2018 19:06:00 GMT+0530 (IST)