Example usage for java.util.zip ZipEntry getName

List of usage examples for java.util.zip ZipEntry getName

Introduction

In this page you can find the example usage for java.util.zip ZipEntry getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the entry.

Usage

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

public static void unzip(File zipFile) {
    FileInputStream fileInputStream = null;
    ZipInputStream zipInputStream = null;
    FileOutputStream fileoutputstream = null;
    BufferedOutputStream bufferedoutputstream = null;
    try {/* w w  w  .  jav  a  2 s .  co m*/
        fileInputStream = new FileInputStream(zipFile);
        zipInputStream = new ZipInputStream(fileInputStream);
        java.util.zip.ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            byte abyte0[] = new byte[BUFFER];
            fileoutputstream = new FileOutputStream(zipEntry.getName());
            bufferedoutputstream = new BufferedOutputStream(fileoutputstream, BUFFER);
            int i;
            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:net.rptools.lib.FileUtil.java

public static void unzip(ZipInputStream in, File destDir) throws IOException {
    if (in == null)
        throw new IOException("input stream cannot be null");

    // Prepare destination
    destDir.mkdirs();/* w  w  w.ja va 2 s  .  c  o m*/
    File absDestDir = destDir.getAbsoluteFile();

    // Pull out the files
    OutputStream out = null;
    ZipEntry entry = null;
    try {
        while ((entry = in.getNextEntry()) != null) {
            if (entry.isDirectory())
                continue;

            // Prepare file destination
            File entryFile = new File(absDestDir, entry.getName());
            entryFile.getParentFile().mkdirs();

            out = new FileOutputStream(entryFile);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(out);
            in.closeEntry();
        }
    } finally {
        IOUtils.closeQuietly(out);
    }
}

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

private static void unzip2(File zipFile, Resource targetDir) throws IOException {
    ZipFile zf = null;// w ww.  j  a  va 2  s . c o  m
    try {
        zf = new ZipFile(zipFile);

        ZipEntry entry;
        Enumeration en = zf.entries();
        while (en.hasMoreElements()) {
            entry = (ZipEntry) en.nextElement();
            Resource target = targetDir.getRealResource(entry.getName());
            if (entry.isDirectory()) {
                target.mkdirs();
            } else {
                Resource parent = target.getParentResource();
                if (!parent.exists())
                    parent.mkdirs();
                InputStream is = zf.getInputStream(entry);
                IOUtil.copy(is, target, true);
            }
            target.setLastModified(entry.getTime());
        }
    } finally {
        IOUtil.closeEL(zf);
    }
}

From source file:com.serotonin.m2m2.Main.java

private static void openZipFiles() throws Exception {
    ProcessEPoll pep = new ProcessEPoll();
    try {/*from   w  w w  .j av a  2s .  c  o  m*/
        new Thread(pep).start();

        File[] zipFiles = new File(new StringBuilder().append(Common.MA_HOME).append("/web/modules").toString())
                .listFiles(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        return name.endsWith(".zip");
                    }
                });
        if ((zipFiles == null) || (zipFiles.length == 0))
            return;
        for (File file : zipFiles) {
            if (!file.isFile()) {
                continue;
            }
            ZipFile zip = new ZipFile(file);
            try {
                Properties props = getProperties(zip);

                String moduleName = props.getProperty("name");
                if (moduleName == null) {
                    throw new RuntimeException("name not defined in module properties");
                }
                if (!ModuleUtils.validateName(moduleName)) {
                    throw new RuntimeException(new StringBuilder().append("Module name '").append(moduleName)
                            .append("' is invalid").toString());
                }
                File moduleDir = new File(
                        new StringBuilder().append(Common.MA_HOME).append("/web/modules").toString(),
                        moduleName);

                if (moduleDir.exists())
                    LOG.info(new StringBuilder().append("Upgrading module ").append(moduleName).toString());
                else {
                    LOG.info(new StringBuilder().append("Installing module ").append(moduleName).toString());
                }
                String persistDirs = props.getProperty("persistPaths");
                File moduleSaveDir = new File(new StringBuilder().append(moduleDir).append(".save").toString());

                if (!org.apache.commons.lang3.StringUtils.isBlank(persistDirs)) {
                    String[] paths = persistDirs.split(",");
                    for (String path : paths) {
                        path = path.trim();
                        if (!org.apache.commons.lang3.StringUtils.isBlank(path)) {
                            File from = new File(moduleDir, path);
                            File to = new File(moduleSaveDir, path);
                            if (from.exists()) {
                                if (from.isDirectory())
                                    moveDir(from, to);
                                else {
                                    FileUtils.moveFile(from, to);
                                }
                            }
                        }
                    }
                }

                deleteDir(moduleDir);

                if (moduleSaveDir.exists())
                    moveDir(moduleSaveDir, moduleDir);
                else {
                    moduleDir.mkdirs();
                }
                Enumeration entries = zip.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) entries.nextElement();
                    String name = entry.getName();
                    File entryFile = new File(moduleDir, name);

                    if (entry.isDirectory())
                        entryFile.mkdirs();
                    else {
                        writeFile(entryFile, zip.getInputStream(entry));
                    }
                }

                File moduleWorkDir = new File(moduleDir, "work");
                if (moduleWorkDir.exists()) {
                    moveDir(moduleWorkDir, new File(Common.MA_HOME, "work"));
                }

                if (HostUtils.isLinux()) {
                    String permissions = props.getProperty("permissions");
                    if (!org.apache.commons.lang3.StringUtils.isBlank(permissions)) {
                        String[] s = permissions.split(",");
                        for (String permission : s) {
                            setPermission(pep, moduleName, moduleDir, permission);
                        }

                    }

                }

                zip.close();
            } catch (Exception e) {
                LOG.warn(new StringBuilder().append("Error while opening zip file ").append(file.getName())
                        .append(". Is this module built for the core version that you are using? Module ignored.")
                        .toString(), e);
            } finally {
                zip.close();
            }

            file.delete();
        }

    } finally {
        pep.waitForAll();
        pep.terminate();
    }
}

From source file:com.gisgraphy.importer.ImporterHelper.java

/**
 * unzip a file in the same directory as the zipped file
 * /*from  w  w  w .  j  a  va  2s . co  m*/
 * @param file
 *            The file to unzip
 */
public static void unzipFile(File file) {
    logger.info("will Extracting file: " + file.getName());
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile;

    try {
        zipFile = new ZipFile(file);

        entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();

            if (entry.isDirectory()) {
                // Assume directories are stored parents first then
                // children.
                (new File(entry.getName())).mkdir();
                continue;
            }

            logger.info("Extracting file: " + entry.getName() + " to " + file.getParent() + File.separator
                    + entry.getName());
            copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(file.getParent() + File.separator + entry.getName())));
        }

        zipFile.close();
    } catch (IOException e) {
        logger.error("can not unzip " + file.getName() + " : " + e.getMessage(), e);
        throw new ImporterException(e);
    }
}

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

public static String getShapefileNameFromCompressed(InputStream is) {
    String shapefileName = null;/*from  w ww .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.vvote.verifierlibrary.utils.io.IOUtils.java

/**
 * Read in the zip file and store each entry so it can be looked up later
 * /*from  w  w  w . j  a  v a2  s  .com*/
 * @param filepath
 * @return the list of all files within the provided zip file
 * @throws IOException
 * @throws FileNotFoundException
 */
public static List<String> readZipFileNames(String filepath) throws FileNotFoundException, IOException {

    logger.debug("Reading zip file contents: {}", filepath);

    if (IOUtils.checkExtension(FileType.ZIP, filepath)) {
        List<String> zipFiles = new ArrayList<String>();

        ZipEntry zipEntry = null;

        try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(filepath))) {

            // loop over each file
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {

                // add the filename
                zipFiles.add(zipEntry.getName());
            }
        }

        logger.debug("Successfully read the zip file: {}", filepath);
        return zipFiles;
    }

    logger.error("Provided filepath: {} does not point to a valid zip file", filepath);
    throw new IOException("Provided filepath: " + filepath + " does not point to a valid zip file");
}

From source file:com.magnet.tools.tests.ScenarioUtils.java

/**
 * Utility method to unzip a file/*from w ww .  java  2 s.co  m*/
 *
 * @param file      file to unzip
 * @param outputDir destination directory
 * @throws IOException if an io exception occurs
 */
public static void unzip(File file, File outputDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                if (!entryDestination.mkdirs()) {
                    throw new IllegalStateException("Cannot create directory " + entryDestination);
                }
                continue;
            }
            if (entryDestination.getParentFile().mkdirs()) {
                throw new IllegalStateException("Cannot create directory " + entryDestination.getParentFile());
            }
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    } finally {
        if (null != zipFile) {
            try {
                zipFile.close();
            } catch (Exception e) {
                /* do nothing */
            }
        }
    }
}

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 ww  w. ja  va 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();

            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: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 {/*from ww 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();
            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);
    }
}