Reputation: 32321
I am trying to directly read a zip file from a Remote URL I have 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 am getting Exception as
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 as the URL of zip file is working fine when running from a browser .
Upvotes: 9
Views: 13371
Reputation: 331
Reading Zip file stored on remote Location.Just need to change your remote location zip file Details in below code base.
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();
}
}
Upvotes: 3
Reputation: 5291
File
class is not designed to work with remote files. It only supports files that are available on a local file system. To open a stream on a remote file, you can use HttpURLConnection
.
Call getInputStream()
on an 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();
Upvotes: 3
Reputation: 61986
None of the above has worked for me.
What did work like a charm, is this:
InputStream inputStream = new URL( urlString ).openStream();
Upvotes: 2
Reputation: 9
you cant create file from url like that try this :
URL ftpUrl = new URL("http://www.nseindia.com/content/historical/EQUITIES>/2015/NOV/cm03NOV2015bhav.csv.zip");
File zipFile = new File("some location on your local drive");
FileUtils.copyURLToFile(ftpUrl, zipFile);
ZipFile zip = new ZipFile(zipFile);
Upvotes: -3
Reputation: 524
With the line
File zipFile = new File(url.toString());
you are trying to create a file named like the URL which contains characters that are not allowed.
The file should be named simpler like
File zipFile = new File("zipfile.csv.zip");
The compiler is telling you that aswell:
(The filename, directory name, or volume label syntax is incorrect)
I am sure this is why you are getting the error. But i'm not certain about the rest of the code.
Upvotes: 1