1

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?

Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30
Cravenica
  • 169
  • 10
  • I think you want to see this: http://stackoverflow.com/questions/991489/i-cant-delete-a-file-in-java – GavinF Sep 29 '16 at 10:35

2 Answers2

3

I think you should close your zipFile at the end of your routine :

zipFile.close();
Rambler
  • 4,994
  • 2
  • 20
  • 27
1

Thank you Rambler! Problem solved after closing the zipfile.

            dest.close();
            is.close();
        } 
    }
    zipfile.close();
}
Cravenica
  • 169
  • 10