Example usage for java.util.zip ZipEntry setMethod

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

Introduction

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

Prototype

public void setMethod(int method) 

Source Link

Document

Sets the compression method for the entry.

Usage

From source file:org.kuali.kfs.module.ar.report.service.impl.TransmitContractsAndGrantsInvoicesServiceImpl.java

/**
 *
 * @param report//  w ww .ja  va 2 s .  c  om
 * @param invoiceFileWritten
 * @param zos
 * @param buffer
 * @param crc
 * @return
 * @throws IOException
 */
private boolean writeFile(byte[] arrayToWrite, ZipOutputStream zos, String fileName) throws IOException {
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    if (ObjectUtils.isNotNull(arrayToWrite) && arrayToWrite.length > 0) {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(arrayToWrite));
        try {
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }
            bis.close();
            // Reset to beginning of input stream
            bis = new BufferedInputStream(new ByteArrayInputStream(arrayToWrite));
            ZipEntry entry = new ZipEntry(fileName);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(arrayToWrite.length);
            entry.setSize(arrayToWrite.length);
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
        } finally {
            bis.close();
        }
        return true;
    }
    return false;
}

From source file:org.nuxeo.template.odt.OOoArchiveModifier.java

protected void writeOOoEntry(ZipOutputStream zipOutputStream, String entryName, File fileEntry, int zipMethod)
        throws IOException {

    if (fileEntry.isDirectory()) {
        entryName = entryName + "/";
        ZipEntry zentry = new ZipEntry(entryName);
        zipOutputStream.putNextEntry(zentry);
        zipOutputStream.closeEntry();/*from w  w w . java  2  s. co m*/
        for (File child : fileEntry.listFiles()) {
            writeOOoEntry(zipOutputStream, entryName + child.getName(), child, zipMethod);
        }
        return;
    }

    ZipEntry zipEntry = new ZipEntry(entryName);
    try (InputStream entryInputStream = new FileInputStream(fileEntry)) {
        zipEntry.setMethod(zipMethod);
        if (zipMethod == ZipEntry.STORED) {
            byte[] inputBytes = IOUtils.toByteArray(entryInputStream);
            CRC32 crc = new CRC32();
            crc.update(inputBytes);
            zipEntry.setCrc(crc.getValue());
            zipEntry.setSize(inputBytes.length);
            zipEntry.setCompressedSize(inputBytes.length);
            zipOutputStream.putNextEntry(zipEntry);
            IOUtils.write(inputBytes, zipOutputStream);
        } else {
            zipOutputStream.putNextEntry(zipEntry);
            IOUtils.copy(entryInputStream, zipOutputStream);
        }
    }
    zipOutputStream.closeEntry();
}

From source file:org.omegat.filters3.xml.opendoc.OpenDocFilter.java

/**
 * Processes a single OpenDocument file, which is actually a ZIP file consisting of many XML files, some
 * of which should be translated./*from ww w.j ava  2s .c  o m*/
 */
@Override
public void processFile(File inFile, File outFile, FilterContext fc) throws IOException, TranslationException {
    ZipFile zipfile = new ZipFile(inFile);
    ZipOutputStream zipout = null;
    if (outFile != null)
        zipout = new ZipOutputStream(new FileOutputStream(outFile));
    Enumeration<? extends ZipEntry> zipcontents = zipfile.entries();
    while (zipcontents.hasMoreElements()) {
        ZipEntry zipentry = zipcontents.nextElement();
        String shortname = zipentry.getName();
        if (shortname.lastIndexOf('/') >= 0)
            shortname = shortname.substring(shortname.lastIndexOf('/') + 1);
        if (TRANSLATABLE.contains(shortname)) {
            File tmpin = tmp();
            FileUtils.copyInputStreamToFile(zipfile.getInputStream(zipentry), tmpin);
            File tmpout = null;
            if (zipout != null)
                tmpout = tmp();

            try {
                createXMLFilter(processOptions).processFile(tmpin, tmpout, fc);
            } catch (Exception e) {
                zipfile.close();
                throw new TranslationException(
                        e.getLocalizedMessage() + "\n" + OStrings.getString("OpenDoc_ERROR_IN_FILE") + inFile);
            }

            if (zipout != null) {
                ZipEntry outentry = new ZipEntry(zipentry.getName());
                outentry.setMethod(ZipEntry.DEFLATED);
                zipout.putNextEntry(outentry);
                FileUtils.copyFile(tmpout, zipout);
                zipout.closeEntry();
            }
            if (!tmpin.delete())
                tmpin.deleteOnExit();
            if (tmpout != null) {
                if (!tmpout.delete())
                    tmpout.deleteOnExit();
            }
        } else {
            if (zipout != null) {
                ZipEntry outentry = new ZipEntry(zipentry.getName());
                zipout.putNextEntry(outentry);
                IOUtils.copy(zipfile.getInputStream(zipentry), zipout);
                zipout.closeEntry();
            }
        }
    }
    if (zipout != null)
        zipout.close();
    zipfile.close();
}

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;
    }// w w  w.j  a v  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: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./* w  w w.  j  ava 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;
    }
}