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:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {/*www  .jav  a 2  s  .  c  o m*/
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * @param zipfile Input .zip file/*from www  .  j  a  va2 s.c  om*/
 * @param outdir Output directory
 */
public static void extract(InputStream zipfile, File outdir) {
    try {
        ZipInputStream zin = new ZipInputStream(zipfile);
        ZipEntry entry;
        String name, dir;
        Log.i("OF", "uncompressinggggg ");
        while ((entry = zin.getNextEntry()) != null) {
            name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(outdir, name);
                continue;
            }
            dir = dirpart(name);
            if (dir != null)
                mkdirs(outdir, dir);

            extractFile(zin, outdir, name);
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.asterix.test.common.TestHelper.java

public static void unzip(String sourceFile, String outputDir) throws IOException {
    if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
        try (ZipFile zipFile = new ZipFile(sourceFile)) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File entryDestination = new File(outputDir, entry.getName());
                if (!entry.isDirectory()) {
                    entryDestination.getParentFile().mkdirs();
                    try (InputStream in = zipFile.getInputStream(entry);
                            OutputStream out = new FileOutputStream(entryDestination)) {
                        IOUtils.copy(in, out);
                    }/*  w  ww  .ja va  2  s.c  o m*/
                }
            }
        }
    } else {
        Process process = new ProcessBuilder("unzip", "-d", outputDir, sourceFile).start();
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IOException(e);
        }
    }
}

From source file:org.apache.geode.management.internal.configuration.utils.ZipUtils.java

public static void unzip(String zipFilePath, String outputDirectoryPath) throws IOException {
    ZipFile zipFile = new ZipFile(zipFilePath);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> zipEntries = (Enumeration<ZipEntry>) zipFile.entries();

    try {//from  ww w  . java  2 s  . com
        while (zipEntries.hasMoreElements()) {
            ZipEntry zipEntry = zipEntries.nextElement();
            String fileName = outputDirectoryPath + File.separator + zipEntry.getName();

            if (zipEntry.isDirectory()) {
                FileUtils.forceMkdir(new File(fileName));
                continue;
            }
            File entryDestination = new File(fileName);
            File parent = entryDestination.getParentFile();
            if (parent != null) {
                FileUtils.forceMkdir(parent);
            }
            if (entryDestination.createNewFile()) {

                InputStream in = zipFile.getInputStream(zipEntry);
                OutputStream out = new FileOutputStream(entryDestination);
                try {
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(in);
                    IOUtils.closeQuietly(out);
                }
            } else {
                throw new IOException("Cannot create file :" + entryDestination.getCanonicalPath());
            }
        }
    } finally {
        zipFile.close();
    }
}

From source file:org.asciidoctor.gradle.ZipUtils.java

/**
 * Unzips a file from an input stream to a location specified by the ZipFileFactory interface.
 * @param zip/*from  ww  w.j a va2s.  c om*/
 * @param zipFileFactory
 * @throws IOException
 */
static void unzip(InputStream zip, ZipFileFactory zipFileFactory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(zip));
    ZipEntry zipEntry = zis.getNextEntry();

    while (zipEntry != null) {

        File unzippedFile = zipFileFactory.createUnzippedFile(zipEntry);

        if (zipEntry.isDirectory()) {
            unzippedFile.mkdirs();
        } else {
            unzippedFile.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(unzippedFile);

            IOUtils.copy(zis, fos);
        }
        zipEntry = zis.getNextEntry();
    }

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

From source file:ZipFileIO.java

/**
 * Return the first directory of this archive. This is needed to determine
 * the plugin directory.//from  w w w . ja  va 2 s.c  o  m
 * 
 * @param zipFile
 * @return <class>File</class> containing the first entry of this archive
 */
public static File getFirstFile(File zipFile) throws IOException {
    ZipInputStream in = null;
    try {
        // Open the ZIP file
        in = new ZipInputStream(new FileInputStream(zipFile));

        // Get the first entry
        ZipEntry entry = null;

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

            if (entry.isDirectory()) {
                return new File(outFilename);
            }
        }
    } finally {
        if (in != null) {
            // Close the stream
            in.close();
        }
    }
    return null;
}

From source file:Main.java

public static void unZip(InputStream zipFileIS, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();//from  www.j  ava 2  s  .c o  m
    }

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

    while (ze != null) {

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

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {

            FileOutputStream fos = new FileOutputStream(newFile);

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

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

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

From source file:de.suse.swamp.util.FileUtils.java

/**
 * Decompress the provided Stream to "targetPath"
 *///from   w w  w  .j  a v a2s .  com
public static void uncompress(InputStream inputFile, String targetPath) throws Exception {
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(inputFile));
    BufferedOutputStream dest = null;
    ZipEntry entry;
    while ((entry = zin.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[2048];
        // write the files to the disk
        if (entry.isDirectory()) {
            org.apache.commons.io.FileUtils.forceMkdir(new File(targetPath + "/" + entry.getName()));
        } else {
            FileOutputStream fos = new FileOutputStream(targetPath + "/" + entry.getName());
            dest = new BufferedOutputStream(fos, 2048);
            while ((count = zin.read(data, 0, 2048)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
    }
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * @param zipfile Input .zip file//from   w w  w.  j  a va2  s.  c  o  m
 * @param outdir Output directory
 */
public static void extract(InputStream zipfile, File outdir) {
    try {
        ZipInputStream zin = new ZipInputStream(zipfile);
        ZipEntry entry;
        String name, dir;
        Log.i("OF", "uncompressinggggg ");
        while ((entry = zin.getNextEntry()) != null) {
            name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(outdir, name);
                continue;
            }
            /* this part is necessary because file entry can come before
             * directory entry where is file located
             * i.e.:
             *   /foo/foo.txt
             *   /foo/
             */
            dir = dirpart(name);
            if (dir != null)
                mkdirs(outdir, dir);

            extractFile(zin, outdir, name);
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:cc.recommenders.utils.gson.GsonUtil.java

public static <T> List<T> deserializeZip(File zip, Class<T> classOfT) throws IOException {

    List<T> res = Lists.newLinkedList();
    ZipInputStream zis = null;/*from w w w  .j a  v a 2s . co m*/
    try {
        InputSupplier<FileInputStream> fis = Files.newInputStreamSupplier(zip);
        zis = new ZipInputStream(fis.getInput());
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final InputStreamReader reader = new InputStreamReader(zis);
                final T data = getInstance().fromJson(reader, classOfT);
                res.add(data);
            }
        }
    } finally {
        Closeables.closeQuietly(zis);
    }
    return res;
}