Example usage for java.util.zip ZipFile getInputStream

List of usage examples for java.util.zip ZipFile getInputStream

Introduction

In this page you can find the example usage for java.util.zip ZipFile getInputStream.

Prototype

public InputStream getInputStream(ZipEntry entry) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:org.eclipse.che.plugin.gwt.Utils.java

/**
 * Reads content of the file from ZIP archive.
 *
 * @param zipFile ZIP file//from   ww  w.  ja v  a2  s. c o m
 * @param path path of the file to read content
 * @return content of the file with the given path
 * @throws IOException if error occurs while reading
 * @throws IllegalArgumentException if file not found in ZIP archive
 */
public static String getFileContent(ZipFile zipFile, String path) throws IOException {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        if (path.equals(entry.getName())) {
            try (InputStream in = zipFile.getInputStream(entry)) {
                byte[] bytes = IOUtils.toByteArray(in);

                return new String(bytes);
            }
        }
    }

    throw new IllegalArgumentException(format("Cannot find file '%s' in '%s'", path, zipFile.getName()));
}

From source file:net.fabricmc.installer.installer.MultiMCInstaller.java

public static void install(File mcDir, String version, IInstallerProgress progress) throws Exception {
    File instancesDir = new File(mcDir, "instances");
    if (!instancesDir.exists()) {
        throw new FileNotFoundException(Translator.getString("install.multimc.notFound"));
    }/*from ww w.  j  av  a 2 s .  c  om*/
    progress.updateProgress(Translator.getString("install.multimc.findInstances"), 10);
    String mcVer = version.split("-")[0];
    List<File> validInstances = new ArrayList<>();
    for (File instanceDir : instancesDir.listFiles()) {
        if (instanceDir.isDirectory()) {
            if (isValidInstance(instanceDir, mcVer)) {
                validInstances.add(instanceDir);
            }
        }
    }
    if (validInstances.isEmpty()) {
        throw new Exception(Translator.getString("install.multimc.noInstances").replace("[MCVER]", mcVer));
    }
    List<String> instanceNames = new ArrayList<>();
    for (File instance : validInstances) {
        instanceNames.add(instance.getName());
    }
    String instanceName = (String) JOptionPane.showInputDialog(null,
            Translator.getString("install.multimc.selectInstance"),
            Translator.getString("install.multimc.selectInstance"), JOptionPane.QUESTION_MESSAGE, null,
            instanceNames.toArray(), instanceNames.get(0));
    if (instanceName == null) {
        progress.updateProgress(Translator.getString("install.multimc.canceled"), 100);
        return;
    }
    progress.updateProgress(
            Translator.getString("install.multimc.installingInto").replace("[NAME]", instanceName), 25);
    File instnaceDir = null;
    for (File instance : validInstances) {
        if (instance.getName().equals(instanceName)) {
            instnaceDir = instance;
        }
    }
    if (instnaceDir == null) {
        throw new FileNotFoundException("Could not find " + instanceName);
    }
    File patchesDir = new File(instnaceDir, "patches");
    if (!patchesDir.exists()) {
        patchesDir.mkdir();
    }
    File fabricJar = new File(patchesDir, "Fabric-" + version + ".jar");
    if (!fabricJar.exists()) {
        progress.updateProgress(Translator.getString("install.client.downloadFabric"), 30);
        FileUtils.copyURLToFile(new URL("http://maven.modmuss50.me/net/fabricmc/fabric-base/" + version
                + "/fabric-base-" + version + ".jar"), fabricJar);
    }
    progress.updateProgress(Translator.getString("install.multimc.createJson"), 70);
    File fabricJson = new File(patchesDir, "fabric.json");
    if (fabricJson.exists()) {
        fabricJson.delete();
    }
    String json = readBaseJson();
    json = json.replaceAll("%VERSION%", version);

    ZipFile fabricZip = new ZipFile(fabricJar);
    ZipEntry dependenciesEntry = fabricZip.getEntry("dependencies.json");
    String fabricDeps = IOUtils.toString(fabricZip.getInputStream(dependenciesEntry), Charset.defaultCharset());
    json = json.replace("%DEPS%", stripDepsJson(fabricDeps.replace("\n", "")));
    FileUtils.writeStringToFile(fabricJson, json, Charset.defaultCharset());
    fabricZip.close();
    progress.updateProgress(Translator.getString("install.success"), 100);
}

From source file:Zip.java

/**
 * Create a InputStream on a given file, by looking for a zip archive whose
 * path is the file path with ".zip" appended, and by reading the first
 * entry in this zip file.//  ww w.j a v  a  2  s  .  c  om
 *
 * @param file the file (with no zip extension)
 *
 * @return a InputStream on the zip entry
 */
public static InputStream createInputStream(File file) {
    try {
        String path = file.getCanonicalPath();

        //ZipFile zf = new ZipFile(path + ".zip");
        ZipFile zf = new ZipFile(path);

        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();

            return zf.getInputStream(entry);
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (IOException ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:net.technicpack.utilslib.ZipUtils.java

private static void unzipEntry(ZipFile zipFile, ZipEntry entry, File outputFile)
        throws IOException, InterruptedException {
    byte[] buffer = new byte[2048];
    BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {// w  w  w .  j a va 2s.co m
        int length;
        while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } catch (ClosedByInterruptException ex) {
        throw new InterruptedException();
    } finally {
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:Zip.java

/**
 * Create a Reader on a given file, by looking for a zip archive whose path
 * is the file path with ".zip" appended, and by reading the first entry in
 * this zip file.//from  w w w  .j av a 2  s. c om
 *
 * @param file the file (with no zip extension)
 *
 * @return a reader on the zip entry
 */
public static Reader createReader(File file) {
    try {
        String path = file.getCanonicalPath();

        ZipFile zf = new ZipFile(path + ".zip");

        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream is = zf.getInputStream(entry);

            return new InputStreamReader(is);
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (IOException ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:net.technicpack.launchercore.util.ZipUtils.java

private static void unzipEntry(ZipFile zipFile, ZipEntry entry, File outputFile) throws IOException {
    byte[] buffer = new byte[2048];
    BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {// w w w . j a  v  a2s.  c om
        int length;
        while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:ZipFileUtil.java

/**
 * @param zipFile//from  w  w w .  j a  v  a2 s .c  o  m
 * @param jiniHomeParentDir
 */
public static void unzipFileIntoDirectory(ZipFile zipFile, File jiniHomeParentDir) {
    Enumeration files = zipFile.entries();
    File f = null;
    FileOutputStream fos = null;

    while (files.hasMoreElements()) {
        try {
            ZipEntry entry = (ZipEntry) files.nextElement();
            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            f = new File(jiniHomeParentDir.getAbsolutePath() + File.separator + entry.getName());

            if (entry.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }

            fos = new FileOutputStream(f);

            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            continue;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

From source file:org.eclipse.jubula.tools.internal.utils.ZipUtil.java

/**
 * //w  w w.java 2  s.  c om
 * @param archive The zip file from which to extract.
 * @param targetFile The file to which the entry contents will be extracted.
 * @param entry The entry to extract.
 * @throws IOException
 */
private static void unzipFile(ZipFile archive, File targetFile, ZipEntry entry) throws IOException {
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = archive.getInputStream(entry);
        out = new BufferedOutputStream(new FileOutputStream(targetFile));

        byte[] buffer = new byte[8192];
        int read;

        while (-1 != (read = in.read(buffer))) {
            out.write(buffer, 0, read);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:io.inkstand.scribble.rules.ZipAssert.java

/**
 * Verifies the zip file contains an entry with the specified character content
 * @param zf//from w ww.ja  v a 2  s. co m
 *  the zip file to be searched
 * @param entryPath
 *  the path to the entry to be verified, the path must not start with a '/'
 * @param expectedContent
 *  the content as a string that is expected to be the content of the file.
 * @throws java.io.IOException
 */
public static void assertZipContent(final ZipFile zf, final String entryPath, final String expectedContent)
        throws IOException {

    final ZipEntry entry = zf.getEntry(entryPath);
    assertNotNull("Entry " + entryPath + " does not exist", entry);
    assertFalse("Entry " + entryPath + " is a directory", entry.isDirectory());
    try (InputStream is = zf.getInputStream(entry)) {
        assertNotNull("Entry " + entryPath + " has no content", is);
        final String actualContent = IOUtils.toString(is);
        assertEquals(expectedContent, actualContent);
    }
}

From source file:org.docx4j.template.utils.WmlZipUtils.java

public static void unzip(File sourceFile, File outputDir) throws ZipException, IOException {
    FileUtils.deleteDirectory(outputDir);
    ZipFile zipFile = new ZipFile(sourceFile);
    Enumeration<?> files = zipFile.entries();
    File f = null;/*from w w  w. j  a  v a  2 s  .  co m*/
    FileOutputStream fos = null;
    while (files.hasMoreElements()) {
        try {
            ZipEntry entry = (ZipEntry) files.nextElement();
            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[BUFFER];
            int bytesRead = 0;
            f = new File(outputDir.getAbsolutePath() + File.separator + entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }
            fos = new FileOutputStream(f);
            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}