srikanth-lingala / zip4j

A Java library for zip files and streams
Apache License 2.0
2.01k stars 307 forks source link

Help #489

Closed Arashvscode closed 1 year ago

Arashvscode commented 1 year ago

hi me using in code


import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import android.app.ProgressDialog;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.progress.ProgressMonitor;

public class NinjaZipPost {
    private static ProgressMonitor progress_m = new ProgressMonitor();
    private static TimerTask timer;
    private static ProgressDialog dialog;
    private static Timer _timer = new Timer();

    public static void ZipFileInProGrassBar(Activity activity, String input, String output) {
        dialog = new ProgressDialog(activity, ProgressDialog.THEME_DEVICE_DEFAULT_DARK);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();

        if ((!progress_m.getState().equals(ProgressMonitor.State.READY))) {
            dialog.setMax((int) 100);
            dialog.setProgress((int) (progress_m.getPercentDone()));

        }
        if (progress_m.getResult().equals(ProgressMonitor.Result.SUCCESS)) {
            dialog.setMessage("o");
            AsyncTask.execute(() -> {

                try {
                    try {
                        java.lang.Thread.sleep(50);
                    } catch (java.lang.InterruptedException exc) {
                    }
                    ZipFile zi = new ZipFile(output);
                    progress_m = zi.getProgressMonitor();
                    zi.setRunInThread(true);
                    zi.addFile(new java.io.File(input));
                    dialog.dismiss();
                } catch (Exception e) {

                }

            });
        }

    }

}

end my app Error Not Run Plz help me...

srikanth-lingala commented 1 year ago

What is the error/exception you get?

Arashvscode commented 1 year ago

The main problem is here, it doesn't give any error, only the program closes. I also wrote a new code that I am sending to you, it also gives an error.



public static void unzio(Activity activity) {
        final ProgressDialog dialog = new ProgressDialog(activity, ProgressDialog.THEME_DEVICE_DEFAULT_DARK);
        dialog.setTitle("Unzip.....");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax((int) 100);
        dialog.show();
        Thread thread = new Thread(() -> {
            ZipFile zipFile = new ZipFile("/storage/emulated/0/apk/color.zip");
            zipFile.setRunInThread(true);
            try {
                zipFile.extractAll("/storage/emulated/0/apk/");
            } catch (ZipException e) {
                throw new RuntimeException(e);
            }
            ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
            while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) {

                activity.runOnUiThread(() -> {
                    dialog.setProgress(progressMonitor.getPercentDone());
                });

            }
            activity.runOnUiThread(() -> {
                dialog.dismiss();
            });
        });
        thread.start();

    }

``
srikanth-lingala commented 1 year ago

When you run zip4j in thread mode, any exceptions are stored in ProgressMonitor. Look at the example code here to check for exceptions. Especially this part:

if (progressMonitor.getResult().equals(ProgressMonitor.Result.SUCCESS)) {
  System.out.println("Successfully added folder to zip");
} else if (progressMonitor.getResult().equals(ProgressMonitor.Result.ERROR)) {
  System.out.println("Error occurred. Error message: " + progressMonitor.getException().getMessage());
} else if (progressMonitor.getResult().equals(ProgressMonitor.Result.CANCELLED)) {
  System.out.println("Task cancelled");
}
Arashvscode commented 1 year ago

Hi, I used your code, but it doesn't do anything in Proges Dialog, can you give me a real code example using Proges Dialog?

srikanth-lingala commented 1 year ago

I am not an Android programmer. Unfortunately, I can't help you with that request. However, I know that a lot of Android projects use zip4j and ProgressMonitor. Maybe there are some open sources ones too with code hosted on github, etc.

Arashvscode commented 1 year ago

aha ok bro tnks to Help me