如何在java中找到zip文件的文件结尾(EOF)



我正试图从http url连接下载一个ZIP文件。如何确认zip文件是否已完全下载。不管怎样,我能得到zip文件的末尾吗。

EOF将在流返回-1时到达(正如MadConan所提到的)。你需要读取数据直到

inputStream.read==-1

下面是一个简单的例子:

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();
        }
    }
}

最新更新