Example usage for java.util.zip ZipEntry isDirectory

List of usage examples for java.util.zip ZipEntry isDirectory

Introduction

In this page you can find the example usage for java.util.zip ZipEntry isDirectory.

Prototype

public boolean isDirectory() 

Source Link

Document

Returns true if this is a directory entry.

Usage

From source file:org.apache.batchee.cli.zip.Zips.java

public static void unzip(final File zipFile, final File destination) throws IOException {
    ZipInputStream in = null;/*from ww w  . j a  v  a 2s .  c o m*/
    try {
        mkdir(destination);

        in = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile), FILE_BUFFER_SIZE));

        ZipEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            final String path = entry.getName();
            final File file = new File(destination, path);

            if (entry.isDirectory()) {
                continue;
            }

            mkdir(file.getParentFile());

            final OutputStream out = new BufferedOutputStream(new FileOutputStream(file), FILE_BUFFER_SIZE);
            try {
                copy(in, out);
            } finally {
                IOUtils.closeQuietly(out);
            }

            final long lastModified = entry.getTime();
            if (lastModified > 0) {
                file.setLastModified(lastModified);
            }

        }
    } catch (final IOException e) {
        throw new IOException("Unable to unzip " + zipFile.getAbsolutePath(), e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:net.minecraftforge.fml.common.asm.transformers.MarkerTransformer.java

private static void processJar(File inFile, File outFile, MarkerTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;/*from  w ww.  jav  a 2 s .c o m*/
    ZipOutputStream outJar = null;

    try {
        try {
            inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open input file: " + e.getMessage());
        }

        try {
            outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open output file: " + e.getMessage());
        }

        ZipEntry entry;
        while ((entry = inJar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                outJar.putNextEntry(entry);
                continue;
            }

            byte[] data = new byte[4096];
            ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();

            int len;
            do {
                len = inJar.read(data);
                if (len > 0) {
                    entryBuffer.write(data, 0, len);
                }
            } while (len != -1);

            byte[] entryData = entryBuffer.toByteArray();

            String entryName = entry.getName();

            if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
                ClassNode cls = new ClassNode();
                ClassReader rdr = new ClassReader(entryData);
                rdr.accept(cls, 0);
                String name = cls.name.replace('/', '.').replace('\\', '.');

                for (MarkerTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }

            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        IOUtils.closeQuietly(outJar);
        IOUtils.closeQuietly(inJar);
    }
}

From source file:org.apache.xml.security.test.resource.TestVectorResolver.java

/**
 * Method init//from w w w. j  a va 2  s  . co  m
 *
 */
public static void init() {

    String thisClass = "org.apache.xml.security.test.resource.TestVectorResolver";
    String testVectorFile = "testvectors.zip";

    if (!TestVectorResolver.alreadyInitialized) {
        TestVectorResolver.alreadyInitialized = true;
        TestVectorResolver.vectors = new java.util.HashMap(30);

        try {
            zis = new java.util.zip.ZipInputStream(
                    Class.forName(thisClass).getResourceAsStream(testVectorFile));

            java.util.zip.ZipEntry ze = null;

            while ((ze = zis.getNextEntry()) != null) {
                if (!ze.isDirectory()) {
                    byte data[] = org.apache.xml.security.utils.JavaUtils.getBytesFromStream(zis);

                    TestVectorResolver.vectors.put(ze.getName(), data);
                    log.debug("Contents of " + thisClass + "/" + testVectorFile + "#" + ze.getName() + " "
                            + data.length + " bytes");
                }
            }
        } catch (java.lang.ClassNotFoundException e) {
        } catch (java.io.IOException e) {
        }
    }
}

From source file:Main.java

/**
 * kudos: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)
 * @param zipFile//  www  . j a  va2 s.  co m
 * @param destinationDirectory
 */
public static void unZip(String zipFile, String destinationDirectory) {
    try {
        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            Log.v("Decompress", "Unzipping " + ze.getName());
            String destinationPath = destinationDirectory + File.separator + ze.getName();
            if (ze.isDirectory()) {
                dirChecker(destinationPath);
            } else {
                FileOutputStream fout;
                try {
                    File outputFile = new File(destinationPath);
                    if (!outputFile.getParentFile().exists()) {
                        dirChecker(outputFile.getParentFile().getPath());
                    }
                    fout = new FileOutputStream(destinationPath);
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }
                    zin.closeEntry();
                    fout.close();
                } catch (Exception e) {
                    // ok for now.
                    Log.v("Decompress", "Error: " + e.getMessage());
                }
            }
        }
        zin.close();
    } catch (Exception e) {
        Log.e("Decompress", "unzip", e);
    }
}

From source file:de.pksoftware.springstrap.sapi.util.WebappZipper.java

/**
 * Unzip it//from  w  ww. j  a v a2s  .  c o  m
 * @param zipFile input zip file
 * @param output zip file output folder
 */
public static void unzip(String zipFile, String outputFolder) {
    logger.info("Unzipping {} into {}.", zipFile, outputFolder);

    byte[] buffer = new byte[1024];

    try {

        //get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            if (ze.isDirectory()) {
                ze = zis.getNextEntry();
                continue;
            }

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            logger.debug("Unzipping: {}", newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

        logger.debug("Unzipping {} completed.", zipFile);

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Extract a zip resource into real files and directories
 * //from  w  w w . j a  v a  2  s  .  c  o  m
 * @param in typically given as getResources().openRawResource(R.raw.something)
 * @param directory target directory
 * @param overwrite indicates whether to overwrite existing files
 * @return list of files that were unpacked (if overwrite is false, this list won't include files
 *         that existed before)
 * @throws IOException
 */
public static List<File> extractZipResource(InputStream in, File directory, boolean overwrite)
        throws IOException {
    final int BUFSIZE = 2048;
    byte buffer[] = new byte[BUFSIZE];
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(in, BUFSIZE));
    List<File> files = new ArrayList<File>();
    ZipEntry entry;
    directory.mkdirs();
    while ((entry = zin.getNextEntry()) != null) {
        File file = new File(directory, entry.getName());
        files.add(file);
        if (overwrite || !file.exists()) {
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                file.getParentFile().mkdirs(); // Necessary because some zip files lack directory entries.
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFSIZE);
                int nRead;
                while ((nRead = zin.read(buffer, 0, BUFSIZE)) > 0) {
                    bos.write(buffer, 0, nRead);
                }
                bos.flush();
                bos.close();
            }
        }
    }
    zin.close();
    return files;
}

From source file:ZipFileIO.java

/**
 * Extract zip file to destination folder.
 * //from w  ww .j  av a2s.co m
 * @param file
 *            zip file to extract
 * @param destination
 *            destinatin folder
 */
public static void extract(File file, File destination) throws IOException {
    ZipInputStream in = null;
    OutputStream out = null;
    try {
        // Open the ZIP file
        in = new ZipInputStream(new FileInputStream(file));

        // Get the first entry
        ZipEntry entry = null;

        while ((entry = in.getNextEntry()) != null) {
            String outFilename = entry.getName();

            // Open the output file
            if (entry.isDirectory()) {
                new File(destination, outFilename).mkdirs();
            } else {
                out = new FileOutputStream(new File(destination, outFilename));

                // Transfer bytes from the ZIP file to the output file
                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Close the stream
                out.close();
            }
        }
    } finally {
        // Close the stream
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:com.diffplug.gradle.ZipMisc.java

/**
 * Unzips a directory to a folder.//from ww w .  ja  va 2  s. c o  m
 *
 * @param input            a zip file
 * @param destinationDir   where the zip will be extracted to
 */
public static void unzip(File input, File destinationDir) throws IOException {
    try (ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream(new FileInputStream(input)))) {
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            File dest = new File(destinationDir, entry.getName());
            if (entry.isDirectory()) {
                FileMisc.mkdirs(dest);
            } else {
                FileMisc.mkdirs(dest.getParentFile());
                try (OutputStream output = new BufferedOutputStream(new FileOutputStream(dest))) {
                    copy(zipInput, output);
                }
            }
        }
    }
}

From source file:com.netflix.nicobar.core.utils.ClassPathUtils.java

/**
 * Get all of the directory paths in a zip/jar file
 * @param pathToJarFile location of the jarfile. can also be a zipfile
 * @return set of directory paths relative to the root of the jar
 */// ww  w  . j  a  v  a2s  . c  o m
public static Set<Path> getDirectoriesFromJar(Path pathToJarFile) throws IOException {
    Set<Path> result = new HashSet<Path>();
    ZipFile jarfile = new ZipFile(pathToJarFile.toFile());
    try {
        final Enumeration<? extends ZipEntry> entries = jarfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                result.add(Paths.get(entry.getName()));
            }
        }
        jarfile.close();
    } finally {
        IOUtils.closeQuietly(jarfile);
    }
    return result;
}

From source file:io.github.jeddict.jcode.parser.ejs.EJSUtil.java

public static void copyDynamicResource(Consumer<FileTypeStream> parserManager, String inputResource,
        FileObject webRoot, Function<String, String> pathResolver, ProgressHandler handler) throws IOException {
    InputStream inputStream = loadResource(inputResource);
    try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }/*from ww  w  .j  a  v  a 2s.  c o m*/
            boolean skipParsing = true;
            String entryName = entry.getName();
            if (entryName.endsWith(".ejs")) {
                skipParsing = false;
                entryName = entryName.substring(0, entryName.lastIndexOf("."));
            }
            String targetPath = pathResolver.apply(entryName);
            if (targetPath == null) {
                continue;
            }
            handler.progress(targetPath);
            FileObject target = org.openide.filesystems.FileUtil.createData(webRoot, targetPath);
            FileLock lock = target.lock();
            try (OutputStream outputStream = target.getOutputStream(lock);) {
                parserManager.accept(new FileTypeStream(entryName, zipInputStream, outputStream, skipParsing));
                zipInputStream.closeEntry();
            } finally {
                lock.releaseLock();
            }
        }
    } catch (Throwable ex) {
        Exceptions.printStackTrace(ex);
        System.out.println("InputResource : " + inputResource);
    }
}