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.
Asked
Active
Viewed 986 times
0
-
1EOF is EOF. It's the same for all streams, -1. – MadConan Jun 27 '15 at 12:22
-
@MadConan is there any other way i can find out the eof. In case of the file gets truncated or only half of the file is downloaded. – wandermonk Jun 27 '15 at 12:32
-
You can read in the file and check for exceptions (indicating a corrupted file), or you can use some sort of hashing mechanism. That's about it. – Samuel O'Malley Jun 27 '15 at 12:41
-
@Samuel O'Malley thanks a lot :) – wandermonk Jun 27 '15 at 12:47
1 Answers
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