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:edu.isi.wings.portal.classes.StorageHandler.java

public static String unzipFile(File f, String todirname, String toDirectory) {
    File todir = new File(toDirectory);
    if (!todir.exists())
        todir.mkdirs();//from   w  ww .j  a va  2s .  co m

    try {
        // Check if the zip file contains only one directory
        ZipFile zfile = new ZipFile(f);
        String topDir = null;
        boolean isOneDir = true;
        for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements();) {
            ZipEntry ze = e.nextElement();
            String name = ze.getName().replaceAll("/.+$", "");
            name = name.replaceAll("/$", "");
            // OSX Zips carry an extra __MACOSX directory. Ignore it
            if (name.equals("__MACOSX"))
                continue;

            if (topDir == null)
                topDir = name;
            else if (!topDir.equals(name)) {
                isOneDir = false;
                break;
            }
        }
        zfile.close();

        // Delete existing directory (if any)
        FileUtils.deleteDirectory(new File(toDirectory + File.separator + todirname));

        // Unzip file(s) into toDirectory/todirname
        ZipInputStream zis = new ZipInputStream(new FileInputStream(f));
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            // OSX Zips carry an extra __MACOSX directory. Ignore it
            if (fileName.startsWith("__MACOSX")) {
                ze = zis.getNextEntry();
                continue;
            }

            // Get relative file path translated to 'todirname'
            if (isOneDir)
                fileName = fileName.replaceFirst(topDir, todirname);
            else
                fileName = todirname + File.separator + fileName;

            // Create directories
            File newFile = new File(toDirectory + File.separator + fileName);
            if (ze.isDirectory())
                newFile.mkdirs();
            else
                newFile.getParentFile().mkdirs();

            try {
                // Copy file
                FileOutputStream fos = new FileOutputStream(newFile);
                IOUtils.copy(zis, fos);
                fos.close();

                String mime = new Tika().detect(newFile);
                if (mime.equals("application/x-sh") || mime.startsWith("text/"))
                    FileUtils.writeLines(newFile, FileUtils.readLines(newFile));

                // Set all files as executable for now
                newFile.setExecutable(true);
            } catch (FileNotFoundException fe) {
                // Silently ignore
                //fe.printStackTrace();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();

        return toDirectory + File.separator + todirname;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

public static List<String> getDirsList(OmniFile zipOmni) {

    ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream());

    List<String> arrayList = new ArrayList<>();

    ZipEntry entry = null;
    try {/*from ww w.ja  va  2  s  .c om*/
        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory())
                arrayList.add(entry.getName());
        }
        zis.close();

    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
    }

    return arrayList;
}

From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Un zip a file to a destination folder.
 * @param zipFile the zip file//from ww  w . ja v  a  2  s.  c om
 * @param outputFolder the output folder
 */
public static void unZipFile(final String zipFile, final String outputFolder) {

    LogUtil.setLogString("UnZip the File", true);
    final byte[] buffer = new byte[1024];
    int len;
    try {
        // create output directory is not exists
        final File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }
        // get the zip file content
        final ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        final StringBuilder outFolderPath = new StringBuilder();
        final StringBuilder fileLogPath = new StringBuilder();
        ZipEntry zipEntry;
        while ((zipEntry = zis.getNextEntry()) != null) {
            final String fileName = zipEntry.getName();
            final File newFile = new File(
                    outFolderPath.append(outputFolder).append(File.separator).append(fileName).toString());
            LogUtil.setLogString(
                    fileLogPath.append("file unzip : ").append(newFile.getAbsoluteFile()).toString(), true);
            // create all non exists folders
            // else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();
            final FileOutputStream fos = new FileOutputStream(newFile);

            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            fileLogPath.setLength(0);
            outFolderPath.setLength(0);
        }

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

        LogUtil.setLogString("Done", true);

    } catch (IOException ex) {
        LOGGER.error("Error in unzip file", ex);
    }
}

From source file:ee.ria.xroad.common.asic.AsicHelper.java

static AsicContainer read(InputStream is) throws Exception {
    Map<String, String> entries = new HashMap<>();
    ZipInputStream zip = new ZipInputStream(is);
    ZipEntry zipEntry;

    while ((zipEntry = zip.getNextEntry()) != null) {
        for (Object expectedEntry : AsicContainerEntries.getALL_ENTRIES()) {
            if (matches(expectedEntry, zipEntry.getName())) {
                String data;/*from   www .  ja va2  s . c o  m*/

                if (ENTRY_TIMESTAMP.equalsIgnoreCase(zipEntry.getName())) {
                    data = encodeBase64(getBinaryData(zip));
                } else {
                    data = getData(zip);
                }

                entries.put(zipEntry.getName(), data);

                break;
            }
        }
    }

    return new AsicContainer(entries);
}

From source file:Utils.java

/**
 * unpack a segment from a zip/*from   w  ww  . ja v a  2  s.  c o  m*/
 * 
 * @param addsi
 * @param packetStream
 * @param version
 */
public static void unpack(InputStream source, File destination) throws IOException {
    ZipInputStream zin = new ZipInputStream(source);
    ZipEntry zipEntry = null;
    FileOutputStream fout = null;

    byte[] buffer = new byte[4096];
    while ((zipEntry = zin.getNextEntry()) != null) {

        long ts = zipEntry.getTime();
        // the zip entry needs to be a full path from the
        // searchIndexDirectory... hence this is correct

        File f = new File(destination, zipEntry.getName());

        f.getParentFile().mkdirs();

        fout = new FileOutputStream(f);
        int len;
        while ((len = zin.read(buffer)) > 0) {
            fout.write(buffer, 0, len);
        }
        zin.closeEntry();
        fout.close();
        f.setLastModified(ts);
    }
    fout.close();
}

From source file:be.fedict.eid.applet.service.signer.odf.ODFUtil.java

/**
 * Checks if a file / zip entry is a content file
 * /*from   w w w  .j a  v  a  2 s  .  c om*/
 * @param zipEntry
 * @return true if zip entry is a content file
 */
public static boolean isContentFile(ZipEntry zipEntry) {
    /*
     * should be enough for most simple ODF files and ODF embedded into
     * another ODF file (e.g. chart in spreadsheet)
     */
    return zipEntry.getName().endsWith("content.xml");
}

From source file:be.fedict.eid.applet.service.signer.odf.ODFUtil.java

/**
 * Checks if a file / zip entry is a signature file
 * //from  w  w  w.j  a  v  a 2 s .  co  m
 * @param zipEntry
 * @return true if zip entry is a signature file
 */
public static boolean isSignatureFile(ZipEntry zipEntry) {
    /*
     * for now, only check for document signatures, not the other macro
     * signatures or application specific signatures
     */
    return zipEntry.getName().equals(SIGNATURE_FILE);
}

From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java

private static void extractEntry(final ZipFile zf, final ZipEntry entry, final File destDir)
        throws IOException {
    File file = new File(destDir, entry.getName());

    if (entry.isDirectory()) {
        file.mkdirs();/*  w w  w.java2  s  . com*/
    } else {
        new File(file.getParent()).mkdirs();

        try (InputStream is = zf.getInputStream(entry); FileOutputStream os = new FileOutputStream(file)) {
            copy(Channels.newChannel(is), os.getChannel());
        }
        // preserve modification time; must be set after the stream is closed
        file.setLastModified(entry.getTime());
    }
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java

/**
 * unzip CSAR packge//from w  w w.j  a v  a  2s. c o m
 * @param fileName filePath
 * @return
 */
public static int unzipCSAR(String fileName, String filePath) {
    final int BUFFER = 2048;
    int status = 0;

    try {
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration emu = zipFile.entries();
        int i = 0;
        while (emu.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) emu.nextElement();
            //read directory as file first,so only need to create directory
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            //Because that is random to read zipfile,maybe the file is read first
            //before the directory is read,so we need to create directory first.
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bos.flush();
            bos.close();
            bis.close();

            if (entry.getName().endsWith(".zip")) {
                File subFile = new File(filePath + entry.getName());
                if (subFile.exists()) {
                    int subStatus = unzipCSAR(filePath + entry.getName(), subFile.getParent() + "/");
                    if (subStatus != 0) {
                        LOG.error("sub file unzip fail!" + subFile.getName());
                        status = Constant.UNZIP_FAIL;
                        return status;
                    }
                }
            }
        }
        status = Constant.UNZIP_SUCCESS;
        zipFile.close();
    } catch (Exception e) {
        status = Constant.UNZIP_FAIL;
        e.printStackTrace();
    }
    return status;
}

From source file:fr.inria.atlanmod.neoemf.benchmarks.datastore.helper.BackendHelper.java

private static File extractEntryFromZip(ZipInputStream inputStream, ZipEntry entry, Path outputDir)
        throws IOException {
    File outputFile = outputDir.resolve(new File(entry.getName()).getName()).toFile();
    if (outputFile.exists()) {
        log.info("Already extracted resource {}", outputFile);
        return outputFile;
    }//  www .j  a v  a2  s.co  m
    IOUtils.copy(inputStream, new FileOutputStream(outputFile));
    return outputFile;
}