Example usage for java.util.zip ZipFile getEntry

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

Introduction

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

Prototype

public ZipEntry getEntry(String name) 

Source Link

Document

Returns the zip file entry for the specified name, or null if not found.

Usage

From source file:org.atombeat.xquery.functions.util.StreamZipEntryToResponse.java

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    ResponseModule resModule = (ResponseModule) context.getModule(ResponseModule.NAMESPACE_URI);

    // request object is read from global variable $response
    Variable respVar = resModule.resolveVariable(ResponseModule.RESPONSE_VAR);

    if (respVar == null || respVar.getValue() == null)
        throw new XPathException(this, "No request object found in the current XQuery context.");

    if (respVar.getValue().getItemType() != Type.JAVA_OBJECT)
        throw new XPathException(this, "Variable $response is not bound to an Java object.");

    JavaObjectValue respValue = (JavaObjectValue) respVar.getValue().itemAt(0);

    if (!"org.exist.http.servlets.HttpResponseWrapper".equals(respValue.getObject().getClass().getName()))
        throw new XPathException(this,
                signature.toString() + " can only be used within the EXistServlet or XQueryServlet");

    ResponseWrapper response = (ResponseWrapper) respValue.getObject();

    String path = args[0].getStringValue();
    logger.debug(path);//from   w w  w.j  av a 2s  .  c  o m
    String entry = args[1].getStringValue();
    logger.debug(entry);
    String contentType = args[2].getStringValue();
    logger.debug(contentType);
    response.setContentType(contentType);

    try {

        ZipFile f = new ZipFile(path);
        ZipEntry e = f.getEntry(entry);
        InputStream in = f.getInputStream(e);
        OutputStream out = response.getOutputStream();
        Stream.copy(in, out);
        response.flushBuffer(); // is this necessary?

    } catch (Exception e) {
        e.printStackTrace();
        throw new XPathException(this, "Exception while streaming data from zip file: " + e.getMessage(), e);
    }

    return Sequence.EMPTY_SEQUENCE;
}

From source file:org.paxml.tag.sql.DdlScript.java

public String readContent() {
    File theFile = null;//  w  w w.  j a  va 2 s  .  c o m
    InputStream in = null;
    try {
        if (container.isDirectory()) {
            theFile = new File(container, file);
            in = new FileInputStream(theFile);
        } else {
            // assume it being a zip
            ZipFile zip = new ZipFile(container);
            ZipEntry entry = zip.getEntry(file);
            in = zip.getInputStream(entry);
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(in, out);
        return out.toString("UTF-8");
    } catch (IOException e) {
        throw new PaxmlRuntimeException("Cannot read file content from: " + getFileName(), e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.replaymod.replaystudio.launcher.ReverseLauncher.java

public void launch(CommandLine cmd) throws Exception {
    ZipFile file = new ZipFile(cmd.getArgs()[0]);
    ZipEntry entry = file.getEntry("recording.tmcpr");
    if (entry == null) {
        throw new IOException("Input file is not a valid replay file.");
    }//from w w  w  . j a  v  a 2  s . c  o m
    long size = entry.getSize();
    if (size == -1) {
        throw new IOException("Uncompressed size of recording.tmcpr not set.");
    }

    InputStream from = file.getInputStream(entry);
    RandomAccessFile to = new RandomAccessFile(cmd.getArgs()[1], "rw");

    to.setLength(size);
    int nRead;
    long pos = size;
    byte[] buffer = new byte[8192];

    long lastUpdate = -1;
    while (true) {
        long pct = 100 - pos * 100 / size;
        if (lastUpdate != pct) {
            System.out.print("Reversing " + size + " bytes... " + pct + "%\r");
            lastUpdate = pct;
        }
        int next = readInt(from);
        int length = readInt(from);
        if (next == -1 || length == -1) {
            break; // reached end of stream
        }
        // Increase buffer if necessary
        if (length + 8 > buffer.length) {
            buffer = new byte[length + 8];
        }
        buffer[0] = (byte) ((next >>> 24) & 0xFF);
        buffer[1] = (byte) ((next >>> 16) & 0xFF);
        buffer[2] = (byte) ((next >>> 8) & 0xFF);
        buffer[3] = (byte) (next & 0xFF);
        buffer[4] = (byte) ((length >>> 24) & 0xFF);
        buffer[5] = (byte) ((length >>> 16) & 0xFF);
        buffer[6] = (byte) ((length >>> 8) & 0xFF);
        buffer[7] = (byte) (length & 0xFF);

        nRead = 0;
        while (nRead < length) {
            nRead += from.read(buffer, 8 + nRead, length - nRead);
        }

        pos -= length + 8;
        to.seek(pos);
        to.write(buffer, 0, length + 8);
    }

    from.close();
    to.close();

    System.out.println("\nDone!");
}

From source file:org.nuxeo.ecm.platform.mimetype.detectors.OOoMimetypeSniffer.java

public String[] guessOOo(File file) {

    String[] mimetype = {};//from w ww. ja  v  a  2 s  .  c  om
    File tempFile = null;

    try {
        ZipFile zip = new ZipFile(file);
        ZipEntry entry = zip.getEntry("mimetype");

        if (entry != null) {
            // we have an opendocument so lets unzip

            // unzip file to process xml content
            tempFile = Framework.createTempFile("nxMimeTypeDetector_", ".dir");
            tempFile.delete(); // to be able to create a dir under this name
            if (!tempFile.isDirectory()) {
                tempFile.mkdir();
            }
            ZipUtils.unzip(file, tempFile);

            // retrieves mimetypefile
            String path = tempFile.getAbsolutePath();
            path += File.separator + "mimetype";
            File mimetypeFile = new File(path);
            mimetype = new String[] { FileUtils.readFile(mimetypeFile) };
        }
    } catch (IOException e) {
        // probably not a zip file
    } finally {
        if (tempFile != null) {
            FileUtils.deleteTree(tempFile);
        }
    }

    return mimetype;
}

From source file:org.eurocarbdb.action.admin.LoadTaxonomy.java

/********************************* 
                               *//from  ww w .j av  a  2 s.  c  o  m
                               *   Open zip file from local file, extract names & nodes files from the 
                               *   zip stream, assign to local fields for later parsing.
                               */
protected void initDefaultInputStreams() throws IOException {
    log.debug("initialising default input streams for NCBI names/nodes files");
    String ncbi_zipfile_name = Eurocarb.getProperty("ncbi.taxonomy.localfile");
    ZipFile ncbi_zipfile = new ZipFile(ncbi_zipfile_name);

    ZipEntry names_file = ncbi_zipfile.getEntry("names.dmp");
    ZipEntry nodes_file = ncbi_zipfile.getEntry("nodes.dmp");

    this.instreamNames = ncbi_zipfile.getInputStream(names_file);
    this.instreamNodes = ncbi_zipfile.getInputStream(nodes_file);
}

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

public static void extractZip(final File archive, final String zipEntryToBeExtracted, final File dstFile,
        final ProgressNotifyable prog) throws IOException {
    ZipFile zf = new ZipFile(archive);
    ZipEntry entry = zf.getEntry(zipEntryToBeExtracted);
    if (entry != null)
        extractEntry(zf, entry, dstFile, prog);
    zf.close();//from w w  w  . j ava2s  . c om
}

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

public static long extractZipEntrySizeInBytes(final File archive, final String zipEntryToBeExtracted)
        throws IOException {
    ZipFile zf = new ZipFile(archive);
    ZipEntry entry = zf.getEntry(zipEntryToBeExtracted);
    zf.close();//w  ww. ja v  a  2s. c om
    if (entry != null)
        return entry.getSize();
    return 0;
}

From source file:org.pentaho.webpackage.deployer.UrlTransformer.java

@Override
public boolean canHandle(File file) {
    if (!file.exists()) {
        return false;
    }/*w  w  w.  j  a  v a2  s .  c om*/

    if (file.getName().endsWith(".tgz")) {
        TarArchiveInputStream tarInput = null;
        try {
            tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file)));
            TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
            while (currentEntry != null) {
                if (currentEntry.getName().endsWith("package.json")) {
                    return true;
                }

                currentEntry = tarInput.getNextTarEntry();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (tarInput != null) {
                try {
                    tarInput.close();
                } catch (IOException ignored) {
                    // Ignore
                }
            }
        }

        return false;
    } else if (file.getName().endsWith(".zip") || file.getName().endsWith(".jar")) {
        ZipFile zipFile = null;

        try {
            zipFile = new ZipFile(file);

            return zipFile.getEntry("package.json") != null && zipFile.getEntry("META-INF/MANIFEST.MF") == null;
        } catch (IOException e) {
            this.logger.error(e.getMessage(), e);
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (IOException ignored) {
                    // Ignore
                }
            }
        }
    }

    return false;
}

From source file:de.dfki.km.perspecting.obie.corpus.BBCMusicCorpus.java

public Reader getGroundTruth(final URI uri) throws Exception {
    if (labelFileMediaType == MediaType.DIRECTORY) {
        return new StringReader(FileUtils.readFileToString(new File(uri)));
    } else if (labelFileMediaType == MediaType.ZIP) {
        ZipFile zipFile = new ZipFile(labelFolder);
        String[] entryName = uri.toURL().getFile().split("/");
        ZipEntry entry = zipFile.getEntry(
                URLDecoder.decode(entryName[entryName.length - 1], "utf-8").replace("txt", "dumps") + ".rdf");

        if (entry != null) {
            log.info("found labels for: " + uri.toString());
        } else {//w ww  . j  av a  2s.c  om
            throw new Exception("did not found labels for: " + uri.toString());
        }
        return new InputStreamReader(zipFile.getInputStream(entry));
    } else {
        throw new Exception("Unsupported media format for labels: " + labelFileMediaType + ". "
                + "Please use zip or plain directories instead.");
    }
}

From source file:de.dfki.km.perspecting.obie.corpus.BBCNatureCorpus.java

public Reader getGroundTruth(final URI uri) throws Exception {
    if (labelFileMediaType == MediaType.DIRECTORY) {
        return new StringReader(FileUtils.readFileToString(new File(uri)));
    } else if (labelFileMediaType == MediaType.ZIP) {
        ZipFile zipFile = new ZipFile(labelFolder);
        String[] entryName = uri.toURL().getFile().split("/");
        ZipEntry entry = zipFile
                .getEntry(URLDecoder.decode(entryName[entryName.length - 1], "utf-8").replace("text", "rdf"));

        if (entry != null) {
            log.info("found labels for: " + uri.toString());
        } else {/*from  w  ww  .jav  a  2s.  c  o  m*/
            throw new Exception("did not found labels for: " + uri.toString());
        }
        return new InputStreamReader(zipFile.getInputStream(entry));
    } else {
        throw new Exception("Unsupported media format for labels: " + labelFileMediaType + ". "
                + "Please use zip or plain directories instead.");
    }
}