如何从远程URL读取zip文件而不提取它

Paw*_*wan 8 java

我试图直接读取我尝试过这种方式的远程URL中的zip文件

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Utils {
    public static void main(String args[]) throws Exception {
        String ftpUrl = "http://wwwccc.zip";
        URL url = new URL(ftpUrl);
        unpackArchive(url);
    }

    public static void unpackArchive(URL url) throws IOException {
        String ftpUrl = "http://www.vvvv.xip";
        File zipFile = new File(url.toString());
        ZipFile zip = new ZipFile(zipFile);
        InputStream in = new BufferedInputStream(url.openStream(), 1024);
        ZipInputStream zis = new ZipInputStream(in);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("entry: " + entry.getName() + ", "
                    + entry.getSize());
            BufferedReader bufferedeReader = new BufferedReader(
                    new InputStreamReader(zip.getInputStream(entry)));
            String line = bufferedeReader.readLine();
            while (line != null) {
                System.out.println(line);
                line = bufferedeReader.readLine();
            }
            bufferedeReader.close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的是Exception

Exception in thread "main" java.io.FileNotFoundException: http:\www.nseindia.com\content\historical\EQUITIES\2015\NOV\cm03NOV2015bhav.csv.zip (The filename, directory name, or volume label syntax is incorrect)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(Unknown Source)
    at java.util.zip.ZipFile.<init>(Unknown Source)
    at java.util.zip.ZipFile.<init>(Unknown Source)
    at Utils.unpackArchive(Utils.java:30)
    at Utils.main(Utils.java:19)
Run Code Online (Sandbox Code Playgroud)

从浏览器运行时,zip文件的URL工作正常.

dav*_* a. 3

File类并非设计用于处理远程文件。它仅支持本地文件系统上可用的文件。要打开远程文件上的流,您可以使用HttpURLConnection.

调用getInputStream()实例HttpURLConnection以获取可以进一步处理的输入流。

例子:

String url= "http://www.nseindia.com/content/historical/EQUITIES/2015/NOV/cm03NOV2015bhav.csv.zip";
InputStream is = new URL(url).openConnection().getInputStream();
Run Code Online (Sandbox Code Playgroud)