vendredi 21 avril 2017

Android unzipping loop issue

I am downloading zip file in my app from the server. Storing in local storage. This runs perfectly.

Now I use this to unzip it. and the decompress class is also there. My issue is that my logcat shows that the process is running multiple times , equivalent to the number of files in the zip archive and it hangs my app..

public class decomp extends Service {
    String datez;
    private static final String NOMEDIA_FILE = ".nomedia";
    @Override
    public void onCreate() {
        super.onCreate();

        SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        datez=pref.getString("dateval", "Date Not");
        makedir();
        decomp();
    }

    void makedir(){
        File folder = new File(Environment.getExternalStorageDirectory() + "/Android/"+"."+datez);
        boolean success = false;
        if (!folder.exists()) {
            success = folder.mkdirs();
            File file = new File(folder,NOMEDIA_FILE);
            if(!file.exists()){
                try {
                    file.createNewFile();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
        if (!success) {
        } else {

        }
    }
    void  decomp() {
        String zipFile = Environment.getExternalStorageDirectory() + "/Android/"+"."+datez+".zip"; //your zip file location
        String unzipLocation = Environment.getExternalStorageDirectory() + "/Android/"+"."+datez+"/"; // unzip location
        Decompress df= new Decompress(zipFile, unzipLocation);
        df.unzip();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");

    }

     void checkbasetable(){


     }


}

This is the decompress class

public class Decompress {

    private String _zipFile;
    private String _location;
    Context context;

    public Decompress(String zipFile, String location) {
        _zipFile = zipFile;
        _location = location;

        _dirChecker("");
    }

    public void unzip() {
        try  {
            FileInputStream fin = new FileInputStream(_zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                Log.v("Decompress", "Unzipping " + ze.getName());

                if(ze.isDirectory()) {
                    _dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(_location +  ze.getName());

                    BufferedOutputStream bufout = new BufferedOutputStream(fout);
                    byte[] buffer = new byte[1024];
                    int read = 0;
                    while ((read = zin.read(buffer)) != -1) {
                        bufout.write(buffer, 0, read);
                    }
                    bufout.close();
                    zin.closeEntry();
                    fout.close();
                }

            }
            zin.close();


            Log.d("Unzip", "Unzipping complete. path :  " +_location );

        } catch(Exception e) {
            Log.e("Decompress", "unzip", e);

            Log.d("Unzip", "Unzipping failed");
        }

    }

    private void _dirChecker(String dir) {
        File f = new File(_location + dir);

        if(!f.isDirectory()) {
            f.mkdirs();
        }
    }


}






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire