Example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream STORED

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream STORED

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream STORED.

Prototype

int STORED

To view the source code for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream STORED.

Click Source Link

Document

Compression method for stored entries.

Usage

From source file:com.haulmont.cuba.core.app.importexport.EntityImportExport.java

@Override
public byte[] exportEntitiesToZIP(Collection<? extends Entity> entities) {
    String json = entitySerialization.toJson(entities, null,
            EntitySerializationOption.COMPACT_REPEATED_ENTITIES);
    byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream);
    zipOutputStream.setMethod(ZipArchiveOutputStream.STORED);
    zipOutputStream.setEncoding(StandardCharsets.UTF_8.name());
    ArchiveEntry singleDesignEntry = newStoredEntry("entities.json", jsonBytes);
    try {/*from   ww  w  . ja v a  2 s . c o  m*/
        zipOutputStream.putArchiveEntry(singleDesignEntry);
        zipOutputStream.write(jsonBytes);
        zipOutputStream.closeArchiveEntry();
    } catch (Exception e) {
        throw new RuntimeException("Error on creating zip archive during entities export", e);
    } finally {
        IOUtils.closeQuietly(zipOutputStream);
    }
    return byteArrayOutputStream.toByteArray();
}

From source file:com.haulmont.cuba.core.app.FoldersServiceBean.java

@Override
public byte[] exportFolder(Folder folder) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream);
    zipOutputStream.setMethod(ZipArchiveOutputStream.STORED);
    zipOutputStream.setEncoding(StandardCharsets.UTF_8.name());
    String xml = createXStream().toXML(folder);
    byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8);
    ArchiveEntry zipEntryDesign = newStoredEntry("folder.xml", xmlBytes);
    zipOutputStream.putArchiveEntry(zipEntryDesign);
    zipOutputStream.write(xmlBytes);/*from   w ww. j  a va  2s . c  o  m*/
    try {
        zipOutputStream.closeArchiveEntry();
    } catch (Exception ex) {
        throw new RuntimeException(
                String.format("Exception occurred while exporting folder %s.", folder.getName()));
    }

    zipOutputStream.close();
    return byteArrayOutputStream.toByteArray();
}

From source file:org.apache.tika.server.writer.ZipWriter.java

public void writeTo(Map<String, byte[]> parts, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    ZipArchiveOutputStream zip = new ZipArchiveOutputStream(entityStream);

    zip.setMethod(ZipArchiveOutputStream.STORED);

    for (Map.Entry<String, byte[]> entry : parts.entrySet()) {
        zipStoreBuffer(zip, entry.getKey(), entry.getValue());
    }/*  w w w  .  j  a  v a 2s.com*/

    zip.close();
}

From source file:org.codehaus.plexus.archiver.jar.JarArchiver.java

/**
 *//*  w  ww .  j  av  a2s .  co  m*/
protected boolean createEmptyZip(File zipFile) throws ArchiverException {
    if (!createEmpty) {
        return true;
    }

    try {
        getLogger().debug("Building MANIFEST-only jar: " + getDestFile().getAbsolutePath());
        zipArchiveOutputStream = new ZipArchiveOutputStream(
                bufferedOutputStream(fileOutputStream(getDestFile(), "jar")));

        zipArchiveOutputStream.setEncoding(getEncoding());
        if (isCompress()) {
            zipArchiveOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED);
        } else {
            zipArchiveOutputStream.setMethod(ZipArchiveOutputStream.STORED);
        }
        ConcurrentJarCreator ps = new ConcurrentJarCreator(Runtime.getRuntime().availableProcessors());
        initZipOutputStream(ps);
        finalizeZipOutputStream(ps);
    } catch (IOException ioe) {
        throw new ArchiverException("Could not create almost empty JAR archive (" + ioe.getMessage() + ")",
                ioe);
    } finally {
        // Close the output stream.
        //IOUtil.close( zOut );
        createEmpty = false;
    }
    return true;
}

From source file:org.codehaus.plexus.archiver.zip.AbstractZipArchiver.java

private void createArchiveMain() throws ArchiverException, IOException {
    //noinspection deprecation
    if (!Archiver.DUPLICATES_SKIP.equals(duplicate)) {
        //noinspection deprecation
        setDuplicateBehavior(duplicate);
    }/*  ww  w.  j a  v  a 2s.  co  m*/

    ResourceIterator iter = getResources();
    if (!iter.hasNext() && !hasVirtualFiles()) {
        throw new ArchiverException("You must set at least one file.");
    }

    zipFile = getDestFile();

    if (zipFile == null) {
        throw new ArchiverException("You must set the destination " + archiveType + "file.");
    }

    if (zipFile.exists() && !zipFile.isFile()) {
        throw new ArchiverException(zipFile + " isn't a file.");
    }

    if (zipFile.exists() && !zipFile.canWrite()) {
        throw new ArchiverException(zipFile + " is read-only.");
    }

    // Whether or not an actual update is required -
    // we don't need to update if the original file doesn't exist

    addingNewFiles = true;

    if (doUpdate && !zipFile.exists()) {
        doUpdate = false;
        getLogger().debug("ignoring update attribute as " + archiveType + " doesn't exist.");
    }

    success = false;

    if (doUpdate) {
        renamedFile = FileUtils.createTempFile("zip", ".tmp", zipFile.getParentFile());
        renamedFile.deleteOnExit();

        try {
            FileUtils.rename(zipFile, renamedFile);
        } catch (SecurityException e) {
            getLogger().debug(e.toString());
            throw new ArchiverException(
                    "Not allowed to rename old file (" + zipFile.getAbsolutePath() + ") to temporary file", e);
        } catch (IOException e) {
            getLogger().debug(e.toString());
            throw new ArchiverException(
                    "Unable to rename old file (" + zipFile.getAbsolutePath() + ") to temporary file", e);
        }
    }

    String action = doUpdate ? "Updating " : "Building ";

    getLogger().info(action + archiveType + ": " + zipFile.getAbsolutePath());

    if (!skipWriting) {
        zipArchiveOutputStream = new ZipArchiveOutputStream(
                bufferedOutputStream(fileOutputStream(zipFile, "zip")));
        zipArchiveOutputStream.setEncoding(encoding);
        zipArchiveOutputStream
                .setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE);
        zipArchiveOutputStream
                .setMethod(doCompress ? ZipArchiveOutputStream.DEFLATED : ZipArchiveOutputStream.STORED);

        zOut = new ConcurrentJarCreator(Runtime.getRuntime().availableProcessors());
    }
    initZipOutputStream(zOut);

    // Add the new files to the archive.
    addResources(iter, zOut);

    // If we've been successful on an update, delete the
    // temporary file
    if (doUpdate) {
        if (!renamedFile.delete()) {
            getLogger().warn("Warning: unable to delete temporary file " + renamedFile.getName());
        }
    }
    success = true;
}