如何知道 zip 文件在使用 adb 推送后是否准备好解压缩

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

我想使用 adb push myfile.zip /data/ 将 zip 文件推送到我的 android 设备,然后在我的应用程序中以编程方式将其解压缩。

有没有办法确保 myfile.zip 在我解压缩之前已经完成下载到 /data/ 中?

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

这会告诉我文件一创建就存在,但如果 zip 文件非常大,它甚至可能在下载完成之前就开始解压缩。

有什么建议吗?

根据您下载的方式,您可以获得大小,然后进行比较。

例如,

int count completed;
  try {
        URL url = new URL(this.link);
        URLConnection connection = url.openConnection();
        connection.connect();
        InputStream input = new BufferedInputStream(url.openStream(),
                8192);
        OutputStream output = new FileOutputStream(this.savedFileName);
        byte data[] = new byte[1024];
        while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
            completed+=count;
        }
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
        Log.e("Error: ", e.getMessage());
    }

在上面的代码中,总大小将表示为已完成,以字节为单位。您可以使用 File.size()

将其与解压缩之前的文件大小进行比较

同样你可以制作一个对象来封装下载项。并创建一个类似 isCompleted()

的方法

您可以使用 FileObserver to monitor the /data/ directory, listen for CLOSE_WRITE 事件并确保触发事件的文件是您感兴趣的文件:

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(); 
}