Android Question Problem with ZipManager

Hirogens

Active Member
Licensed User
Longtime User
Hello, I try to zip 3 files in 1 zip. I use a classe ZipManager and I have this error:
B4X:
zipmanager_zip (java line: 73)
java.lang.RuntimeException: Method: zipFiles not found in: rpcycling.appdev.zipmanager
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
    at rpcycling.appdev.zipmanager._zip(zipmanager.java:73)
    at rpcycling.appdev.page_activite._save_fatigue_click(page_activite.java:1278)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24697)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

And this is my code:
B4X:
Dim zm As ZipManager
        zm.Initialize
        zm.Zip(Array As String (File.Combine(shared, json_name), _
                        File.Combine(shared, chunck_name), _
                        File.Combine(shared, gps_name)), _
                        File.Combine(shared, "myimages.zip"))
B4X:
Sub Class_Globals
    Private jo As JavaObject
End Sub

'Initializes the ZipManager
Public Sub Initialize
    jo = Me
End Sub

Public Sub Zip (Source() As String, Destination As String)
    jo.RunMethod("zipFiles", Array(Source,Destination))
End Sub

Public Sub UnZip (Source As String, Destination As String) As Boolean
    Return jo.RunMethod("unzipFiles", Array(Source,Destination))
End Sub
This is the class.
Thanks
 

npsonic

Active Member
Licensed User
It just says that there is no method "zipFiles". Did you use ZipManager class that I created. There are methods "zipFiles" and "unzipFiles" that are part of inline java.
You should copy whole class as it is, so nothing isn't left out from it.
 
Upvote 0

Hirogens

Active Member
Licensed User
Longtime User
It just says that there is no method "zipFiles". Did you use ZipManager class that I created. There are methods "zipFiles" and "unzipFiles" that are part of inline java.
You should copy whole class as it is, so nothing isn't left out from it.
Hello, Yes I had copy all your class, but I need something in addition to create a zip ?
 
Upvote 0

npsonic

Active Member
Licensed User
Hello, Yes I had copy all your class, but I need something in addition to create a zip ?
You shouldn't need anything else if you used ZipManager class as it is.

This was what I wrote, but exception you are getting would implicate that there is no method "zipFiles".
Does your ZipManager look like this?

B4X:
Sub Class_Globals
    Private jo As JavaObject
End Sub

'Initializes the ZipManager
Public Sub Initialize
    jo = Me
End Sub

Public Sub Zip (Source() As String, Destination As String)
    jo.RunMethod("zipFiles", Array(Source,Destination))
End Sub

Public Sub UnZip (Source As String, Destination As String) As Boolean
    Return jo.RunMethod("unzipFiles", Array(Source,Destination))
End Sub

#If Java
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.util.zip.ZipOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.io.File;
import java.util.zip.ZipInputStream;

private static int BUFFER_SIZE = 6 * 1024;
 
public static void zipFiles(String[] files, String zipFile) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
    try {
        byte data[] = new byte[BUFFER_SIZE];

        for (int i = 0; i < files.length; i++) {
            FileInputStream fi = new FileInputStream(files[i]);
            origin = new BufferedInputStream(fi, BUFFER_SIZE);
            try {
                ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }
            } finally {
                origin.close();
            }
        }
    } finally {
        out.close();
    }
}

public static boolean unzipFiles(String zipFile, String location) throws IOException {
    try {
        File f = new File(location);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
        try {
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                String path = location + File.separator + ze.getName();

                if (ze.isDirectory()) {
                    File unzipFile = new File(path);
                    if (!unzipFile.isDirectory()) {
                        unzipFile.mkdirs();
                    }
                } else {
                    FileOutputStream fout = new FileOutputStream(path, false);

                    try {
                        for (int c = zin.read(); c != -1; c = zin.read()) {
                            fout.write(c);
                        }
                        zin.closeEntry();
                    } finally {
                        fout.close();
                    }
                }
            }
        } finally {
            zin.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
#End If
 
Upvote 0

Hirogens

Active Member
Licensed User
Longtime User
You shouldn't need anything else if you used ZipManager class as it is.

This was what I wrote, but exception you are getting would implicate that there is no method "zipFiles".
Does your ZipManager look like this?

B4X:
Sub Class_Globals
    Private jo As JavaObject
End Sub

'Initializes the ZipManager
Public Sub Initialize
    jo = Me
End Sub

Public Sub Zip (Source() As String, Destination As String)
    jo.RunMethod("zipFiles", Array(Source,Destination))
End Sub

Public Sub UnZip (Source As String, Destination As String) As Boolean
    Return jo.RunMethod("unzipFiles", Array(Source,Destination))
End Sub

#If Java
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.util.zip.ZipOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.io.File;
import java.util.zip.ZipInputStream;

private static int BUFFER_SIZE = 6 * 1024;
 
public static void zipFiles(String[] files, String zipFile) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
    try {
        byte data[] = new byte[BUFFER_SIZE];

        for (int i = 0; i < files.length; i++) {
            FileInputStream fi = new FileInputStream(files[i]);
            origin = new BufferedInputStream(fi, BUFFER_SIZE);
            try {
                ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }
            } finally {
                origin.close();
            }
        }
    } finally {
        out.close();
    }
}

public static boolean unzipFiles(String zipFile, String location) throws IOException {
    try {
        File f = new File(location);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
        try {
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                String path = location + File.separator + ze.getName();

                if (ze.isDirectory()) {
                    File unzipFile = new File(path);
                    if (!unzipFile.isDirectory()) {
                        unzipFile.mkdirs();
                    }
                } else {
                    FileOutputStream fout = new FileOutputStream(path, false);

                    try {
                        for (int c = zin.read(); c != -1; c = zin.read()) {
                            fout.write(c);
                        }
                        zin.closeEntry();
                    } finally {
                        fout.close();
                    }
                }
            }
        } finally {
            zin.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
#End If

This is what I have
B4X:
Sub Class_Globals
    Private jo As JavaObject
End Sub

'Initializes the ZipManager
Public Sub Initialize
    jo = Me
End Sub

Public Sub Zip (Source() As String, Destination As String)
    jo.RunMethod("zipFiles", Array(Source,Destination))
End Sub

Public Sub UnZip (Source As String, Destination As String) As Boolean
    Return jo.RunMethod("unzipFiles", Array(Source,Destination))
End Sub
 
Upvote 0

npsonic

Active Member
Licensed User
This is what I have
B4X:
Sub Class_Globals
    Private jo As JavaObject
End Sub

'Initializes the ZipManager
Public Sub Initialize
    jo = Me
End Sub

Public Sub Zip (Source() As String, Destination As String)
    jo.RunMethod("zipFiles", Array(Source,Destination))
End Sub

Public Sub UnZip (Source As String, Destination As String) As Boolean
    Return jo.RunMethod("unzipFiles", Array(Source,Destination))
End Sub
Thats not correct. Did you use proper way to add existing module?

1. Download ZipManager
2. Project > Add Existing Modules
3. Navigate to ZipManager download dir, select ZipManager and click Open.
4. Select 'Copy to project folder' and click OK
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I created a project using @npsonic's ZipManager class. It works very well. I have a couple of small observations and only one question #4:
1. If you have files in File.DirAssets, the files must be copied to one of the other folders, like internal, defautlExt or rooExt before you can zip them.
2. If you prefer, you can replace for example:
File.Combine(File.DirRootExternal, "languages.txt") with File.DirRootExternal & "/languages.txt"
3. I iterated over the files and created the array before issuing the zip. It made my code neater.
4. I do not know if you need a zip and unzip complete event to be raised if you are zipping hundreds of files. I tested with at least 10 files and it works flawlessly. This is for npsonic to answer.
5. @npsonic , keep up the good work.
 
Upvote 0

npsonic

Active Member
Licensed User
I created a project using @npsonic's ZipManager class. It works very well. I have a couple of small observations and only one question #4:
1. If you have files in File.DirAssets, the files must be copied to one of the other folders, like internal, defautlExt or rooExt before you can zip them.
2. If you prefer, you can replace for example:
File.Combine(File.DirRootExternal, "languages.txt") with File.DirRootExternal & "/languages.txt"
3. I iterated over the files and created the array before issuing the zip. It made my code neater.
4. I do not know if you need a zip and unzip complete event to be raised if you are zipping hundreds of files. I tested with at least 10 files and it works flawlessly. This is for npsonic to answer.
5. @npsonic , keep up the good work.
You were right it lags when zipping hundreds of files. I added "ZipComplete" and "UnZipComplete" events. If someone is trying to find the ZipManager it's here.
 
Upvote 0
Top