Eyal Sooliman
Eyal Sooliman

Reputation: 2268

Reading remote zip file using apache.commons.compress.archivers.zip

I am trying to read a remote zip file (created with IONIC vendor) but I need to use apache.commons.compress.archivers.zip in order to achieve it with java only.

I already tried using java.util.zip ZipInputStream but without success (for regular zip file it works but not for IONIC zip files).

the code I have tried:

  public static String read(String zipPath, String partOfFileName) throws IOException {
    URL url = new URL(zipPath);
    InputStream in = new BufferedInputStream(url.openStream(), 1024);
    ZipInputStream stream = new ZipInputStream(in);
    ZipEntry entry;

    while ((entry = stream.getNextEntry()) != null) {
            if (FilenameUtils.getName(entry.getName()).contains(partOfFileName)) {
                StringBuilder out = getTxtFiles(stream);
                return out.toString();
            }
    }

    return zipPath;
}

private static StringBuilder getTxtFiles(InputStream in) {
    StringBuilder out = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line;
    try {
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out;
  }
}

Any help will be appreciated.

Upvotes: 0

Views: 1150

Answers (1)

Eyal Sooliman
Eyal Sooliman

Reputation: 2268

I solved it by using FileZip():

        ZipFile zipFile = new ZipFile(new File(zipPath));
        List<String> filesList = new ArrayList<>();
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        ZipEntry entry = null;
        
        StringBuilder out = null;
        while (entries.hasMoreElements()) {
            entry = entries.nextElement();
           
            if (FilenameUtils.getName(entry.getName()).contains(partOfFileName)) {
                out = getTxtFiles(zipFile.getInputStream(entry));
                LogUtil.logServer(out.toString());

                return out.toString();
            }
        }

Upvotes: 0

Related Questions