Java ZipEntry Read getZipEntryAsString(File archive, String name)

Here you can find the source of getZipEntryAsString(File archive, String name)

Description

get Zip Entry As String

License

Open Source License

Declaration

public static String getZipEntryAsString(File archive, String name) throws IOException 

Method Source Code


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

import java.io.BufferedInputStream;

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

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

import java.util.ArrayList;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static String getZipEntryAsString(File archive, String name) throws IOException {
        FileInputStream fileIn = null;
        try {/*from   www .  j  a va 2  s  . c o  m*/
            fileIn = new FileInputStream(archive);
            BufferedInputStream buffered = new BufferedInputStream(fileIn);
            ZipInputStream zip = new ZipInputStream(buffered);
            ArrayList<String> entries = new ArrayList<String>();
            ZipEntry next = zip.getNextEntry();
            while (next != null) {
                if (name.equals(next.getName())) {
                    return asString(zip);
                }
                entries.add(next.getName());
                next = zip.getNextEntry();
            }
            zip.close();
            return null;
        } finally {
            if (fileIn != null) {
                fileIn.close();
            }
        }
    }

    public static String asString(InputStream inputStream) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder buffer = new StringBuilder();
        try {
            String line = in.readLine();
            while (line != null) {
                buffer.append(line);
                buffer.append("\n");
                line = in.readLine();
            }
        } finally {
            in.close();
        }

        return buffer.toString();
    }
}

Related

  1. getZipEntry(String zipEntryName)
  2. getZipEntry(String zipFileLoc)
  3. getZipEntry(ZipFile jarFile, String path)
  4. getZipEntry(ZipFile zip, final String name)
  5. getZipEntry(ZipFile zip, String name)
  6. getZipEntryAsString(InputStream is)
  7. getZipEntryByteContent(ZipEntry ze, ZipFile zip)
  8. getZipEntryContent(final File zip, final String entry)
  9. getZipEntryFileName(final ZipEntry zipEntry)