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:com.codelanx.codelanxlib.util.Reflections.java

/**
 * Checks whether or not there is a plugin on the server with the name of
 * the passed {@code name} paramater. This method achieves this by scanning
 * the plugins folder and reading the {@code plugin.yml} files of any
 * respective jarfiles in the directory.
 * /*w w  w  .  ja  v  a  2  s. co  m*/
 * @since 0.1.0
 * @version 0.1.0
 * 
 * @param name The name of the plugin as specified in the {@code plugin.yml}
 * @return The {@link File} for the plugin jarfile, or {@code null} if not
 *         found
 */
public static File findPluginJarfile(String name) {
    File plugins = new File("plugins");
    Exceptions.illegalState(plugins.isDirectory(), "'plugins' isn't a directory! (wat)");
    for (File f : plugins.listFiles((File pathname) -> {
        return pathname.getPath().endsWith(".jar");
    })) {
        try (InputStream is = new FileInputStream(f); ZipInputStream zi = new ZipInputStream(is)) {
            ZipEntry ent = null;
            while ((ent = zi.getNextEntry()) != null) {
                if (ent.getName().equalsIgnoreCase("plugin.yml")) {
                    break;
                }
            }
            if (ent == null) {
                continue; //no plugin.yml found
            }
            ZipFile z = new ZipFile(f);
            try (InputStream fis = z.getInputStream(ent);
                    InputStreamReader fisr = new InputStreamReader(fis);
                    BufferedReader scan = new BufferedReader(fisr)) {
                String in;
                while ((in = scan.readLine()) != null) {
                    if (in.startsWith("name: ")) {
                        if (in.substring(6).equalsIgnoreCase(name)) {
                            return f;
                        }
                    }
                }
            }
        } catch (IOException ex) {
            Debugger.error(ex, "Error reading plugin jarfiles");
        }
    }
    return null;
}

From source file:com.splout.db.common.CompressorUtil.java

public static void uncompress(File file, File dest) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File entryDestination = new File(dest, entry.getName());
        entryDestination.getParentFile().mkdirs();
        InputStream in = zipFile.getInputStream(entry);
        OutputStream out = new FileOutputStream(entryDestination);
        IOUtils.copy(in, out);//from  w  ww . j  av  a 2 s  .c  o  m
        in.close();
        out.close();
    }
}

From source file:net.bpelunit.util.ZipUtil.java

public static void unzipFile(File zip, File dir) throws IOException {
    InputStream in = null;//w  w w.j  ava 2  s .  c o  m
    OutputStream out = null;
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.getName().endsWith("/")) {
            File unzippedFile = new File(dir, entry.getName());
            try {
                in = zipFile.getInputStream(entry);
                unzippedFile.getParentFile().mkdirs();

                out = new FileOutputStream(unzippedFile);

                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
    }
}

From source file:local.edg.ClassDB.java

/**
 * Generates a data base with class informations about a given application
 * //from ww w.jav  a2s  . co m
 * @param appClasses
 *            The classes of the application as directories to class-files or as path to jar-files.
 *            
 * @return Data base with class information.
 */
public static Map<String, Class> create(String[] appClasses) {
    ClassDbVisitor cv = new ClassDbVisitor();
    for (String s : appClasses) {
        File f = new File(s);
        if (f.isDirectory()) {
            Collection<File> files = FileUtils.listFiles(f, new String[] { "class" }, true);
            for (File cf : files) {
                try {
                    ClassReader cr = new ClassReader(new FileInputStream(cf));
                    cr.accept(cv, 0);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else if (f.isFile() && s.toLowerCase().endsWith(".jar")) {
            try {
                ZipFile zf = new ZipFile(f);
                Enumeration<? extends ZipEntry> en = zf.entries();
                while (en.hasMoreElements()) {
                    ZipEntry e = en.nextElement();
                    String name = e.getName();
                    if (name.endsWith(".class")) {
                        new ClassReader(zf.getInputStream(e)).accept(cv, 0);
                    }
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return cv.getClassDb();
}

From source file:edu.umd.cs.guitar.testcase.plugin.edg.ClassDB.java

/**
 * Generates a database containing class information about given application classes
 * @param appClasses The classes of an application as directories to class-files or as path to jar-files
 * @return Database containing class information
 *//*  www .java  2  s  .c  o m*/
public static Map<String, Class> create(String[] appClasses) {
    ClassDBVisitor cv = new ClassDBVisitor();
    for (String s : appClasses) {
        File f = new File(s);
        if (f.isDirectory()) {
            Collection<File> files = FileUtils.listFiles(f, new String[] { "class" }, true);
            for (File cf : files) {
                try {
                    ClassReader cr = new ClassReader(new FileInputStream(cf));
                    cr.accept(cv, 0);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else if (f.isFile() && s.toLowerCase().endsWith(".jar")) {
            try {
                ZipFile zf = new ZipFile(f);
                Enumeration<? extends ZipEntry> en = zf.entries();
                while (en.hasMoreElements()) {
                    ZipEntry e = en.nextElement();
                    String name = e.getName();
                    if (name.endsWith(".class")) {
                        new ClassReader(zf.getInputStream(e)).accept(cv, 0);
                    }
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return cv.getClassDb();
}

From source file:com.opoopress.maven.plugins.plugin.zip.ZipUtils.java

public static void unzipFileToDirectory(File file, File destDir, boolean keepTimestamp) throws IOException {
    // create output directory if it doesn't exist
    if (!destDir.exists())
        destDir.mkdirs();/*  ww  w. j  av  a 2  s.c o  m*/

    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            new File(destDir, entry.getName()).mkdirs();
            continue;
        }

        InputStream inputStream = zipFile.getInputStream(entry);
        File destFile = new File(destDir, entry.getName());

        System.out.println("Unzipping to " + destFile.getAbsolutePath());
        FileUtils.copyInputStreamToFile(inputStream, destFile);
        IOUtils.closeQuietly(inputStream);

        if (keepTimestamp) {
            long time = entry.getTime();
            if (time > 0) {
                destFile.setLastModified(time);
            }
        }
    }

    zipFile.close();
}

From source file:org.apereo.portal.utils.ZipUtils.java

public static void extract(File archive, File outputDir) throws IOException {
    final ZipFile zipfile = new ZipFile(archive);
    for (final Enumeration<? extends ZipEntry> e = zipfile.entries(); e.hasMoreElements();) {
        final ZipEntry entry = e.nextElement();

        final File outputFile = checkDirectories(entry, outputDir);
        if (outputFile != null) {
            final InputStream is = zipfile.getInputStream(entry);
            try {
                writeFile(is, outputFile);
            } finally {
                is.close();//  w  w w  . ja v  a 2  s .com
            }
        }
    }
}

From source file:com.vamonossoftware.core.ZipUtil.java

public static int unzip(File zip, File dest) {
    try {//  w w w . ja  va  2  s  .c om
        ZipFile zf = new ZipFile(zip);
        int count = 0;
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            File destfile = new File(dest, entry.getName());
            if (entry.isDirectory()) {
                destfile.mkdirs();
            } else {
                IOUtils.copy(zf.getInputStream(entry), new FileOutputStream(destfile));
            }
        }
        return count;
    } catch (IOException e) {
        return -1;
    }
}

From source file:com.aliyun.odps.local.common.utils.ArchiveUtils.java

/**
 * Given a File input it will unzip the file in a the unzip directory passed
 * as the second parameter/*from   ww w  .  j a  va 2 s.  co m*/
 * 
 * @param inFile
 *          The zip file as input
 * @param unzipDir
 *          The unzip directory where to unzip the zip file.
 * @throws IOException
 */
public static void unZip(File inFile, File unzipDir) throws IOException {
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile = new ZipFile(inFile);

    try {
        entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = zipFile.getInputStream(entry);
                try {
                    File file = new File(unzipDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        zipFile.close();
    }
}

From source file:org.apache.oozie.tools.OozieDBImportCLI.java

private static <E> int importFromJSONtoDB(EntityManager entityManager, ZipFile zipFile, String filename,
        Class<E> clazz) throws JPAExecutorException, IOException {
    int wfjSize = 0;
    Gson gson = new Gson();
    ZipEntry entry = zipFile.getEntry(filename);
    if (entry != null) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(zipFile.getInputStream(entry), "UTF-8"));
        String line;/*from  w w  w  .j  a  v  a2  s .c o m*/
        while ((line = reader.readLine()) != null) {
            E workflow = gson.fromJson(line, clazz);
            entityManager.persist(workflow);
            wfjSize++;
        }
        reader.close();
    }
    return wfjSize;
}