0

I am trying to download a ZIP file from a http url connection. How do i confirm if the zip file is completely downloaded. Is there anyway i can get the end of zip file.

wandermonk
  • 6,856
  • 6
  • 43
  • 93

1 Answers1

0

EOF will reach when stream return -1 (as mentioned by MadConan also). You need to read the data until

inputStream.read == -1

A simple example shown below:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class ZipFileDownloader {

    public static void downloadZipFile(String urlPath, String saveTo) {
        try {
            URLConnection connection = new URL(urlPath).openConnection();
            InputStream in = connection.getInputStream();
            FileOutputStream out = new FileOutputStream(saveTo + "myFile.zip");
            byte[] b = new byte[1024];
            int count;
            while ((count = in.read(b)) > 0) {
                out.write(b, 0, count);
            }
            out.flush(); 
            out.close(); 
            in.close();                   
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27