Example usage for java.util.zip ZipFile entries

List of usage examples for java.util.zip ZipFile entries

Introduction

In this page you can find the example usage for java.util.zip ZipFile entries.

Prototype

public Enumeration<? extends ZipEntry> entries() 

Source Link

Document

Returns an enumeration of the ZIP file entries.

Usage

From source file:com.samczsun.helios.LoadedFile.java

public void reset() throws IOException {
    files.clear();//from   w w w.jav a2s. co  m
    classes.clear();
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
        while (enumeration.hasMoreElements()) {
            ZipEntry zipEntry = enumeration.nextElement();
            if (!zipEntry.isDirectory()) {
                load(zipEntry.getName(), zipFile.getInputStream(zipEntry));
            }
        }
    } catch (ZipException e) { //Probably not a ZIP file
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            load(this.name, inputStream);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:de.shadowhunt.subversion.internal.AbstractHelper.java

private void extractArchive(final File zip, final File prefix) throws Exception {
    final ZipFile zipFile = new ZipFile(zip);
    final Enumeration<? extends ZipEntry> enu = zipFile.entries();
    while (enu.hasMoreElements()) {
        final ZipEntry zipEntry = enu.nextElement();

        final String name = zipEntry.getName();

        final File file = new File(prefix, name);
        if (name.charAt(name.length() - 1) == Resource.SEPARATOR_CHAR) {
            if (!file.isDirectory() && !file.mkdirs()) {
                throw new IOException("can not create directory structure: " + file);
            }/*from  w w  w .j  a v  a  2 s  .co  m*/
            continue;
        }

        final File parent = file.getParentFile();
        if (parent != null) {
            if (!parent.isDirectory() && !parent.mkdirs()) {
                throw new IOException("can not create directory structure: " + parent);
            }
        }

        try (final InputStream is = zipFile.getInputStream(zipEntry)) {
            try (final OutputStream os = new FileOutputStream(file)) {
                IOUtils.copy(is, os);
            }
        }
    }
    zipFile.close();
}

From source file:gov.nih.nci.caarray.application.fileaccess.FileAccessUtils.java

/**
 * Unzips a .zip file, removes it from <code>files</code> and adds the extracted files to <code>files</code>.
 * /*from  ww  w  .  j  a  v a  2s  .  c o m*/
 * @param files the list of files to unzip and the files extracted from the zips
 * @param fileNames the list of filenames to go along with the list of files
 */
@SuppressWarnings("PMD.ExcessiveMethodLength")
public void unzipFiles(List<File> files, List<String> fileNames) {
    try {
        final Pattern p = Pattern.compile("\\.zip$");
        int index = 0;
        for (int i = 0; i < fileNames.size(); i++) {
            final Matcher m = p.matcher(fileNames.get(i).toLowerCase());

            if (m.find()) {
                final File file = files.get(i);
                final String fileName = file.getAbsolutePath();
                final String directoryPath = file.getParent();
                final ZipFile zipFile = new ZipFile(fileName);
                final Enumeration<? extends ZipEntry> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    final ZipEntry entry = entries.nextElement();
                    final File entryFile = new File(directoryPath + "/" + entry.getName());

                    final InputStream fileInputStream = zipFile.getInputStream(entry);
                    final FileOutputStream fileOutputStream = new FileOutputStream(entryFile);
                    IOUtils.copy(fileInputStream, fileOutputStream);
                    IOUtils.closeQuietly(fileOutputStream);

                    files.add(entryFile);
                    fileNames.add(entry.getName());
                }
                zipFile.close();
                files.remove(index);
                fileNames.remove(index);
            }
            index++;
        }
    } catch (final IOException e) {
        throw new FileAccessException("Couldn't unzip archive.", e);
    }
}

From source file:com.phonegap.plugin.files.ExtractZipFilePlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext cbc) {
    PluginResult.Status status = PluginResult.Status.OK;
    try {/*  www . j ava 2 s.c  o  m*/
        String filename = args.getString(0);
        URL url;
        try {
            url = new URL(filename);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        File file = new File(url.getFile());
        String[] dirToSplit = url.getFile().split(File.separator);
        String dirToInsert = "";
        for (int i = 0; i < dirToSplit.length - 1; i++) {
            dirToInsert += dirToSplit[i] + File.separator;
        }

        ZipEntry entry;
        ZipFile zipfile;
        try {
            zipfile = new ZipFile(file);
            Enumeration e = zipfile.entries();
            while (e.hasMoreElements()) {
                entry = (ZipEntry) e.nextElement();
                BufferedInputStream is = null;
                try {
                    is = new BufferedInputStream(zipfile.getInputStream(entry));
                    int count;
                    byte data[] = new byte[102222];
                    String fileName = dirToInsert + entry.getName();
                    File outFile = new File(fileName);
                    if (entry.isDirectory()) {
                        if (!outFile.exists() && !outFile.mkdirs()) {
                            Log.v("info", "Unable to create directories: " + outFile.getAbsolutePath());
                            cbc.sendPluginResult(new PluginResult(IO_EXCEPTION));
                            return false;
                        }
                    } else {
                        File parent = outFile.getParentFile();
                        if (parent.mkdirs()) {
                            Log.v("info", "created directory leading to file");
                        }
                        FileOutputStream fos = null;
                        BufferedOutputStream dest = null;
                        try {
                            fos = new FileOutputStream(outFile);
                            dest = new BufferedOutputStream(fos, 102222);
                            while ((count = is.read(data, 0, 102222)) != -1) {
                                dest.write(data, 0, count);
                            }
                        } finally {
                            if (dest != null) {
                                dest.flush();
                                dest.close();
                            }
                            if (fos != null) {
                                fos.close();
                            }
                        }
                    }
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
            }
        } catch (ZipException e1) {
            Log.v("error", e1.getMessage(), e1);
            cbc.sendPluginResult(new PluginResult(MALFORMED_URL_EXCEPTION));
            return false;
        } catch (IOException e1) {
            Log.v("error", e1.getMessage(), e1);
            cbc.sendPluginResult(new PluginResult(IO_EXCEPTION));
            return false;
        }

    } catch (JSONException e) {
        cbc.sendPluginResult(new PluginResult(JSON_EXCEPTION));
        return false;
    }
    cbc.sendPluginResult(new PluginResult(status));
    return true;
}

From source file:org.dbgl.util.FileUtils.java

public static String[] getExecutablesInZipOrIsoOrFat(final String archive) throws IOException {
    List<String> result = new ArrayList<String>();
    File arcFile = new File(archive);

    if (archive.toLowerCase().endsWith(ARCHIVES[0])) { // zip
        ZipFile zfile = new ZipFile(arcFile);
        for (Enumeration<? extends ZipEntry> entries = zfile.entries(); entries.hasMoreElements();) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (!entry.isDirectory() && isExecutable(name)) {
                result.add(PlatformUtils.archiveToNativePath(name));
            }/*from  w ww .j  av  a2s. co m*/
        }
        zfile.close();
    } else if (archive.toLowerCase().endsWith(ARCHIVES[1])) { // 7-zip
        MyRandomAccessFile istream = new MyRandomAccessFile(archive, "r");
        IInArchive zArchive = new Handler();
        if (zArchive.Open(istream) != 0) {
            throw new IOException(
                    Settings.getInstance().msg("general.error.opensevenzip", new Object[] { archive }));
        }
        for (int i = 0; i < zArchive.size(); i++) {
            SevenZipEntry entry = zArchive.getEntry(i);
            String name = entry.getName();
            if (!entry.isDirectory() && isExecutable(name)) {
                result.add(PlatformUtils.archiveToNativePath(name));
            }
        }
        zArchive.close();
    } else if (isIsoFile(archive)) {
        ISO9660FileSystem iso = new ISO9660FileSystem(new File(archive));
        for (Enumeration<ISO9660FileEntry> entries = iso.getEntries(); entries.hasMoreElements();) {
            ISO9660FileEntry entry = entries.nextElement();
            String name = entry.getPath();
            if (!entry.isDirectory() && isExecutable(name)) {
                result.add(PlatformUtils.archiveToNativePath(name));
            }
        }
        iso.close();
    } else if (isFatImage(archive)) {
        BlockDevice device = new FileDisk(new File(archive));
        result.addAll(readFatEntries(new FatFileSystem(device).getRoot(), ""));
        device.close();
    }

    Collections.sort(result, new FilenameComparator());
    return result.toArray(new String[result.size()]);
}

From source file:com.denimgroup.threadfix.importer.impl.upload.ClangChannelImporter.java

private Integer getFindingCount(ZipFile zipFile) {
    if (zipFile.entries() == null) {
        return null;
    }//  w ww .j a  v  a 2s . c o m
    int i = 0;
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.getName().matches(REGEX_REPORT_FILE)) {
            i++;
        }
    }
    return i;
}

From source file:ch.entwine.weblounge.maven.MyGengoI18nImport.java

/**
 * {@inheritDoc}// w  w w.j a  v a  2s. co  m
 * 
 * @see org.apache.maven.plugin.Mojo#execute()
 */
public void execute() throws MojoExecutionException, MojoFailureException {
    log.info("Start importing i18n files from myGengo.");
    URLConnection conn = getMyGengoConn();
    ZipFile zip = extractZipFileFromConn(conn);

    if (zip != null) {
        Enumeration<? extends ZipEntry> entries = zip.entries();
        ZipEntry entry;
        while (entries.hasMoreElements()) {
            try {
                entry = entries.nextElement();
                importLanguageFile(entry.getName(), zip.getInputStream(entry));
            } catch (FileNotFoundException e) {
                log.error("Error while reading zip file.");
            } catch (IOException e) {
                log.error("Error while reading zip file.");
            }
        }
    }
    log.info("Importing i18n files from myGengo finished.");
}

From source file:org.kuali.rice.kew.plugin.ZipFilePluginLoader.java

/**
 * Extracts the plugin files if necessary.
 *///  w  w w.j a  va  2 s.  com
protected void extractPluginFiles() throws Exception {
    if (isExtractNeeded()) {
        // first, delete the current copy of the extracted plugin
        if (extractionDirectory.exists()) {
            // TODO how to handle locked files under windows?!?  This will throw an IOException in this case.
            FileUtils.deleteDirectory(extractionDirectory);
        }
        if (!extractionDirectory.mkdir()) {
            throw new WorkflowRuntimeException("Could not create the extraction directory for the plugin: "
                    + extractionDirectory.getAbsolutePath());
        }
        ZipFile zipFile = new ZipFile(pluginZipFile, ZipFile.OPEN_READ);
        for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            File entryFile = new File(extractionDirectory + java.io.File.separator + entry.getName());
            if (entry.isDirectory()) { // if its a directory, create it
                if (!entryFile.mkdir()) {
                    throw new WorkflowRuntimeException(
                            "Failed to create directory: " + entryFile.getAbsolutePath());
                }
                continue;
            }
            InputStream is = null;
            OutputStream os = null;
            try {
                is = new BufferedInputStream(zipFile.getInputStream(entry)); // get the input stream
                os = new BufferedOutputStream(new FileOutputStream(entryFile));
                while (is.available() > 0) { // write contents of 'is' to 'fos'
                    os.write(is.read());
                }
            } finally {
                if (os != null) {
                    os.close();
                }
                if (is != null) {
                    is.close();
                }
            }
        }
    }
}

From source file:org.ambraproject.article.service.XslIngestArchiveProcessor.java

/**
 * Generate a description of the given zip archive.
 *
 * @param zip the zip archive to describe
 * @return the xml doc describing the archive (adheres to zip.dtd)
 * @throws IOException if an exception occurred reading the zip archive
 *//*from  w w  w.  jav  a  2 s .c om*/
public static String describeZip(ZipFile zip) throws IOException {
    StringBuilder res = new StringBuilder(500);
    res.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    res.append("<ZipInfo");
    if (zip.getName() != null)
        res.append(" name=\"").append(attrEscape(zip.getName())).append("\"");
    res.append(">\n");

    Enumeration<? extends ZipEntry> entries = zip.entries();
    while (entries.hasMoreElements())
        entry2xml(entries.nextElement(), res);

    res.append("</ZipInfo>\n");
    return res.toString();
}

From source file:cpw.mods.fml.server.FMLServerHandler.java

private void searchZipForENUSLanguage(File source) throws IOException {
    ZipFile zf = new ZipFile(source);
    for (ZipEntry ze : Collections.list(zf.entries())) {
        Matcher matcher = assetENUSLang.matcher(ze.getName());
        if (matcher.matches()) {
            FMLLog.fine("Injecting found translation data in zip file %s at %s into language system",
                    source.getName(), ze.getName());
            StringTranslate.inject(zf.getInputStream(ze));
        }//from  w  w w .j a  va 2 s  .co m
    }
    zf.close();
}