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:com.sqli.liferay.imex.util.xml.ZipUtils.java

private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;//from   w ww .j a v a2  s .c  o m
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    log.debug("Extracting: " + entry);
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }
}

From source file:com.samczsun.helios.LoadedFile.java

public void reset() throws IOException {
    files.clear();//from w  w w.j  ava  2 s  .co  m
    classes.clear();
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
        while (enumeration.hasMoreElements()) {
            ZipEntry zipEntry = enumeration.nextElement();
            if (!zipEntry.isDirectory()) {
                load(zipEntry.getName(), zipFile.getInputStream(zipEntry));
            }
        }
    } catch (ZipException e) { //Probably not a ZIP file
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            load(this.name, inputStream);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:gui.accessories.DownloadProgressWork.java

/**
 * Uncompress all the files contained in the compressed file and its folders in the same folder where the zip file is placed. Doesn't respects the
 * directory tree of the zip file. This method seems a clear candidate to ZipManager.
 *
 * @param file Zip file.//from  ww  w.ja v a  2s. c om
 * @return Count of files uncopressed.
 * @throws ZipException Exception.
 */
private int doUncompressZip(File file) throws ZipException {
    int fileCount = 0;
    try {
        byte[] buf = new byte[1024];
        ZipFile zipFile = new ZipFile(file);

        Enumeration zipFileEntries = zipFile.entries();

        while (zipFileEntries.hasMoreElements()) {
            ZipEntry zipentry = (ZipEntry) zipFileEntries.nextElement();
            if (zipentry.isDirectory()) {
                continue;
            }
            File entryZipFile = new File(zipentry.getName());
            File outputFile = new File(file.getParentFile(), entryZipFile.getName());
            FileOutputStream fileoutputstream = new FileOutputStream(outputFile);
            InputStream is = zipFile.getInputStream(zipentry);
            int n;
            while ((n = is.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, n);
            }
            fileoutputstream.close();
            is.close();
            fileoutputstream.close();
            fileCount++;
        }
        zipFile.close();

    } catch (IOException ex) {
        throw new ZipException(ex.getMessage());
    }

    return fileCount;
}

From source file:com.arykow.autotools.generator.App.java

private void generate() throws Exception {
    File directory = new File(projectDirectory);
    if (!directory.isDirectory()) {
        throw new RuntimeException();
    }//from   w  ww.  j  av  a 2  s.  c om
    File output = new File(directory, projectName);
    if (output.exists()) {
        if (projectForced) {
            if (!output.isDirectory()) {
                throw new RuntimeException();
            }
        } else {
            throw new RuntimeException();
        }
    } else if (!output.mkdir()) {
        throw new RuntimeException();
    }

    CodeSource src = getClass().getProtectionDomain().getCodeSource();
    if (src != null) {
        URL jar = src.getLocation();
        ZipInputStream zip = new ZipInputStream(jar.openStream());
        while (true) {
            ZipEntry e = zip.getNextEntry();
            if (e == null)
                break;
            if (!e.isDirectory() && e.getName().startsWith(String.format("%s", templateName))) {
                // generate(output, e);

                StringWriter writer = new StringWriter();
                IOUtils.copy(zip, writer);
                String content = writer.toString();

                Map<String, String> expressions = new HashMap<String, String>();
                expressions.put("author", "Karim DRIDI");
                expressions.put("name_undescore", projectName);
                expressions.put("license", "<Place your desired license here.>");
                expressions.put("name", projectName);
                expressions.put("version", "1.0");
                expressions.put("copyright", "Your copyright notice");
                expressions.put("description", "Hello World in C++,");

                for (Map.Entry<String, String> entry : expressions.entrySet()) {
                    content = content.replaceAll(String.format("<project\\.%s>", entry.getKey()),
                            entry.getValue());
                }

                String name = e.getName().substring(templateName.length() + 1);
                if ("gitignore".equals(name)) {
                    name = ".gitignore";
                }
                File file = new File(output, name);
                File parent = file.getParentFile();
                if (parent.exists()) {
                    if (!parent.isDirectory()) {
                        throw new RuntimeException();
                    }
                } else {
                    if (!parent.mkdirs()) {
                        throw new RuntimeException();
                    }
                }
                OutputStream stream = new FileOutputStream(file);
                IOUtils.copy(new StringReader(content), stream);
                IOUtils.closeQuietly(stream);
            }
        }
    }
}

From source file:ZipFileViewer.java

public Object getValueAt(int row, int col) {
    ZipEntry zipEntry = (ZipEntry) (m_zipEntries.get(row));
    switch (col) {
    case NAME://from w w w  . j ava2  s  .  c o  m
        return zipEntry.getName();
    case SIZE:
        if (zipEntry.isDirectory()) {
            return "";
        } else {
            return String.valueOf(zipEntry.getSize() / 1000) + " KB";
        }
    case COMP_SIZE:
        if (zipEntry.isDirectory()) {
            return "";
        } else {
            return String.valueOf(zipEntry.getCompressedSize() / 1000) + " KB";
        }
    case TYPE:
        if (zipEntry.isDirectory()) {
            return "Directory";
        } else {
            return "File";
        }
    case LAST_MODI:
        return String.valueOf(new Date(zipEntry.getTime()));
    }
    return new String();
}

From source file:net.codestory.simplelenium.driver.Downloader.java

protected void unzip(File zip, File toDir) throws IOException {
    try (ZipFile zipFile = new ZipFile(zip)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                continue;
            }/*w w  w.  j  ava 2s.c o m*/

            File to = new File(toDir, entry.getName());

            File parent = to.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create folder " + parent);
                }
            }

            try (InputStream input = zipFile.getInputStream(entry)) {
                Files.copy(input, to.toPath(), REPLACE_EXISTING);
            }
        }
    }
}

From source file:ch.astina.hesperid.web.services.dbmigration.impl.ClasspathMigrationResolver.java

public Set<Migration> resolve() {
    Set<Migration> migrations = new HashSet<Migration>();

    try {/*from  www. j  a  v a2  s.  c  o  m*/
        Enumeration<URL> enumeration = getClass().getClassLoader().getResources(classPath);

        while (enumeration.hasMoreElements()) {

            URL url = enumeration.nextElement();

            if (url.toExternalForm().startsWith("jar:")) {
                String file = url.getFile();

                file = file.substring(0, file.indexOf("!"));
                file = file.substring(file.indexOf(":") + 1);

                InputStream is = new FileInputStream(file);

                ZipInputStream zip = new ZipInputStream(is);

                ZipEntry ze;

                while ((ze = zip.getNextEntry()) != null) {
                    if (!ze.isDirectory() && ze.getName().startsWith(classPath)
                            && ze.getName().endsWith(".sql")) {

                        Resource r = new UrlResource(getClass().getClassLoader().getResource(ze.getName()));

                        String version = versionExtractor.extractVersion(r.getFilename());
                        migrations.add(migrationFactory.create(version, r));
                    }
                }
            } else {
                File file = new File(url.getFile());

                for (String s : file.list()) {
                    Resource r = new UrlResource(getClass().getClassLoader().getResource(classPath + "/" + s));

                    String version = versionExtractor.extractVersion(r.getFilename());
                    migrations.add(migrationFactory.create(version, r));
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error while resolving migrations", e);
    }

    return migrations;
}

From source file:org.geowe.server.upload.FileUploadZipServlet.java

private String readZipFile(ZipFile zipFile) {
    String content = EMPTY;//from ww w.j  a  v a 2  s .  com

    try {

        Enumeration<?> enu = zipFile.entries();
        if (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();
            if (zipEntry.isDirectory()) {
                content = BAD_FORMAT;
            } else if (!(zipEntry.getName().equals(PRJ_FILE_NAME))) {
                content = BAD_FORMAT;
            } else {
                InputStream is = zipFile.getInputStream(zipEntry);
                content = new java.util.Scanner(is).useDelimiter("\\A").next();
            }

            // String name = zipEntry.getName();
            // long size = zipEntry.getSize();
            // long compressedSize = zipEntry.getCompressedSize();
            // LOG.info("name: " + name + " | size: " + size
            // + " | compressed size: " + compressedSize);

        }
        zipFile.close();

    } catch (IOException e) {
        LOG.error("Se produce error en ZipFile: " + e.getMessage());
        e.printStackTrace();
    }

    return content;
}

From source file:com.seer.datacruncher.streams.ZipStreamTest.java

@Test
public void testZipStream() {
    String fileName = properties.getProperty("zip_test_stream_file_name");
    InputStream in = this.getClass().getClassLoader().getResourceAsStream(stream_file_path + fileName);
    byte[] arr = null;
    try {//from   w ww. ja  v a  2s  .c om
        arr = IOUtils.toByteArray(in);
    } catch (IOException e) {
        assertTrue("IOException while Zip test file reading", false);
    }

    ZipInputStream inStream = null;
    try {
        inStream = new ZipInputStream(new ByteArrayInputStream(arr));
        ZipEntry entry;
        while (!(isStreamClose(inStream)) && (entry = inStream.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                DatastreamsInput datastreamsInput = new DatastreamsInput();
                datastreamsInput.setUploadedFileName(entry.getName());
                byte[] byteInput = IOUtils.toByteArray(inStream);
                String res = datastreamsInput.datastreamsInput(null, (Long) schemaEntity.getIdSchema(),
                        byteInput, true);
                assertTrue("Zip file validation failed", Boolean.parseBoolean(res));
            }
            inStream.closeEntry();
        }
    } catch (IOException ex) {
        assertTrue("Error occured during fetch records from ZIP file.", false);
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
            }
    }
}

From source file:com.google.gwt.dev.resource.impl.ZipFileClassPathEntry.java

private Set<ZipFileResource> buildIndex(TreeLogger logger) {
    logger = Messages.BUILDING_INDEX.branch(logger, zipFile.getName(), null);

    Set<ZipFileResource> results = new IdentityHashSet<ZipFileResource>();
    Enumeration<? extends ZipEntry> e = zipFile.entries();
    while (e.hasMoreElements()) {
        ZipEntry zipEntry = e.nextElement();
        if (zipEntry.isDirectory()) {
            // Skip directories.
            continue;
        }//from w  w w.  ja  v a  2  s.c  om
        if (zipEntry.getName().startsWith("META-INF/")) {
            // Skip META-INF since classloaders normally make this invisible.
            continue;
        }
        ZipFileResource zipResource = new ZipFileResource(this, zipEntry.getName());
        results.add(zipResource);
        Messages.READ_ZIP_ENTRY.log(logger, zipEntry.getName(), null);
    }
    return Sets.normalize(results);
}