收录日期:2021/02/24 14:25:08 时间:2016/07/23 16:27:33 标签:Java SE
我写了一个解压zip的文件,想解压完以后删除zip文件,可以每次都删不掉,为什么?
public void zipPic(String path,String name)
{
String unzipfile =path+"\\"+name; //解压缩的文件名
try
{
File olddirec = new File(unzipfile); //解压缩的文件路径(为了获取路径)
ZipInputStream zin = new ZipInputStream(new FileInputStream(unzipfile));
ZipEntry entry;
//创建文件夹
while ( (entry = zin.getNextEntry()) != null)
{
if (entry.isDirectory())
{
File directory = new File(olddirec.getParent(), entry.getName());
if (!directory.exists())
if (!directory.mkdirs())
System.exit(0);
zin.closeEntry();
}
if (!entry.isDirectory())
{
File myFile = new File(entry.getName());
FileOutputStream fout = new FileOutputStream(path+"\\"+myFile.getPath());
DataOutputStream dout = new DataOutputStream(fout);
byte[] b = new byte[1024];
int len = 0;
while ( (len = zin.read(b)) != -1) {
dout.write(b, 0, len);
}
dout.close();
fout.close();
zin.closeEntry();
}
}
zin.closeEntry();
olddirec.delete();
}
catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
}