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.ikon.util.ReportUtils.java

/**
 * Get report parameters// w  ww  . j  ava 2  s.co  m
 */
public static List<FormElement> getReportParameters(long rpId)
        throws ParseException, DatabaseException, IOException {
    log.debug("getReportParameters({})", rpId);
    List<FormElement> params = null;
    ByteArrayInputStream bais = null;
    ZipInputStream zis = null;

    try {
        Report rp = ReportDAO.findByPk(rpId);
        bais = new ByteArrayInputStream(SecureStore.b64Decode(rp.getFileContent()));
        zis = new ZipInputStream(bais);
        ZipEntry zi = null;

        while ((zi = zis.getNextEntry()) != null) {
            if ("params.xml".equals(zi.getName())) {
                params = FormUtils.parseReportParameters(zis);
                break;
            }
        }

        if (params == null) {
            params = new ArrayList<FormElement>();
            log.warn("Report '{}' has no params.xml file", rpId);
        }

        // Activity log
        // UserActivity.log(session.getUserID(), "GET_REPORT_PARAMETERS", rpId+"", null);
    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(zis);
        IOUtils.closeQuietly(bais);
    }

    log.debug("getReportParameters: {}", params);
    return params;
}

From source file:com.ibm.jaggr.core.util.ZipUtil.java

/**
 * Extracts the specified zip file to the specified location. If {@code selector} is specified,
 * then only the entry specified by {@code selector} (if {@code selector} is a filename) or the
 * contents of the directory specified by {@code selector} (if {@code selector} is a directory
 * name) will be extracted. If {@code selector} specifies a directory, then the contents of the
 * directory in the zip file will be rooted at {@code destDir} when extracted.
 *
 * @param zipFile//w  w  w. ja v  a  2s. c om
 *            the {@link File} object for the file to unzip
 * @param destDir
 *            the {@link File} object for the target directory
 * @param selector
 *            The name of a file or directory to extract
 * @throws IOException
 */
public static void unzip(File zipFile, File destDir, String selector) throws IOException {
    final String sourceMethod = "unzip"; //$NON-NLS-1$
    final boolean isTraceLogging = log.isLoggable(Level.FINER);
    if (isTraceLogging) {
        log.entering(sourceClass, sourceMethod, new Object[] { zipFile, destDir });
    }

    boolean selectorIsFolder = selector != null && selector.charAt(selector.length() - 1) == '/';
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFile));
    try {
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String entryName = entry.getName();
            if (selector == null || !selectorIsFolder && entryName.equals(selector) || selectorIsFolder
                    && entryName.startsWith(selector) && entryName.length() != selector.length()) {
                if (selector != null) {
                    if (selectorIsFolder) {
                        // selector is a directory.  Strip selected path
                        entryName = entryName.substring(selector.length());
                    } else {
                        // selector is a filename.  Extract the filename portion of the path
                        int idx = entryName.lastIndexOf("/"); //$NON-NLS-1$
                        if (idx != -1) {
                            entryName = entryName.substring(idx + 1);
                        }
                    }
                }
                File file = new File(destDir, entryName.replace("/", File.separator)); //$NON-NLS-1$
                if (!entry.isDirectory()) {
                    // if the entry is a file, extract it
                    extractFile(entry, zipIn, file);
                } else {
                    // if the entry is a directory, make the directory
                    extractDirectory(entry, file);
                }
                zipIn.closeEntry();
            }
            entry = zipIn.getNextEntry();
        }
    } finally {
        zipIn.close();
    }

    if (isTraceLogging) {
        log.exiting(sourceClass, sourceMethod);
    }
}

From source file:com.simiacryptus.mindseye.lang.Layer.java

/**
 * From zip nn key./*from w w  w  .j a va  2  s  .  c  om*/
 *
 * @param zipfile the zipfile
 * @return the nn key
 */
@Nonnull
static Layer fromZip(@Nonnull final ZipFile zipfile) {
    Enumeration<? extends ZipEntry> entries = zipfile.entries();
    @Nullable
    JsonObject json = null;
    @Nonnull
    HashMap<CharSequence, byte[]> resources = new HashMap<>();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        CharSequence name = zipEntry.getName();
        try {
            InputStream inputStream = zipfile.getInputStream(zipEntry);
            if (name.equals("model.json")) {
                json = new GsonBuilder().create().fromJson(new InputStreamReader(inputStream),
                        JsonObject.class);
            } else {
                resources.put(name, IOUtils.readFully(inputStream, (int) zipEntry.getSize()));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return fromJson(json, resources);
}

From source file:com.openkm.util.ReportUtils.java

/**
 * Get report parameters//  ww w .  jav  a2s.  c o  m
 */
public static List<FormElement> getReportParameters(long rpId)
        throws ParseException, DatabaseException, IOException {
    log.debug("getReportParameters({})", rpId);
    long begin = System.currentTimeMillis();
    List<FormElement> params = null;
    ByteArrayInputStream bais = null;
    ZipInputStream zis = null;

    try {
        Report rp = ReportDAO.findByPk(rpId);
        bais = new ByteArrayInputStream(SecureStore.b64Decode(rp.getFileContent()));
        zis = new ZipInputStream(bais);
        ZipEntry zi = null;

        while ((zi = zis.getNextEntry()) != null) {
            if ("params.xml".equals(zi.getName())) {
                params = FormUtils.parseReportParameters(zis);
                break;
            }
        }

        if (params == null) {
            params = new ArrayList<FormElement>();
            log.warn("Report '{}' has no params.xml file", rpId);
        }

        // Activity log
        // UserActivity.log(session.getUserID(), "GET_REPORT_PARAMETERS", rpId+"", null);
    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(zis);
        IOUtils.closeQuietly(bais);
    }

    log.trace("getReportParameters.Time: {}", System.currentTimeMillis() - begin);
    log.debug("getReportParameters: {}", params);
    return params;
}

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

@Then("^the package \"([^\"]*)\" should contain the following entries:$")
public static void package_should_contain(String packagePath, List<String> entries) throws Throwable {
    File file = new File(expandVariables(packagePath));
    Assert.assertTrue("File " + file + " should exist", file.exists());

    ZipInputStream zip = null;//from  www  .j av a  2s . co  m
    Set<String> actualSet;
    try {
        FileInputStream fis = new FileInputStream(file);
        zip = new ZipInputStream(fis);
        ZipEntry ze;
        actualSet = new HashSet<String>();
        while ((ze = zip.getNextEntry()) != null) {
            actualSet.add(ze.getName());
        }
    } finally {
        if (null != zip) {
            zip.close();
        }
    }

    for (String e : entries) {
        String expected = expandVariables(e);
        Assert.assertTrue("File " + file + " should contain entry " + expected + ", actual set of entries are "
                + actualSet, actualSet.contains(e));
    }
}

From source file:azkaban.utils.Utils.java

public static void unzip(ZipFile source, File dest) throws IOException {
    Enumeration<?> entries = source.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File newFile = new File(dest, entry.getName());
        if (entry.isDirectory()) {
            newFile.mkdirs();//from   w  w w  . j ava  2 s. c  om
        } else {
            newFile.getParentFile().mkdirs();
            InputStream src = source.getInputStream(entry);
            try {
                OutputStream output = new BufferedOutputStream(new FileOutputStream(newFile));
                try {
                    IOUtils.copy(src, output);
                } finally {
                    output.close();
                }
            } finally {
                src.close();
            }
        }
    }
}

From source file:com.eviware.soapui.integration.exporter.ProjectExporter.java

public static void unpackageAll(String archive, String path) {
    try {//w  ww  .  j  a  va2  s.c  om
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(archive);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(path + File.separator + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch (Exception e) {
        SoapUI.logError(e);
    }
}

From source file:com.thruzero.common.core.utils.FileUtilsExt.java

public static final boolean unzipArchive(final File fromFile, final File toDir) throws IOException {
    boolean result = false; // assumes error

    logHelper.logBeginUnzip(fromFile, toDir);

    ZipFile zipFile = new ZipFile(fromFile);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    if (!toDir.exists()) {
        toDir.mkdirs();/*from   ww w. j ava 2 s .  c  om*/
        logHelper.logProgressCreatedToDir(toDir);
    }
    logHelper.logProgressToDirIsWritable(toDir);

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

            if (entry.isDirectory()) {
                File dir = new File(
                        toDir.getAbsolutePath() + EnvironmentHelper.FILE_PATH_SEPARATOR + entry.getName());

                if (!dir.exists()) {
                    dir.mkdirs();
                    logHelper.logProgressFilePathCreated(dir);
                }
            } else {
                File fosz = new File(
                        toDir.getAbsolutePath() + EnvironmentHelper.FILE_PATH_SEPARATOR + entry.getName());
                logHelper.logProgressCopyEntry(fosz);

                File parent = fosz.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }

                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fosz));
                IOUtils.copy(zipFile.getInputStream(entry), bos);
                bos.flush();
            }
        }
        zipFile.close();
        result = true; // success
    } else {
        logHelper.logFileWriteError(fromFile, null);
        zipFile.close();
    }

    return result;
}

From source file:gov.va.chir.tagline.dao.FileDao.java

public static TagLineModel loadTagLineModel(final File file) throws Exception {
    final TagLineModel model = new TagLineModel();

    // Unzip each file to temp
    final File temp = new File(System.getProperty("java.io.tmpdir"));

    byte[] buffer = new byte[BUFFER_SIZE];

    final ZipInputStream zis = new ZipInputStream(new FileInputStream(file));

    ZipEntry entry = zis.getNextEntry();

    while (entry != null) {
        final String name = entry.getName();

        File tempFile = new File(temp, name);

        // Write out file
        final FileOutputStream fos = new FileOutputStream(tempFile);
        int len;/*from w  ww  .j av  a 2  s .  co  m*/
        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }

        fos.close();

        // Determine which file was written
        if (name.equalsIgnoreCase(FILENAME_FEATURES)) {
            model.setFeatures(loadFeatures(tempFile));
        } else if (name.equalsIgnoreCase(FILENAME_HEADER)) {
            model.setHeader(loadHeader(tempFile));
        } else if (name.equalsIgnoreCase(FILENAME_MODEL)) {
            model.setModel(loadModel(tempFile));
        } else {
            throw new IllegalStateException(String.format("Unknown file in TagLine model file (%s)", name));
        }

        // Delete temp file
        tempFile.delete();

        // Get next entry
        zis.closeEntry();
        entry = zis.getNextEntry();
    }

    zis.close();

    return model;
}

From source file:com.edgenius.core.util.ZipFileUtil.java

public static void expandZipToFolder(InputStream is, String destFolder) throws ZipFileUtilException {
    // got our directory, so write out the input file and expand the zip file
    // this is really a hack - write it out temporarily then read it back in again! urg!!!!
    ZipInputStream zis = new ZipInputStream(is);
    int count;// www . j a  va 2 s.com
    byte data[] = new byte[BUFFER_SIZE];
    ZipEntry entry = null;
    BufferedOutputStream dest = null;
    String entryName = null;

    try {

        // work through each file, creating a node for each file
        while ((entry = zis.getNextEntry()) != null) {
            entryName = entry.getName();
            if (!entry.isDirectory()) {

                String destName = destFolder + File.separator + entryName;
                //It must sort out the directory information to current OS. 
                //e.g, if zip file is zipped under windows, but unzip in linux.  
                destName = FileUtil.makeCanonicalPath(destName);

                prepareDirectory(destName);

                FileOutputStream fos = new FileOutputStream(destName);
                dest = new BufferedOutputStream(fos, BUFFER_SIZE);
                while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                IOUtils.closeQuietly(dest);
                dest = null;
            } else {
                String destName = destFolder + File.separator + entryName;
                destName = FileUtil.makeCanonicalPath(destName);
                new File(destName).mkdirs();
            }

        }

    } catch (IOException ioe) {

        log.error("Exception occured processing entries in zip file. Entry was " + entryName, ioe);
        throw new ZipFileUtilException(
                "Exception occured processing entries in zip file. Entry was " + entryName, ioe);

    } finally {
        IOUtils.closeQuietly(dest);
        IOUtils.closeQuietly(zis);
    }
}