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:abfab3d.shapejs.Project.java

private static void extractZip(String zipFile, String outputFolder, Map<String, String> sceneFiles,
        List<String> resources) {
    byte[] buffer = new byte[1024];

    try {//  w  w  w .  j a  v  a2 s .  co  m
        //create output directory is not exists
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            // Ignore directories
            if (ze.isDirectory())
                continue;

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            System.out.println("file unzip : " + newFile.getAbsoluteFile());

            FileOutputStream fos = new FileOutputStream(newFile);

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

            // Save path to the script and parameters files
            if (fileName.endsWith(".json")) {
                sceneFiles.put("paramFile", newFile.getAbsolutePath());
            } else if (fileName.endsWith(".js")) {
                sceneFiles.put("scriptFile", newFile.getAbsolutePath());
            } else {
                resources.add(newFile.getAbsolutePath());
            }

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

        zis.closeEntry();
        zis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.bukanir.android.utils.Utils.java

public static String unzipSubtitle(String zip, String path) {
    InputStream is;//from w  w w .j a  v a2s .c o  m
    ZipInputStream zis;
    try {
        String filename = null;
        is = new FileInputStream(zip);
        zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) {
            filename = ze.getName();

            if (ze.isDirectory()) {
                File fmd = new File(path + "/" + filename);
                fmd.mkdirs();
                continue;
            }

            if (filename.endsWith(".srt") || filename.endsWith(".sub")) {
                FileOutputStream fout = new FileOutputStream(path + "/" + filename);
                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }
                fout.close();
                zis.closeEntry();
                break;
            }
            zis.closeEntry();
        }
        zis.close();

        File z = new File(zip);
        z.delete();

        return path + "/" + filename;

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.stevpet.sonar.plugins.dotnet.mscover.codecoverage.command.ZipUtils.java

/**
 * Extracts the specified folder from the specified archive, into the supplied output directory.
 * /*from ww  w .  j a  va2 s . c  om*/
 * @param archivePath
 *          the archive Path
 * @param folderToExtract
 *          the folder to extract
 * @param outputDirectory
 *          the output directory
 * @return the extracted folder path
 * @throws IOException
 *           if a problem occurs while extracting
 */
public static File extractArchiveFolderIntoDirectory(String archivePath, String folderToExtract,
        String outputDirectory) throws IOException {
    File destinationFolder = new File(outputDirectory);
    destinationFolder.mkdirs();

    ZipFile zip = null;
    try {
        zip = new ZipFile(new File(archivePath));
        Enumeration<?> zipFileEntries = zip.entries();
        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            if (currentEntry.startsWith(folderToExtract)) {
                File destFile = new File(destinationFolder, currentEntry);
                destFile.getParentFile().mkdirs();
                if (!entry.isDirectory()) {
                    BufferedInputStream is = null;
                    BufferedOutputStream dest = null;
                    try {
                        is = new BufferedInputStream(zip.getInputStream(entry));
                        int currentByte;
                        // establish buffer for writing file
                        byte data[] = new byte[BUFFER_SIZE];

                        // write the current file to disk
                        FileOutputStream fos = new FileOutputStream(destFile);
                        dest = new BufferedOutputStream(fos, BUFFER_SIZE);

                        // read and write until last byte is encountered
                        while ((currentByte = is.read(data, 0, BUFFER_SIZE)) != -1) {
                            dest.write(data, 0, currentByte);
                        }
                    } finally {
                        if (dest != null) {
                            dest.flush();
                        }
                        IOUtils.closeQuietly(dest);
                        IOUtils.closeQuietly(is);
                    }
                }
            }
        }
    } finally {
        if (zip != null) {
            zip.close();
        }
    }

    return new File(destinationFolder, folderToExtract);
}

From source file:edu.kit.dama.util.ZipUtils.java

/**
 * Extract single entry of zip file./*www . j  av  a2 s  .c o m*/
 *
 * @param pOutputDir where to store the unzipped entry.
 * @param pZipFile file containing zipped files.
 * @param pEntry one entry to extract.
 * @throws IOException missing access rights?
 */
private static void extractEntry(File pOutputDir, ZipFile pZipFile, ZipEntry pEntry) throws IOException {
    File destinationFilePath = new File(pOutputDir, pEntry.getName());
    if (pEntry.isDirectory()) {
        // The following command is neccessary to create also
        // empty directories.
        if (destinationFilePath.mkdirs()) {
            LOGGER.trace("create directory: '{}'", destinationFilePath.getPath());
        }
    } else {
        //create directories if required.
        if (destinationFilePath.getParentFile().mkdirs()) {
            LOGGER.trace("create directory: '{}'", destinationFilePath.getPath());
        }
    }
    //if the entry is directory, leave it. Otherwise extract it.
    if (!pEntry.isDirectory()) {
        LOGGER.debug("Extracting " + destinationFilePath);

        /*
         * Get the InputStream for current entry
         * of the zip file using
         *
         * InputStream getInputStream(Entry entry) method.
         */
        int noOfBytes;
        byte buffer[] = new byte[1024];

        try (BufferedInputStream bis = new BufferedInputStream(pZipFile.getInputStream(pEntry));
                FileOutputStream fos = new FileOutputStream(destinationFilePath);
                BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);) {
            /*
             * read the current entry from the zip file, extract it
             * and write the extracted file.
             */
            while ((noOfBytes = bis.read(buffer, 0, buffer.length)) != -1) {
                bos.write(buffer, 0, noOfBytes);
            }
            //flush the output stream.
            bos.flush();
        }
    }
}

From source file:net.ftb.util.FileUtils.java

/**
 * Extracts given zip to given location/*from  w  w  w . j  av  a  2  s .c o  m*/
 * @param zipLocation - the location of the zip to be extracted
 * @param outputLocation - location to extract to
 */
public static void extractZipTo(String zipLocation, String outputLocation) {
    ZipInputStream zipinputstream = null;
    try {
        byte[] buf = new byte[1024];
        zipinputstream = new ZipInputStream(new FileInputStream(zipLocation));
        ZipEntry zipentry = zipinputstream.getNextEntry();
        while (zipentry != null) {
            String entryName = zipentry.getName();
            int n;
            if (!zipentry.isDirectory() && !entryName.equalsIgnoreCase("minecraft")
                    && !entryName.equalsIgnoreCase(".minecraft") && !entryName.equalsIgnoreCase("instMods")) {
                new File(outputLocation + File.separator + entryName).getParentFile().mkdirs();
                FileOutputStream fileoutputstream = new FileOutputStream(
                        outputLocation + File.separator + entryName);
                while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
                    fileoutputstream.write(buf, 0, n);
                }
                fileoutputstream.close();
            }
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();
        }
    } catch (Exception e) {
        Logger.logError("Error while extracting zip", e);
        backupExtract(zipLocation, outputLocation);
    } finally {
        try {
            zipinputstream.close();
        } catch (IOException e) {
        }
    }
}

From source file:ZipUtil.java

public static void unZipZipFileToLocation(File zipFile, File targetDir) throws IOException {
    if (!targetDir.isDirectory()) {
        throw new Exception("Target is not a directory.");
    }//  w ww  . ja  v  a2s  . c  om
    FileInputStream flInStr = new FileInputStream(zipFile);
    try {
        ZipInputStream zis = new ZipInputStream(flInStr);
        try {
            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                String name = entry.getName();
                File newFile = new File(targetDir, name);
                if (entry.isDirectory() && !newFile.exists()) {
                    newFile.mkdirs();
                } else if (!entry.isDirectory()) {
                    if (newFile.exists()) {
                        newFile.delete();
                    }
                    File parentDir = newFile.getParentFile();
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }
                    FileOutputStream stmOut = new FileOutputStream(newFile);
                    try {
                        simpleInputStreamToOutputStream(zis, stmOut);
                    } finally {
                        stmOut.close();
                    }
                }
            } //end while.
        } finally {
            zis.close();
        }
    } finally {
        flInStr.close();
    }
}

From source file:org.nebulaframework.core.job.archive.GridArchive.java

/**
 * Detects all classes inside the given {@code .nar} file and returns an
 * array of fully qualified class name of each class, as {@code String}.
 * //from w  ww  .  j av  a2 s.c  o m
 * @param file
 *            {@code .nar File}
 * 
 * @return Fully qualified class names classes in {@code File}
 * 
 * @throws IOException
 *             if occurred during File I/O operations
 */
protected static String[] getAllClassNames(File file) throws IOException {

    // Holds Class Names
    List<String> names = new ArrayList<String>();

    // Create ZipArchive for File
    ZipFile archive = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = archive.entries();

    // Read each entry in archive
    while (entries.hasMoreElements()) {

        ZipEntry entry = entries.nextElement();

        // Ignore Directories
        if (entry.isDirectory())
            continue;

        // Ignore content in NEBULA-INF
        if (entry.getName().startsWith(GridArchive.NEBULA_INF)) {
            continue;
        }

        // Add each file which is a valid class file to list
        if (isClass(entry.getName())) {
            names.add(toClassName(entry.getName()));
        }
    }
    return names.toArray(new String[] {});
}

From source file:apim.restful.exportimport.utils.APIImportUtil.java

/**
 * This method decompresses the archive/*from  w  w w  .j av  a 2  s.  c  o  m*/
 *
 * @param sourceFile           the archive containing the API
 * @param destinationDirectory location of the archive to be extracted
 * @return extractedFolder the name of the zip
 */
public static String unzipArchive(File sourceFile, File destinationDirectory) throws APIManagementException {

    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;
    File destinationFile;
    ZipFile zipfile = null;
    String extractedFolder = null;

    try {
        zipfile = new ZipFile(sourceFile);
        Enumeration zipEntries = null;
        if (zipfile != null) {
            zipEntries = zipfile.entries();
        }
        if (zipEntries != null) {
            int index = 0;
            while (zipEntries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) zipEntries.nextElement();

                if (entry.isDirectory()) {

                    //This index variable is used to get the extracted folder name; that is root directory
                    if (index == 0) {

                        //Get the folder name without the '/' character at the end
                        extractedFolder = entry.getName().substring(0, entry.getName().length() - 1);
                    }
                    index = -1;
                    new File(destinationDirectory, entry.getName()).mkdir();
                    continue;
                }
                inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
                destinationFile = new File(destinationDirectory, entry.getName());
                fileOutputStream = new FileOutputStream(destinationFile);
                copyStreams(inputStream, fileOutputStream);
            }
        }
    } catch (ZipException e) {
        log.error("Error in retrieving archive files.");
        throw new APIManagementException("Error in retrieving archive files.", e);
    } catch (IOException e) {
        log.error("Error in decompressing API archive files.");
        throw new APIManagementException("Error in decompressing API archive files.", e);
    } finally {
        try {
            if (zipfile != null) {
                zipfile.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        } catch (IOException e) {
            log.error("Error in closing streams while decompressing files.");
        }
    }
    return extractedFolder;
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

public static void expandZip(String zipFileName, String rootDir, String prefixDir)
        throws FileNotFoundException, IOException {
    Engine.logEngine.debug("Expanding the zip file " + zipFileName);

    // Creating the root directory
    File ftmp = new File(rootDir);
    if (!ftmp.exists()) {
        ftmp.mkdirs();/*from ww  w. jav a2 s .  c  o  m*/
        Engine.logEngine.debug("Root directory created");
    }

    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));

    try {
        ZipEntry entry;
        int prefixSize = prefixDir != null ? prefixDir.length() : 0;

        while ((entry = zis.getNextEntry()) != null) {
            // Ignoring directories
            if (entry.isDirectory()) {

            } else {

                String entryName = entry.getName();
                Engine.logEngine.debug("+ Analyzing the entry: " + entryName);

                try {
                    // Ignore entry if does not belong to the project directory
                    if ((prefixDir == null) || entryName.startsWith(prefixDir)) {

                        // Ignore entry from _data or _private directory
                        if ((entryName.indexOf("/_data/") != prefixSize)
                                && (entryName.indexOf("/_private/") != prefixSize)) {
                            Engine.logEngine.debug("  The entry is accepted");
                            String s1 = rootDir + "/" + entryName;
                            String dir = s1.substring(0, s1.lastIndexOf('/'));

                            // Creating the directory if needed
                            ftmp = new File(dir);
                            if (!ftmp.exists()) {
                                ftmp.mkdirs();
                                Engine.logEngine.debug("  Directory created");
                            }

                            // Writing the files to the disk
                            File file = new File(rootDir + "/" + entryName);
                            FileOutputStream fos = new FileOutputStream(file);
                            try {
                                IOUtils.copy(zis, fos);
                            } finally {
                                fos.close();
                            }
                            file.setLastModified(entry.getTime());
                            Engine.logEngine.debug("  File written to: " + rootDir + "/" + entryName);
                        }
                    }
                } catch (IOException e) {
                    Engine.logEngine.error(
                            "Unable to expand the ZIP entry \"" + entryName + "\": " + e.getMessage(), e);
                }
            }
        }
    } finally {
        zis.close();
    }
}

From source file:com.ariatemplates.attester.maven.ArtifactExtractor.java

public static void unzip(File zipFile, File outputFolder) throws IOException {
    ZipInputStream zipInputStream = null;
    try {/*from   ww w  .  j av  a  2 s  .co m*/
        ZipEntry entry = null;
        zipInputStream = new ZipInputStream(FileUtils.openInputStream(zipFile));
        while ((entry = zipInputStream.getNextEntry()) != null) {
            File outputFile = new File(outputFolder, entry.getName());

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

            OutputStream outputStream = null;
            try {
                outputStream = FileUtils.openOutputStream(outputFile);
                IOUtils.copy(zipInputStream, outputStream);
                outputStream.close();
            } catch (IOException exception) {
                outputFile.delete();
                throw new IOException(exception);
            } finally {
                IOUtils.closeQuietly(outputStream);
            }
        }
        zipInputStream.close();
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}