繁体   English   中英

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

[英]How to read a zip file from a remote URL without extracting it

我正在尝试从我尝试过的远程 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();
        }
    }
}

我收到异常作为

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)

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

File类不是为处理远程文件而设计的。 它仅支持在本地文件系统上可用的文件。 要在远程文件上打开流,您可以使用HttpURLConnection

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

例子:

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

以上都不适合我。

没有什么像一个魅力的工作,是这样的:

InputStream inputStream = new URL( urlString ).openStream();

随着线

File zipFile = new File(url.toString());

您正在尝试创建一个名为 URL 的文件,其中包含不允许的字符。

该文件应命名为更简单,如

File zipFile = new File("zipfile.csv.zip");

编译器也告诉你:

(文件名、目录名或卷标语法不正确)

我确定这就是您收到错误的原因。 但我不确定其余的代码。

读取存储在远程位置的 Zip 文件。只需要在下面的代码库中更改您的远程位置 zip 文件的详细信息。

import java.io.*;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

  public class ReadZipFileFromRemote{
        public static void main(String args[]){
           String url="https://test-po.s3.ap-south-1.amazonaws.com/dev/coding/76/55/14/1/1587736321256.zip";
           String content=readZipFileFromRemote(url);
           System.out.println(content);  
        }  

        public String readZipFileFromRemote(String remoteFileUrl) {        

           StringBuilder sb = new StringBuilder();
         try {
             URL url = new URL(remoteFileUrl);
             InputStream in = new BufferedInputStream(url.openStream(), 1024);
             ZipInputStream stream = new ZipInputStream(in);
             byte[] buffer = new byte[1024];
             ZipEntry entry;
             while ((entry = stream.getNextEntry()) != null) {
                  int read;
                 while ((read = stream.read(buffer, 0, 1024)) >= 0) {
                      sb.append(new String(buffer, 0, read));
                }
            }
     } catch (Exception e) {
        e.printStackTrace();
     }
    return sb.toString();
  }

}

你不能像这样从 url 创建文件试试这个:

URL ftpUrl = 新 URL(" http://www.nseindia.com/content/historical/EQUITIES >/2015/NOV/cm03NOV2015bhav.csv.zip");

File zipFile = new File("本地驱动器上的某个位置");

FileUtils.copyURLToFile(ftpUrl, zipFile);

ZipFile zip = 新的 ZipFile(zipFile);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM