我的Java应用程序存在问题,它会下载并提取文件。
以下是代码:
private void downloadFile(String link) throws MalformedURLException, IOException{
URL url = new URL(link);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
long max = conn.getContentLength();
ActivityOutput.setText(ActivityOutput.getText()+"\nUpdate size(compressed): "+max+" bytes");
BufferedOutputStream fOut = new BufferedOutputStream(new FileOutputStream(new File(getCWDir()+"\\update.zip")));
byte[] buffer = new byte[32768];
int bytesRead = 0;
int in = 0;
while((bytesRead = is.read(buffer)) != -1){
in += bytesRead;
fOut.write(buffer, 0, bytesRead);
}
fOut.flush();
fOut.close();
is.close();
ActivityOutput.setText(ActivityOutput.getText()+"\nDownload Compete!");
}
private void unzip() throws IOException{
File tempLoc = new File(getCWDir()+"\\update");
if(!tempLoc.exists()){
tempLoc.mkdir();
}
int BUFFER = 2048;
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipFile zipfile = new ZipFile(getCWDir()+"\\update.zip");
Enumeration e = zipfile.entries();
new File(getCWDir()+"\\update\\").mkdir();
while(e.hasMoreElements()){
ZipEntry entry = (ZipEntry)e.nextElement();
ActivityOutput.setText(ActivityOutput.getText()+"\nExtracting: "+entry);
if(entry.isDirectory()){
new File(getCWDir()+"\\update\\"+entry.getName()).mkdir();
} else {
new File(getCWDir()+"\\update\\"+entry.getName()).createNewFile();
is = new BufferedInputStream(zipfile.getInputStream(entry));
byte[] data = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(getCWDir()+"\\update\\"+entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
int count;
while((count = is.read(data, 0, BUFFER)) != -1){
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
}
}
}
事情是在此之后我想删除文件,但它们仍在使用中,我无法执行删除功能。我无法找到问题。我可以帮忙解决这个问题吗?
答案 0 :(得分:3)
我认为你应该在日常工作结束时关闭你的zipFile:
zipFile.close();
答案 1 :(得分:1)
谢谢Rambler! 关闭zipfile后问题解决了。
dest.close();
is.close();
}
}
zipfile.close();
}