Search code examples
androidfileunzip

How to know if the zip file is ready to be unzipped after pushing it with adb


I want to push a zip file to my android device using adb push myfile.zip /data/ and then to programmatically unzip it in my app.

Is there a way to make sure myfile.zip has finished downloading into /data/ before I unzip it?

File file = new File("/data/myfile.zip");               
if(file.exists())
    unzip();

This will show me that the file exists as soon as it is created but if the zip file is very large it might start unzipping even before it finished downloading.

Any suggestions?


Solution

  • You can use a FileObserver to monitor the /data/ directory, listen for CLOSE_WRITE events and ensure that the file triggering the event is the one you are interested in :

    FileObserver dataDirObserver = new FileObserver("/data/", FileObserver.CLOSE_WRITE){
        public void onEvent(int event, String path){
            if("/data/myfile.zip".equals(path)){
                 unzip();
            }
        }
    };
    
    
    //don't forget to start/stop watching at some point.
    public void onPause(){
       super.onPause();
       dataDirObserver.stopWatching(); 
    }
    public void onResume(){
       super.onPause();
       dataDirObserver.startWatching(); 
    }