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 upZipFile(File zipFile, String folderPath) {
    ZipFile zf;/*  ww w. j  a va2  s.c o  m*/
    try {
        zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            if (entry.isDirectory()) {
                String dirstr = entry.getName();
                dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
                File f = new File(dirstr);
                f.mkdir();
                continue;
            }

            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            str = new String(str.getBytes("8859_1"), "GB2312");
            File desFile = new File(str);
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                desFile.createNewFile();
            }

            OutputStream out = new FileOutputStream(desFile);
            byte buffer[] = new byte[BUFF_SIZE];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }
            in.close();
            out.close();
        }
    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void unzip(final InputStream input, final File destFolder) {
    try {/*from   w  w w . j  av  a2  s .  c o m*/
        byte[] buffer = new byte[4096];
        int read;
        ZipInputStream is = new ZipInputStream(input);
        ZipEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                String fileName = entry.getName();
                File fileFolder = destFolder;
                int lastSep = entry.getName().lastIndexOf(File.separatorChar);
                if (lastSep != -1) {
                    String dirPath = fileName.substring(0, lastSep);
                    fileFolder = new File(fileFolder, dirPath);
                    fileName = fileName.substring(lastSep + 1);
                }
                fileFolder.mkdirs();
                File file = new File(fileFolder, fileName);
                FileOutputStream os = new FileOutputStream(file);
                while ((read = is.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
                os.flush();
                os.close();
            }
        }
        is.close();
    } catch (Exception ex) {
        throw new RuntimeException("Cannot unzip stream to " + destFolder.getAbsolutePath(), ex);
    }
}

From source file:com.opengamma.util.ZipUtils.java

/**
 * Unzips a ZIP archive.//from   ww w.  j  av a 2s.  com
 * 
 * @param zipFile  the archive file, not null
 * @param outputDir  the output directory, not null
 */
public static void unzipArchive(final ZipFile zipFile, final File outputDir) {
    ArgumentChecker.notNull(zipFile, "zipFile");
    ArgumentChecker.notNull(outputDir, "outputDir");

    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                FileUtils.forceMkdir(new File(outputDir, entry.getName()));
                continue;
            }
            File entryDestination = new File(outputDir, entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        zipFile.close();
    } catch (IOException ex) {
        throw new OpenGammaRuntimeException(
                "Error while extracting file: " + zipFile.getName() + " to: " + outputDir, ex);
    }
}

From source file:net.sourceforge.atunes.utils.ZipUtils.java

/**
 * Unzips a zip entry in a directory// www.  j ava2  s  . c  o  m
 * 
 * @param zipfile
 * @param entry
 * @param outputDir
 * @throws IOException
 */
private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;
    }
    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        ClosingUtils.close(outputStream);
        ClosingUtils.close(inputStream);
    }
}

From source file:org.apache.jackrabbit.oak.upgrade.cli.Util.java

public static void unzip(InputStream is, File targetDir) throws IOException {
    long start = System.currentTimeMillis();
    log.info("Unzipping to {}", targetDir.getAbsolutePath());
    final ZipInputStream zis = new ZipInputStream(is);
    try {//from w  ww  .  ja va  2s . co  m
        ZipEntry entry = null;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                new File(targetDir, entry.getName()).mkdirs();
            } else {
                File target = new File(targetDir, entry.getName());
                target.getParentFile().mkdirs();
                OutputStream output = new FileOutputStream(target);
                try {
                    IOUtils.copy(zis, output);
                } finally {
                    output.close();
                }
            }
        }
    } finally {
        zis.close();
    }
    final long delta = System.currentTimeMillis() - start;
    if (delta > 1000L) {
        log.info("Unzip took {} msec", delta);
    }
}

From source file:Main.java

public static List<File> unzip(File zip, File toDir) throws IOException {
    ZipFile zf = null;/*from  ww w.  j a va  2 s . co  m*/
    List<File> files = null;
    try {
        zf = new ZipFile(zip);
        files = new ArrayList<File>();
        Enumeration<?> entries = zf.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            if (entry.isDirectory()) {
                new File(toDir, entry.getName()).mkdirs();
                continue;
            }

            InputStream input = null;
            OutputStream output = null;
            try {
                File f = new File(toDir, entry.getName());
                input = zf.getInputStream(entry);
                output = new FileOutputStream(f);
                copy(input, output);
                files.add(f);
            } finally {
                closeQuietly(output);
                closeQuietly(input);
            }
        }
    } finally {
        if (zf != null) {
            zf.close();
        }
    }
    return files;
}

From source file:Main.java

public static File UpdateZipFromPath(String sZipFile, String sPath) throws Exception {
    File tmpFile = File.createTempFile("z4zip-tmp-", ".zip");
    tmpFile.deleteOnExit();/*from w ww. ja  va2s  .  c  om*/
    ZipFile inZip = new ZipFile(sZipFile);
    ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(tmpFile.getPath()));

    Enumeration<? extends ZipEntry> entries = inZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        outZip.putNextEntry(e);
        if (!e.isDirectory()) {
            File f = new File(sPath + "/" + e.getName());
            if (f.exists()) {
                copy(new FileInputStream(f.getPath()), outZip);
            } else {
                copy(inZip.getInputStream(e), outZip);
            }
        }
        outZip.closeEntry();
    }
    inZip.close();
    outZip.close();
    return tmpFile;
}

From source file:com.google.gdt.eclipse.designer.webkit.WebKitSupportWin32.java

private static void extract(ZipFile zipFile) throws Exception {
    // remove any possibly corrupted contents
    FileUtils.deleteQuietly(WEBKIT_DIR);
    WEBKIT_DIR.mkdirs();/*from  w ww .  ja v  a 2 s . c o m*/
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            new File(WEBKIT_DIR, entry.getName()).mkdirs();
            continue;
        }
        InputStream inputStream = zipFile.getInputStream(entry);
        File outputFile = new File(WEBKIT_DIR, entry.getName());
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        IOUtils.copy(inputStream, outputStream);
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:cc.recommenders.utils.Zips.java

public static void unzip(File source, File dest) throws IOException {
    ZipInputStream zis = null;//from  w w w .ja  va 2  s .  co m
    try {
        InputSupplier<FileInputStream> fis = Files.newInputStreamSupplier(source);
        zis = new ZipInputStream(fis.getInput());
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final File file = new File(dest, entry.getName());
                Files.createParentDirs(file);
                Files.write(ByteStreams.toByteArray(zis), file);
            }
        }
    } finally {
        Closeables.closeQuietly(zis);
    }
}

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

public static int unzip(File zip, File dest) {
    try {/*from w w w .j a 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;
    }
}