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

I am trying to directly read a zip file from a remote url I tried this way

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

I get an exception like

 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) 

Where, how does the zip file url work fine when launched from the browser.

+5
source share
4 answers

File class is not designed to work with remote files. It only supports files that are available on the local file system. To open a stream in a remote file, you can use HttpURLConnection .

Call getInputStream() on the HttpURLConnection instance to get an input stream that you can process further.

Example:

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

None of the above work for me.

What he did , working like a charm, is:

 InputStream inputStream = new URL( urlString ).openStream(); 
+2
source

With line

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

You are trying to create a file with a name similar to a URL that contains characters that are not allowed.

The file should be named simpler, for example

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

The compiler tells you that also:

(Invalid file name, directory or volume name.

I am sure that is why you get the error. But I'm not sure about the rest of the code.

+1
source

You cannot create a file from a URL, try the following:

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

File zipFile = new file ("some space on your local disk");

FileUtils.copyURLToFile (ftpUrl, zipFile);

ZipFile zip = new ZipFile (zipFile);

-2
source

Source: https://habr.com/ru/post/1235137/


All Articles