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.sastix.cms.server.services.content.impl.ZipFileHandlerServiceImpl.java

@Override
public DataMaps unzip(byte[] bytes) throws IOException {
    Map<String, String> foldersMap = new HashMap<>();

    Map<String, byte[]> extractedBytesMap = new HashMap<>();
    InputStream byteInputStream = new ByteArrayInputStream(bytes);
    //validate that it is a zip file
    if (isZipFile(bytes)) {
        try {//  ww  w  .  j a  va2  s  . c  om
            //get the zip file content
            ZipInputStream zis = new ZipInputStream(byteInputStream);
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                if (!ze.isDirectory()) {//if entry is a directory, we should not add it as a file
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    try {
                        ByteBuffer bufIn = ByteBuffer.allocate(1024);
                        int bytesRead;
                        while ((bytesRead = zis.read(bufIn.array())) > 0) {
                            baos.write(bufIn.array(), 0, bytesRead);
                            bufIn.rewind();
                        }
                        bufIn.clear();
                        extractedBytesMap.put(fileName, baos.toByteArray());
                    } finally {
                        baos.close();
                    }
                } else {
                    foldersMap.put(fileName, fileName);
                }
                ze = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    DataMaps dataMaps = new DataMaps();
    dataMaps.setBytesMap(extractedBytesMap);
    dataMaps.setFoldersMap(foldersMap);
    return dataMaps;
}

From source file:com.wakatime.eclipse.plugin.Dependencies.java

private void unzip(String zipFile, File outputDir) throws IOException {
    if (!outputDir.exists())
        outputDir.mkdirs();// w w  w .  jav  a 2  s  . c o m

    byte[] buffer = new byte[1024];
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        String fileName = ze.getName();
        File newFile = new File(outputDir, fileName);

        if (ze.isDirectory()) {
            // WakaTime.log("Creating directory: "+newFile.getParentFile().getAbsolutePath());
            newFile.mkdirs();
        } else {
            // WakaTime.log("Extracting File: "+newFile.getAbsolutePath());
            FileOutputStream fos = new FileOutputStream(newFile.getAbsolutePath());
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
        }

        ze = zis.getNextEntry();
    }

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

From source file:io.apicurio.hub.api.codegen.OpenApi2ThorntailTest.java

/**
 * Test method for {@link io.apicurio.hub.api.codegen.OpenApi2Thorntail#generate()}.
 *//*from ww w. j  a va 2  s  .  c  o m*/
@Test
public void testGenerateUpdateOnly() throws IOException {
    OpenApi2Thorntail generator = new OpenApi2Thorntail();
    generator.setUpdateOnly(true);
    generator
            .setOpenApiDocument(getClass().getClassLoader().getResource("OpenApi2ThorntailTest/beer-api.json"));
    ByteArrayOutputStream outputStream = generator.generate();

    // Validate the result
    try (ZipInputStream zipInputStream = new ZipInputStream(
            new ByteArrayInputStream(outputStream.toByteArray()))) {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            if (!zipEntry.isDirectory()) {
                String name = zipEntry.getName();
                Assert.assertNotNull(name);

                URL expectedFile = getClass().getClassLoader()
                        .getResource(getClass().getSimpleName() + "/_expected-full/generated-api/" + name);
                Assert.assertNotNull("Could not find expected file for entry: " + name, expectedFile);
                String expected = IOUtils.toString(expectedFile);

                String actual = IOUtils.toString(zipInputStream);
                Assert.assertEquals("Expected vs. actual failed for entry: " + name, normalizeString(expected),
                        normalizeString(actual));
            }
            zipEntry = zipInputStream.getNextEntry();
        }
    }
}

From source file:io.sledge.core.impl.extractor.SledgeApplicationPackageExtractor.java

@Override
public List<String> getEnvironmentNames(ApplicationPackage appPackage) {
    List<String> envFiles = new ArrayList<>();
    ZipInputStream zipStream = getNewUtf8ZipInputStream(appPackage);

    try {/*from  ww w .  j  a  v  a 2s . c  o  m*/
        ZipEntry zipEntry = null;

        while ((zipEntry = zipStream.getNextEntry()) != null) {

            if (zipEntry.isDirectory()) {
                zipStream.closeEntry();
                continue;
            }

            if (zipEntry.getName().startsWith("environments/")) {
                // test-publish.properties -> test-publish
                envFiles.add(getBaseName(zipEntry.getName()));
                zipStream.closeEntry();
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            zipStream.close();
            appPackage.getPackageFile().reset();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    return envFiles;
}

From source file:com.ning.maven.plugins.duplicatefinder.ClasspathDescriptor.java

private void addArchive(File element) throws IOException {
    if (addCached(element)) {
        return;//from w w  w .  ja  v a2s  .co m
    }

    List classes = new ArrayList();
    List resources = new ArrayList();
    ZipFile zipFile = null;

    try {
        zipFile = new ZipFile(element);
        Enumeration entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                String name = entry.getName();

                if ("class".equals(FilenameUtils.getExtension(name))) {
                    String className = FilenameUtils.removeExtension(name).replace('/', '.').replace('\\', '.');

                    classes.add(className);
                    addClass(className, element);
                } else {
                    String resourcePath = name.replace('\\', File.separatorChar);

                    resources.add(resourcePath);
                    addResource(resourcePath, element);
                }
            }
        }

        CACHED_BY_ELEMENT.put(element, new Cached(classes, resources));
    } finally {
        // IOUtils has no closeQuietly for Closable in the 1.x series.
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ex) {
                // ignored
            }
        }
    }
}

From source file:io.apicurio.hub.api.codegen.OpenApi2ThorntailTest.java

/**
 * Test method for {@link io.apicurio.hub.api.codegen.OpenApi2Thorntail#generate()}.
 *//* www .  j  a v a 2  s .co  m*/
@Test
public void testGenerateFull() throws IOException {
    OpenApi2Thorntail generator = new OpenApi2Thorntail();
    generator.setUpdateOnly(false);
    generator
            .setOpenApiDocument(getClass().getClassLoader().getResource("OpenApi2ThorntailTest/beer-api.json"));
    ByteArrayOutputStream outputStream = generator.generate();

    //        FileUtils.writeByteArrayToFile(new File("C:\\Users\\ewittman\\tmp\\output-full.zip"), outputStream.toByteArray());

    // Validate the result
    try (ZipInputStream zipInputStream = new ZipInputStream(
            new ByteArrayInputStream(outputStream.toByteArray()))) {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            if (!zipEntry.isDirectory()) {
                String name = zipEntry.getName();
                //                    System.out.println(name);
                Assert.assertNotNull(name);

                URL expectedFile = getClass().getClassLoader()
                        .getResource(getClass().getSimpleName() + "/_expected-full/generated-api/" + name);
                Assert.assertNotNull("Could not find expected file for entry: " + name, expectedFile);
                String expected = IOUtils.toString(expectedFile);

                String actual = IOUtils.toString(zipInputStream);
                //                    System.out.println("-----");
                //                    System.out.println(actual);
                //                    System.out.println("-----");
                Assert.assertEquals("Expected vs. actual failed for entry: " + name, normalizeString(expected),
                        normalizeString(actual));
            }
            zipEntry = zipInputStream.getNextEntry();
        }
    }
}

From source file:io.apicurio.hub.api.codegen.OpenApi2ThorntailTest.java

/**
 * Test method for {@link io.apicurio.hub.api.codegen.OpenApi2Thorntail#generate()}.
 */// w  ww. ja va2 s  .c  om
@Test
public void testGenerateFull_GatewayApi() throws IOException {
    OpenApi2Thorntail generator = new OpenApi2Thorntail();
    generator.setUpdateOnly(false);
    generator.setOpenApiDocument(
            getClass().getClassLoader().getResource("OpenApi2ThorntailTest/gateway-api.json"));
    ByteArrayOutputStream outputStream = generator.generate();

    //        FileUtils.writeByteArrayToFile(new File("C:\\Users\\ewittman\\tmp\\testGenerateFull_GatewayApi.zip"), outputStream.toByteArray());

    // Validate the result
    try (ZipInputStream zipInputStream = new ZipInputStream(
            new ByteArrayInputStream(outputStream.toByteArray()))) {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            if (!zipEntry.isDirectory()) {
                String name = zipEntry.getName();
                //                    System.out.println(name);
                Assert.assertNotNull(name);

                URL expectedFile = getClass().getClassLoader().getResource(
                        getClass().getSimpleName() + "/_expected-gatewayApi-full/generated-api/" + name);
                Assert.assertNotNull("Could not find expected file for entry: " + name, expectedFile);
                String expected = IOUtils.toString(expectedFile);

                String actual = IOUtils.toString(zipInputStream);
                //                    System.out.println("-----");
                //                    System.out.println(actual);
                //                    System.out.println("-----");
                Assert.assertEquals("Expected vs. actual failed for entry: " + name, normalizeString(expected),
                        normalizeString(actual));
            }
            zipEntry = zipInputStream.getNextEntry();
        }
    }
}

From source file:io.sledge.core.impl.extractor.SledgeApplicationPackageExtractor.java

@Override
public String getEnvironmentFile(String environmentName, ApplicationPackage appPackage) {
    ZipInputStream zipStream = getNewUtf8ZipInputStream(appPackage);
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    try {//from ww  w.j av  a  2  s.  c  o  m
        byte[] buffer = new byte[2048];
        ZipEntry zipEntry = null;

        while ((zipEntry = zipStream.getNextEntry()) != null) {

            if (zipEntry.isDirectory()) {
                zipStream.closeEntry();
                continue;
            }

            if (getBaseName(zipEntry.getName()).equals(environmentName)) {

                int length;
                while ((length = zipStream.read(buffer, 0, buffer.length)) >= 0) {
                    output.write(buffer, 0, length);
                }

                zipStream.closeEntry();

                // Stop here because the file has been already read
                break;
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            zipStream.close();
            appPackage.getPackageFile().reset();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    return output.toString();
}

From source file:io.sledge.core.impl.extractor.SledgeApplicationPackageExtractor.java

@Override
public Map<String, InputStream> getPackages(ApplicationPackage appPackage) {
    Map<String, InputStream> packages = new HashMap<>();
    ZipInputStream zipStream = getNewUtf8ZipInputStream(appPackage);

    try {//from   w ww .jav  a2s  .c  om
        byte[] buffer = new byte[2048];
        ZipEntry zipEntry = null;

        while ((zipEntry = zipStream.getNextEntry()) != null) {

            if (zipEntry.isDirectory()) {
                zipStream.closeEntry();
                continue;
            }

            if (zipEntry.getName().startsWith("packages/")) {
                ByteArrayOutputStream output = new ByteArrayOutputStream();

                int length;
                while ((length = zipStream.read(buffer, 0, buffer.length)) >= 0) {
                    output.write(buffer, 0, length);
                }

                String packageFileName = zipEntry.getName().replace("packages/", "");
                packages.put(packageFileName, new ByteArrayInputStream(output.toByteArray()));

                zipStream.closeEntry();
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            zipStream.close();
            appPackage.getPackageFile().reset();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    return packages;
}

From source file:isl.FIMS.utils.Utils.java

/**
 * Unzip it (This implementation works only when zip contains files-folders
 * with ASCII filenames Greek characters break the code!
 *
 * @param zipFile input zip file/*from  w  ww  . j ava 2s .  c o m*/
 * @param output zip file output folder
 */
public static void unzip(String zipFile, String outputFolder) {

    String rootFolderName = "";
    String rootFlashFilename = "";
    byte[] buffer = new byte[1024];

    try {

        //get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(outputFolder + File.separator + zipFile));
        //get the zipped file list entry

        ZipEntry ze = zis.getNextEntry();

        boolean rootDirFound = false;
        boolean flashFileFound = false;
        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            if (!ze.getName().contains("__MACOSX")) {
                if (ze.isDirectory()) {
                    if (rootDirFound == false) {
                        rootFolderName = newFile.getName();
                        rootDirFound = true;
                    }
                    new File(newFile.getParent()).mkdirs();
                } else {
                    FileOutputStream fos = null;

                    new File(newFile.getParent()).mkdirs();

                    if (flashFileFound == false && newFile.getName().endsWith(".swf")
                            && !newFile.getName().startsWith(".")) {
                        rootFlashFilename = newFile.getName();
                        flashFileFound = true;
                    }

                    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();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}