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.dreikraft.axbo.sound.SoundPackageUtil.java

/**
 * Extracts the packageFile and writes its content in temporary directory
 *
 * @param packageFile the packageFile (zip format)
 * @param tempDir the tempDir directory/*from   w w w.j  a va  2 s. c o m*/
 */
public static void extractPackage(File packageFile, File tempDir) throws SoundPackageException {

    if (packageFile == null) {
        throw new SoundPackageException(new IllegalArgumentException("missing package file"));
    }

    ZipFile packageZip = null;
    try {
        packageZip = new ZipFile(packageFile);
        Enumeration<?> entries = packageZip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String entryName = entry.getName();
            if (log.isDebugEnabled()) {
                log.debug("ZipEntry name: " + entryName);
            }
            if (entry.isDirectory()) {
                File dir = new File(tempDir, entryName);
                if (dir.mkdirs())
                    log.info("successfully created dir: " + dir.getAbsolutePath());
            } else {
                FileUtil.createFileFromInputStream(getPackageEntryStream(packageFile, entryName),
                        tempDir + File.separator + entryName);
            }
        }
    } catch (FileNotFoundException ex) {
        throw new SoundPackageException(ex);
    } catch (IOException ex) {
        throw new SoundPackageException(ex);
    } finally {
        try {
            if (packageZip != null)
                packageZip.close();
        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
        }
    }
}

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

public static void backupExtract(String zipLocation, String outputLocation) {
    Logger.logInfo("Extracting (Backup way)");
    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;/* ww  w.j av  a  2  s .c  o m*/
    ZipEntry ze;
    try {
        File folder = new File(outputLocation);
        if (!folder.exists()) {
            folder.mkdir();
        }
        zis = new ZipInputStream(new FileInputStream(zipLocation));
        ze = zis.getNextEntry();
        while (ze != null) {
            File newFile = new File(outputLocation, ze.getName());
            newFile.getParentFile().mkdirs();
            if (!ze.isDirectory()) {
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.flush();
                fos.close();
            }
            ze = zis.getNextEntry();
        }
    } catch (IOException ex) {
        Logger.logError("Error while extracting zip", ex);
    } finally {
        try {
            zis.closeEntry();
            zis.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.sangupta.jerry.util.ZipUtils.java

/**
 * Extract the given ZIP file into the given destination folder.
 * /*from   ww w.j av  a2  s  .c om*/
 * @param zipFile
 *            file to extract
 *            
 * @param baseFolder
 *            destination folder to extract in
 */
public static void extractZipToFolder(File zipFile, File baseFolder) {
    try {
        byte[] buf = new byte[1024];

        ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipentry = zipinputstream.getNextEntry();

        while (zipentry != null) {
            // for each entry to be extracted
            String entryName = zipentry.getName();

            entryName = entryName.replace('/', File.separatorChar);
            entryName = entryName.replace('\\', File.separatorChar);

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

            fileoutputstream = new FileOutputStream(newFile);
            while ((numBytes = zipinputstream.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, numBytes);
            }

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

        zipinputstream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public static InputStream getShapefileFromCompressed(InputStream is, ShpFileType type) {
    InputStream shapefile = null;
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry ze;
    try {/*from ww w .  j  av a 2  s.  c  om*/
        while ((ze = zis.getNextEntry()) != null) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                String baseShapefileName = type.toBase(fileName);
                if (baseShapefileName != null) {
                    shapefile = zis;
                    break;
                }
            }
        }
    } catch (IOException e) {
        try {
            zis.closeEntry();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return shapefile;
}

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

public static byte[] getShapeDataFromZip(byte[] localSourceData) {
    byte[] data = null;
    InputStream in = new ByteArrayInputStream(localSourceData);
    ZipInputStream zis = new ZipInputStream(in);
    ZipEntry ze;
    try {/*from ww w  . ja  v a  2s.  co  m*/
        while ((ze = zis.getNextEntry()) != null) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                if (fileName.endsWith(".shp")) {
                    long entrySize = ze.getSize();
                    if (entrySize != -1) {
                        data = IOUtils.toByteArray(zis, entrySize);
                        break;
                    }
                }
            }
        }
    } 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 data;
}

From source file:org.apache.asterix.event.service.AsterixEventServiceUtil.java

public static void unzip(String sourceFile, String destDir) throws IOException {
    final FileInputStream fis = new FileInputStream(sourceFile);
    final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    final File destDirFile = new File(destDir);
    final byte[] data = new byte[BUFFER_SIZE];

    ZipEntry entry;
    Set<String> visitedDirs = new HashSet<>();
    createDir(destDir);//from w  ww.  j  a  v a2s .  com
    while ((entry = zis.getNextEntry()) != null) {
        createDir(destDirFile, entry, visitedDirs);
        if (entry.isDirectory()) {
            continue;
        }
        int count;

        // write the file to the disk
        File dst = new File(destDir, entry.getName());
        FileOutputStream fos = new FileOutputStream(dst);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        // close the output streams
        dest.flush();
        dest.close();
    }

    zis.close();
}

From source file:com.glaf.core.util.ZipUtils.java

public static void unzip(java.io.InputStream inputStream, String dir) throws Exception {
    File file = new File(dir);
    FileUtils.mkdirsWithExistsCheck(file);
    ZipInputStream zipInputStream = null;
    FileOutputStream fileoutputstream = null;
    BufferedOutputStream bufferedoutputstream = null;
    try {/*from w  w w. j  a v a 2s  . c  o m*/
        zipInputStream = new ZipInputStream(inputStream);

        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 + sp + 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(zipInputStream);
        IOUtils.closeStream(fileoutputstream);
        IOUtils.closeStream(bufferedoutputstream);
    }
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

public static void unzip(String sourceFile, String destDir) throws IOException {
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(sourceFile);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry = null;

    int BUFFER_SIZE = 4096;
    while ((entry = zis.getNextEntry()) != null) {
        String dst = destDir + File.separator + entry.getName();
        if (entry.isDirectory()) {
            createDir(destDir, entry);/*from w ww . j a va  2 s  . c  o  m*/
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];

        // write the file to the disk
        FileOutputStream fos = new FileOutputStream(dst);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        // close the output streams
        dest.flush();
        dest.close();
    }

    zis.close();
}

From source file:com.glaf.core.util.ZipUtils.java

public static void unzip(java.io.InputStream inputStream, String path, List<String> excludes) throws Exception {
    File file = new File(path);
    FileUtils.mkdirsWithExistsCheck(file);
    ZipInputStream zipInputStream = null;
    FileOutputStream fileoutputstream = null;
    BufferedOutputStream bufferedoutputstream = null;
    try {/*  www . j  a  v  a 2s.  c om*/
        zipInputStream = new ZipInputStream(inputStream);
        java.util.zip.ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            boolean isDirectory = zipEntry.isDirectory();
            byte abyte0[] = new byte[BUFFER];
            String s1 = zipEntry.getName();
            String ext = FileUtils.getFileExt(s1);
            if (excludes.contains(ext) || excludes.contains(ext.toLowerCase())) {
                continue;
            }

            s1 = convertEncoding(s1);

            String s2 = path + sp + 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(zipInputStream);
        IOUtils.closeStream(fileoutputstream);
        IOUtils.closeStream(bufferedoutputstream);
    }
}

From source file:org.apache.synapse.libraries.util.LibDeployerUtils.java

private static void extract(String sourcePath, String destPath) throws IOException {
    Enumeration entries;//ww  w .  j a v  a 2 s .  c  om
    ZipFile zipFile;

    zipFile = new ZipFile(sourcePath);
    entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        // we don't need to copy the META-INF dir
        if (entry.getName().startsWith("META-INF/")) {
            continue;
        }
        // if the entry is a directory, create a new dir
        if (entry.isDirectory()) {
            createDir(destPath + entry.getName());
            continue;
        }
        // if the entry is a file, write the file
        copyInputStream(zipFile.getInputStream(entry),
                new BufferedOutputStream(new FileOutputStream(destPath + entry.getName())));
    }
    zipFile.close();
}