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.glaf.core.util.ZipUtils.java

public static void unzip(File zipFile, String dir) throws Exception {
    File file = new File(dir);
    FileUtils.mkdirsWithExistsCheck(file);
    FileInputStream fileInputStream = null;
    ZipInputStream zipInputStream = null;
    FileOutputStream fileoutputstream = null;
    BufferedOutputStream bufferedoutputstream = null;
    try {//from w  w w . j  av a2s. c  om
        fileInputStream = new FileInputStream(zipFile);
        zipInputStream = new ZipInputStream(fileInputStream);
        fileInputStream = new FileInputStream(zipFile);
        zipInputStream = new ZipInputStream(fileInputStream);
        java.util.zip.ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            boolean isDirectory = zipEntry.isDirectory();
            byte abyte0[] = new byte[BUFFER];
            String s1 = zipEntry.getName();
            s1 = convertEncoding(s1);

            String s2 = dir + "/" + s1;
            s2 = FileUtils.getJavaFileSystemPath(s2);

            if (s2.indexOf('/') != -1 || isDirectory) {
                String s4 = s2.substring(0, s2.lastIndexOf('/'));
                File file2 = new File(s4);
                FileUtils.mkdirsWithExistsCheck(file2);
            }

            if (isDirectory) {
                continue;
            }

            fileoutputstream = new FileOutputStream(s2);
            bufferedoutputstream = new BufferedOutputStream(fileoutputstream, BUFFER);
            int i = 0;
            while ((i = zipInputStream.read(abyte0, 0, BUFFER)) != -1) {
                bufferedoutputstream.write(abyte0, 0, i);
            }
            bufferedoutputstream.flush();
            IOUtils.closeStream(fileoutputstream);
            IOUtils.closeStream(bufferedoutputstream);
        }

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(fileInputStream);
        IOUtils.closeStream(zipInputStream);
        IOUtils.closeStream(fileoutputstream);
        IOUtils.closeStream(bufferedoutputstream);
    }
}

From source file:com.elastica.helper.FileUtility.java

public static void extractJar(final String storeLocation, final Class<?> clz) throws IOException {
    File firefoxProfile = new File(storeLocation);
    String location = clz.getProtectionDomain().getCodeSource().getLocation().getFile();

    JarFile jar = new JarFile(location);
    System.out.println("Extracting jar file::: " + location);
    firefoxProfile.mkdir();/*from   w w w  .  j  av a2  s. co  m*/

    Enumeration<?> jarFiles = jar.entries();
    while (jarFiles.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) jarFiles.nextElement();
        String currentEntry = entry.getName();
        File destinationFile = new File(storeLocation, currentEntry);
        File destinationParent = destinationFile.getParentFile();

        // create the parent directory structure if required
        destinationParent.mkdirs();
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(jar.getInputStream(entry));
            int currentByte;

            // buffer for writing file
            byte[] data = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destinationFile);
            BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER);

            // read and write till last byte
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                destination.write(data, 0, currentByte);
            }

            destination.flush();
            destination.close();
            is.close();
        }
    }

    FileUtils.deleteDirectory(new File(storeLocation + "\\META-INF"));
    if (OSUtility.isWindows()) {
        new File(storeLocation + "\\" + clz.getCanonicalName().replaceAll("\\.", "\\\\") + ".class").delete();
    } else {
        new File(storeLocation + "/" + clz.getCanonicalName().replaceAll("\\.", "/") + ".class").delete();
    }
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java

public static String getShapefileNameFromCompressed(InputStream is) {
    String shapefileName = null;/*from w w w. j  a v a 2 s  . c om*/
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry ze;
    try {
        while (((ze = zis.getNextEntry()) != null) && (shapefileName == null)) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                if (fileName.endsWith(".shp")) {
                    shapefileName = fileName;
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            zis.closeEntry();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return shapefileName;
}

From source file:com.github.alexfalappa.nbspringboot.projects.initializr.InitializrProjectWizardIterator.java

private static void unZipFile(InputStream source, FileObject projectRoot, boolean removeMvnWrapper)
        throws IOException {
    try {/*from w  w w.  j a  va 2 s  . com*/
        ZipInputStream str = new ZipInputStream(source);
        ZipEntry entry;
        while ((entry = str.getNextEntry()) != null) {
            final String entryName = entry.getName();
            // optionally skip entries related to maven wrapper
            if (removeMvnWrapper && (entryName.contains(".mvn") || entryName.contains("mvnw"))) {
                continue;
            }
            if (entry.isDirectory()) {
                FileUtil.createFolder(projectRoot, entryName);
            } else {
                FileObject fo = FileUtil.createData(projectRoot, entryName);
                if ("nbproject/project.xml".equals(entryName)) {
                    // Special handling for setting name of Ant-based projects; customize as needed:
                    filterProjectXML(fo, str, projectRoot.getName());
                } else {
                    writeFile(str, fo);
                }
            }
        }
    } finally {
        source.close();
    }
}

From source file:lucee.commons.io.compress.CompressUtil.java

private static void unzip(Resource zipFile, Resource targetDir) throws IOException {
    /*if(zipFile instanceof File){
       unzip((File)zipFile, targetDir);/*from  w ww.ja  v a 2  s.co m*/
       return;
    }*/

    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(IOUtil.toBufferedInputStream(zipFile.getInputStream()));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            Resource target = targetDir.getRealResource(entry.getName());
            if (entry.isDirectory()) {
                target.mkdirs();
            } else {
                Resource parent = target.getParentResource();
                if (!parent.exists())
                    parent.mkdirs();
                IOUtil.copy(zis, target, false);
            }
            target.setLastModified(entry.getTime());
            zis.closeEntry();
        }
    } finally {
        IOUtil.closeEL(zis);
    }
}

From source file:net.ymate.platform.commons.util.ClassUtils.java

@SuppressWarnings("unchecked")
protected static <T> void __doFindClassByZip(Collection<Class<T>> collections, Class<T> clazz,
        String packageName, URL zipUrl, Class<?> callingClass) {
    ZipInputStream _zipStream = null;
    try {//from ww w.j  av a2s  . c  o m
        String _zipFilePath = zipUrl.toString();
        if (_zipFilePath.indexOf('!') > 0) {
            _zipFilePath = StringUtils.substringBetween(zipUrl.toString(), "zip:", "!");
        } else {
            _zipFilePath = StringUtils.substringAfter(zipUrl.toString(), "zip:");
        }
        _zipStream = new ZipInputStream(new FileInputStream(new File(_zipFilePath)));
        ZipEntry _zipEntry = null;
        while (null != (_zipEntry = _zipStream.getNextEntry())) {
            if (!_zipEntry.isDirectory()) {
                if (_zipEntry.getName().endsWith(".class") && _zipEntry.getName().indexOf('$') < 0) {
                    Class<?> _class = __doProcessEntry(zipUrl, _zipEntry);
                    if (_class != null) {
                        if (clazz.isAnnotation()) {
                            if (isAnnotationOf(_class, (Class<Annotation>) clazz)) {
                                collections.add((Class<T>) _class);
                            }
                        } else if (clazz.isInterface()) {
                            if (isInterfaceOf(_class, clazz)) {
                                collections.add((Class<T>) _class);
                            }
                        } else if (isSubclassOf(_class, clazz)) {
                            collections.add((Class<T>) _class);
                        }
                    }
                }
            }
            _zipStream.closeEntry();
        }
    } catch (Exception e) {
        _LOG.warn("", RuntimeUtils.unwrapThrow(e));
    } finally {
        if (_zipStream != null) {
            try {
                _zipStream.close();
            } catch (IOException e) {
                _LOG.warn("", RuntimeUtils.unwrapThrow(e));
            }
        }
    }
}

From source file:eionet.gdem.utils.ZipUtil.java

/**
 * Unzips files to output directory./*w w  w.  jav a  2  s.  c  om*/
 * @param inZip Zip file
 * @param outDir Output directory
 * @throws IOException If an error occurs.
 */
public static void unzip(String inZip, String outDir) throws IOException {

    File sourceZipFile = new File(inZip);
    File unzipDestinationDirectory = new File(outDir);

    // Open Zip file for reading
    ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

        String currentEntry = entry.getName();
        // System.out.println("Extracting: " + entry);

        File destFile = new File(unzipDestinationDirectory, currentEntry);

        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        // extract file if not a directory
        if (!entry.isDirectory()) {
            BufferedInputStream is = null;
            BufferedOutputStream dest = null;
            try {
                is = new BufferedInputStream(zipFile.getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte[] data = new byte[BUFFER];

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

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

From source file:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java

/**
 * @param tmp//from w  w  w . j av  a2 s.com
 * @param
 * @throws FileNotFoundException
 * @throws IOException
 */
private static void unzip(File file, File dir, PropertySetter ps, JProgressBar bar)
        throws FileNotFoundException, IOException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(file);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;
    int value = 0;
    while ((entry = zis.getNextEntry()) != null) {
        bar.setValue(value++);
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        String outputFile = dir.getAbsolutePath() + File.separator + entry.getName();
        if (entry.isDirectory()) {
            new File(outputFile).mkdirs();
        } else {
            FileOutputStream fos = new FileOutputStream(outputFile);
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
            File oFile = new File(outputFile);
            if (OSInfosDefault.INSTANCE.getOs() == OperatingSystem.OSX) {
                // FIXME: we need to make everything executable as somehow
                // the executable bit is not preserved in
                // OSX
                oFile.setExecutable(true);
            }
            if (ps.filterFilename(oFile)) {
                ps.setProperty(outputFile);
            }
        }
    }
    zis.close();
    file.delete();
}

From source file:com.cip.crane.agent.utils.FileExtractUtils.java

/**
 * Unzip an input file into an output file.
 * <p>//  w  w  w.  j a  v  a2s .c  o m
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.zip' extension. 
 * 
 * @param inputFile     the input .zip file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@File} with the ungzipped content.
 */
public static void unZip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException {
    LOG.debug(
            String.format("Unzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;
    zipinputstream = new ZipInputStream(new FileInputStream(inputFile));

    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        //for each entry to be extracted
        String entryName = outputDir + "/" + zipentry.getName();
        entryName = entryName.replace('/', File.separatorChar);
        entryName = entryName.replace('\\', File.separatorChar);
        int n;
        FileOutputStream fileoutputstream;
        File newFile = new File(entryName);

        if (zipentry.isDirectory()) {
            if (!newFile.mkdirs()) {
                break;
            }
            zipentry = zipinputstream.getNextEntry();
            continue;
        }
        fileoutputstream = new FileOutputStream(entryName);

        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }

        fileoutputstream.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();

    } //while
    zipinputstream.close();
}

From source file:marytts.util.io.FileUtils.java

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

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;/*ww  w. jav  a 2s. c  om*/
    }

    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 {
        outputStream.close();
        inputStream.close();
    }
}