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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5216533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:00:57  来源:igfitidea点击:

Delete a zip file after unzip in java

javadelete-file

提问by vnshetty

How to delete a zip file in java? file.delete method returns false. Why?

如何在java中删除zip文件?file.delete 方法返回 false。为什么?

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


    }

} 

回答by joe

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

回答by Jon Skeet

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, e.g. what does file.exists()return?
  • 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 (e.g. 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
  • 检查您的路径是否正确,例如file.exists()返回什么?
  • 检查您是否有权以运行您的应用程序的用户身份删除该文件
  • 检查您的代码中是否没有打开文件句柄(例如,您是否只是从中读取数据而没有关闭输入流?)
  • 检查您没有在桌面应用程序中打开该文件

回答by Dan Fabulich

This is very common when attempting to delete a file that you've created. Be sure to closethe 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()哪个应该成功,即使您不小心打开了几个文件句柄。

回答by Keita

Use : FileUtils.delete(yourFile);

利用 : FileUtils.delete(yourFile);

This corrected my problem

这纠正了我的问题