如何删除java中的zip文件?file.delete方法返回false.为什么?
File file = new File("/mibook/"+mFilename+"/"+mZipname.toString());
boolean deleted = file.delete();
Run Code Online (Sandbox Code Playgroud)
编辑:
规则"目录应该在删除前清空.."它是否适用于zip文件?
我的文件解压缩代码
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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
你必须确保关闭ZipFile.
比如我有:
ZipFile zFile = new ZipFile("blah");
//lots-o-code
zFile.close();
File file = new File("blah");
file.delete();
Run Code Online (Sandbox Code Playgroud)
如果file.delete()返回false,那么我的猜测是另一个进程打开了zip文件 - 甚至可能是你自己的进程.
file.exists()返回什么?