I have an issue with Java application which downloads and extracts the files.
Here's the code:
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();
}
}
}
The thing is after this I want to delete the files but they are still in use and I can not perform a delete function. I can't find the issue. Can I get some help with this?