Java ZipEntry Read getZipEntryContent(final File zip, final String entry)

Here you can find the source of getZipEntryContent(final File zip, final String entry)

Description

Get the content of a zip entry.

License

Open Source License

Parameter

Parameter Description
zip zip.
entry entry.

Exception

Parameter Description
IOException error while reading.
InterruptedException task cancelled.

Return

its content.

Declaration

public static String getZipEntryContent(final File zip, final String entry)
        throws IOException, InterruptedException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.File;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    /**/*from  w  w w  .  j a  va  2s  .  c o  m*/
     * Get the content of a zip entry.
     * 
     * @param zip
     *            zip.
     * @param entry
     *            entry.
     * @return its content.
     * @throws IOException
     *             error while reading.
     * @throws InterruptedException
     *             task cancelled.
     */
    public static String getZipEntryContent(final File zip, final String entry)
            throws IOException, InterruptedException {
        try (ZipFile f = new ZipFile(zip)) {
            final ZipEntry e = f.getEntry(entry);
            final InputStream is = f.getInputStream(e);
            final BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            final StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                // Check for interrupt.
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException();
                }
                // Read next line.
                sb.append(line);
            }
            return sb.toString();
        }
    }
}

Related

  1. getZipEntry(ZipFile zip, final String name)
  2. getZipEntry(ZipFile zip, String name)
  3. getZipEntryAsString(File archive, String name)
  4. getZipEntryAsString(InputStream is)
  5. getZipEntryByteContent(ZipEntry ze, ZipFile zip)
  6. getZipEntryFileName(final ZipEntry zipEntry)
  7. getZipEntryName(int pathOffset, String absolutePath)
  8. getZipEntryNames(File zipFile)
  9. getZipEntryPath(File f, String archiveSourceDir)