Example usage for java.util.zip ZipEntry DEFLATED

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

Introduction

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

Prototype

int DEFLATED

To view the source code for java.util.zip ZipEntry DEFLATED.

Click Source Link

Document

Compression method for compressed (deflated) entries.

Usage

From source file:org.sead.nds.repository.BagGenerator.java

private void createFileFromURL(final String name, final String uri)
        throws IOException, ExecutionException, InterruptedException {

    ZipArchiveEntry archiveEntry = new ZipArchiveEntry(name);
    archiveEntry.setMethod(ZipEntry.DEFLATED);
    InputStreamSupplier supp = RO.getInputStreamSupplier(uri);
    addEntry(archiveEntry, supp);/* w  w  w.  ja  v  a  2 s.c  om*/
}

From source file:org.sead.nds.repository.BagGenerator.java

private boolean createFileFromLocalSource(String name, String hashtype, String childHash) throws IOException {
    InputStreamSupplier supp = lcProvider.getSupplierFor(hashtype, childHash);
    if (supp != null) {
        ZipArchiveEntry archiveEntry = new ZipArchiveEntry(name);
        archiveEntry.setMethod(ZipEntry.DEFLATED);
        addEntry(archiveEntry, supp);//from w w  w  .  ja va2 s.co m
        return true;
    }
    return false;
}

From source file:org.wso2.carbon.registry.synchronization.operation.CheckOutCommand.java

private void dumpToFile(Registry registry) throws SynchronizationException {

    String outputXml = outputFile + SynchronizationConstants.META_FILE_EXTENSION;
    if (workingDir != null) {
        outputFile = workingDir + File.separator + outputFile;
    }/*from  w ww  .j av a 2  s  . co m*/

    try {
        if (!registry.resourceExists(checkOutPath)) {
            throw new SynchronizationException(MessageCode.ERROR_IN_DUMPING_NO_RESOURCE_OR_NO_PERMISSION,
                    new String[] { "path: " + checkOutPath, "username: " + username });
        }
    } catch (Exception e) {
        if (e.getCause() instanceof UnknownHostException) {
            throw new SynchronizationException(MessageCode.ERROR_IN_CONNECTING_REGISTRY, e,
                    new String[] { " registry url:" + registryUrl });
        }
        throw new SynchronizationException(MessageCode.ERROR_IN_DUMPING_AUTHORIZATION_FAILED, e,
                new String[] { "path: " + checkOutPath, "username: " + username });
    }

    try {
        // we don't care what is dumping..
        // doing the dump
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile));
        ZipEntry ze = new ZipEntry(outputXml);
        ze.setMethod(ZipEntry.DEFLATED);
        zos.putNextEntry(ze);

        Writer zipWriter = new OutputStreamWriter(zos);

        log.debug("Starting to do registry 'dumpToFile' for : " + checkOutPath + " with : " + outputXml);
        registry.dumpLite(checkOutPath, zipWriter);
        log.debug("Registry 'dumpToFile' completed for : " + checkOutPath + " in : " + outputXml);

        zos.close();
    } catch (Exception e) {
        throw new SynchronizationException(MessageCode.ERROR_IN_DUMPING, e,
                new String[] { "path: " + checkOutPath, "username: " + username });
    }

    if (cleanRegistry && registryUrl == null) {
        Utils.cleanEmbeddedRegistry();
    }
}

From source file:reconcile.hbase.mapreduce.ZipInputFormat.java

private List<ZipEntrySplit> getZipFileEntries(JobContext context, FileSystem fs, Path[] zipFiles,
        Integer maxEntryFiles, Integer ignoreFilesLargerThanMB, List<String> processMimeTypes)
        throws IOException {
    ArrayList<ZipEntrySplit> splits = new ArrayList<ZipEntrySplit>();
    ZipInputStream zis = null;/*from   ww w. j  a  va2  s. c  om*/
    ZipEntry zipEntry = null;

    for (int i = 0; i < zipFiles.length; i++) {
        Path file = zipFiles[i];
        LOG.debug("Opening zip file: " + file.toString());
        try {
            zis = new ZipInputStream(fs.open(file));
            while ((zipEntry = zis.getNextEntry()) != null) {
                if (maxEntryFiles != null && splits.size() == maxEntryFiles.intValue()) {
                    LOG.debug("Exceeded maximum number of splits.  End getSplits()");
                    return splits;
                }

                boolean processFile = true;

                if (processMimeTypes.size() > 0) {
                    // Ensure that if process mime types were specified, that entry
                    // mime type meets that criteria
                    String mimeType = fileNameMap.getContentTypeFor(zipEntry.getName());
                    if (mimeType == null || (!processMimeTypes.contains(mimeType.toLowerCase()))) {
                        processFile = false;
                        LOG.debug("Ignoring entry file (" + zipEntry.getName() + " mimeType(" + mimeType
                                + ") not in process list");
                    }
                }

                long byteCount = zipEntry.getSize();
                /*
                if (byteCount <= 0) {
                   // Read entry and figure out size for ourselves
                   byteCount = 0;
                   while (zis.available()==1) {
                      zis.read();
                      ++byteCount;
                   }
                }
                */
                if (ignoreFilesLargerThanMB != null && byteCount > ignoreFilesLargerThanMB.intValue()) {
                    processFile = false;
                    LOG.debug("Ignoring entry file (" + zipEntry.getName() + ") which exceeds size limit");
                }

                if (processFile) {
                    LOG.debug("Creating split for zip entry: " + zipEntry.getName() + " Size: " + byteCount
                            + " Method: " + (ZipEntry.DEFLATED == zipEntry.getMethod() ? "DEFLATED" : "STORED")
                            + " Compressed Size: " + zipEntry.getCompressedSize());

                    ZipEntrySplit zipSplit = new ZipEntrySplit(file, zipEntry.getName(), zipEntry.getSize(),
                            context);
                    splits.add(zipSplit);
                }
                zis.closeEntry();
            }
        } finally {
            IOUtils.closeQuietly(zis);
        }

    }
    return splits;
}

From source file:VASSAL.tools.io.ZipArchive.java

/**
 * Gets an {@link OutputStream} to write to the given file.
 *
 * <b>Note:</b> It is imperative the that calling code ensures that this
 * stream is eventually closed, since the returned stream holds a write
 * lock on the archive./*from  w w w  . ja v a 2 s  . c om*/
 *
 * @param path the path to the file in the archive
 * @param compress whether to compress the file
 * @return an <code>OutputStream</code> for the requested file
 * @throws IOException
 */
public OutputStream getOutputStream(String path, boolean compress) throws IOException {
    w.lock();
    try {
        openIfClosed();

        modified = true;

        // update the entries map
        Entry e = entries.get(path);
        if (e == null) {
            e = new Entry(null, null);
            entries.put(path, e);
        }

        // set up new ZipEntry
        final ZipEntry ze = new ZipEntry(path);
        ze.setMethod(compress ? ZipEntry.DEFLATED : ZipEntry.STORED);
        e.ze = ze;

        // clean up old temp file
        if (e.file != null) {
            e.file.delete();
        }

        // create new temp file
        e.file = TempFileManager.getInstance().createTempFile("zip", ".tmp");

        return new ZipArchiveOutputStream(new FileOutputStream(e.file), new CRC32(), e.ze);
    } catch (IOException ex) {
        w.unlock();
        throw ex;
    }
}

From source file:VASSAL.tools.io.ZipArchive.java

private void writeToDisk() throws IOException {
    // write all files to a temporary zip archive
    final File tmpFile = File.createTempFile("tmp", ".zip", archiveFile.getParentFile());

    ZipOutputStream out = null;/*w  w  w.j av  a 2 s. co  m*/
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tmpFile)));
        out.setLevel(9);

        final byte[] buf = new byte[8192];

        if (zipFile != null) {
            zipFile.close();
            zipFile = null;

            // copy unmodified file into the temp archive
            ZipInputStream in = null;
            try {
                in = new ZipInputStream(new BufferedInputStream(new FileInputStream(archiveFile)));

                ZipEntry ze = null;
                while ((ze = in.getNextEntry()) != null) {
                    // skip modified or removed entries
                    final Entry e = entries.get(ze.getName());
                    if (e == null || e.file != null)
                        continue;

                    // We can't reuse entries for compressed files because there's
                    // no way to reset all fields to acceptable values.
                    if (ze.getMethod() == ZipEntry.DEFLATED) {
                        ze = new ZipEntry(ze.getName());
                        ze.setTime(ze.getTime());
                    }

                    out.putNextEntry(ze);
                    IOUtils.copy(in, out, buf);

                    entries.remove(ze.getName());
                }

                in.close();
            } finally {
                IOUtils.closeQuietly(in);
            }
        }

        for (String name : entries.keySet()) {
            final Entry e = entries.get(name);

            // write new or modified file into the temp archive
            FileInputStream in = null;
            try {
                in = new FileInputStream(e.file);
                out.putNextEntry(e.ze);
                IOUtils.copy(in, out, buf);
                in.close();
            } finally {
                IOUtils.closeQuietly(in);
            }
        }

        out.close();
    } finally {
        IOUtils.closeQuietly(out);
    }

    // Replace old archive with temp archive.
    if (!tmpFile.renameTo(archiveFile)) {
        try {
            FileUtils.forceDelete(archiveFile);
            FileUtils.moveFile(tmpFile, archiveFile);
        } catch (IOException e) {
            String err = "Unable to overwrite " + archiveFile.getAbsolutePath() + ": ";

            if (!archiveFile.exists()) {
                err += " file does not exist.";
            } else if (!archiveFile.canWrite()) {
                err += " file is not writable.";
            } else if (!archiveFile.isFile()) {
                err += " not a normal file.";
            }

            err += " Data written to " + tmpFile.getAbsolutePath() + " instead.";
            throw (IOException) new IOException(err).initCause(e);
        }
    }

    closed = true;
    modified = false;
    entries.clear();
}