简体   繁体   English

在java中解压缩后删除一个zip文件

[英]Delete a zip file after unzip in java

How to delete a zip file in java? 如何删除java中的zip文件? file.delete method returns false. file.delete方法返回false。 Why? 为什么?

File file = new File("/mibook/"+mFilename+"/"+mZipname.toString());
boolean deleted = file.delete();

edit: 编辑:
Rule "Directory should empty before deletion.." does it apply for zip file? 规则“目录应该在删除前清空..”它是否适用于zip文件?

My file unzipping code 我的文件解压缩代码


   public void unzip() throws IOException { 
        FileInputStream fin=null;
        ZipInputStream zin=null;
        File file =null;
        ZipEntry ze ;
        FileOutputStream fout=null;
        try{ 
            System.out.println(_zipFile );
            System.out.println(_location);
            fin = new FileInputStream(_zipFile); 
            zin = new ZipInputStream(fin); 
            ze= null; 
            byte[] buffer = new byte[1024];
            int length;
            while ((ze = zin.getNextEntry()) != null) { 
                file = new File((_location +"/" + ze.getName()));
                file.getParentFile().mkdirs();
                 fout= new FileOutputStream(_location + ze.getName()); 
                while ((length = zin.read(buffer))>0) {
                    fout.write(buffer, 0, length);
                }
                zin.closeEntry(); 
                fout.close();
} zin.close(); }catch(Exception e) { Log.e("Decompress", "unzip", e); }
finally {

            fin.close();
            zin.close();
            fout.close();


    }

} 

You have to make sure you close your ZipFile. 你必须确保关闭ZipFile。

For example I had: 比如我有:

ZipFile zFile = new ZipFile("blah");
//lots-o-code
zFile.close();
File file = new File("blah");
file.delete();

If file.delete() returns false, then my guess is that another process has the zip file open - or possibly even your own process. 如果file.delete()返回false,那么我的猜测是另一个进程打开了zip文件 - 甚至可能是你自己的进程。

  • Check that you've got the path correct, eg what does file.exists() return? 检查你的路径是否正确,例如file.exists()返回什么?
  • Check that you've got permission to delete the file as the user running your application 检查您是否有权以运行应用程序的用户身份删除该文件
  • Check that you haven't got an open handle to the file within your code (eg have you just read from it and not closed the input stream?) 检查您的代码中是否没有打开文件的句柄(例如,您是否只是从中读取而未关闭输入流?)
  • Check that you don't have the file opened in a desktop app 检查您是否在桌面应用中打开了该文件

Use : FileUtils.delete(yourFile); 使用: FileUtils.delete(yourFile);

This corrected my problem 这纠正了我的问题

This is very common when attempting to delete a file that you've created. 这在尝试删除您创建的文件时非常常见。 Be sure to close the FileWriter you used to create the unzipped file. 请务必close用于创建解压缩文件的FileWriter。

If you can't figure out where to close the file, your best bet may be to call file.deleteOnExit() which should succeed even if you accidentally leave a few file handles open. 如果你无法弄清楚文件的关闭位置,最好的办法就是调用file.deleteOnExit() ,即使你不小心打开了几个文件句柄也应该成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM