Search code examples
javaapacheftpapache-commons-net

Enumerating FTPFile from FTP using ZipFile


My usual approach without an FTP to read ZipFiles was the following:

private void getLogFromZip(File logZip){
  ZipFile zf = new ZipFile(logZip);
  Enumeration<?> entries = zf.entries();
  while (entries.hasMoreElements()) {
    ZipEntry ze = (ZipEntry) entries.nextElement();
  //do something with entry    
}

Now that I have a connection to an FTP server, retrieving an FTPFile and using it makes things difficult:

private void getLogFromZip(FTPFile logZip){
  ZipFile zf = new ZipFile(logZip.getName()); //here's the problem
  Enumeration<?> entries = zf.entries();
  while (entries.hasMoreElements()) {
    ZipEntry ze = (ZipEntry) entries.nextElement();
  //do something with entry    
}

Directly in line 1 I am getting this:

java.io.FileNotFoundException: logger_150510_092333.zip (System cannot find the file? specified)

What is a workaround for that? How can I specify a path so that it knows where to look for the zip?

Many thanks in advance!


Solution

  • You probably have to get the file from the FTP server first, before you can access it. The FTPFile instance appears to be only a link to the actual file.

    Have a look here for an example: http://www.mysamplecode.com/2012/03/apache-commons-ftpclient-java-example_16.html