Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry setMethod

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry setMethod

Introduction

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

Prototype

public void setMethod(int method) 

Source Link

Document

Sets the compression method of this entry.

Usage

From source file:org.apache.sis.internal.maven.Assembler.java

/**
 * Creates the distribution file.//from   w  ww . ja  va 2s. c  o  m
 *
 * @throws MojoExecutionException if the plugin execution failed.
 */
@Override
public void execute() throws MojoExecutionException {
    final File sourceDirectory = new File(rootDirectory, ARTIFACT_PATH);
    if (!sourceDirectory.isDirectory()) {
        throw new MojoExecutionException("Directory not found: " + sourceDirectory);
    }
    final File targetDirectory = new File(rootDirectory, TARGET_DIRECTORY);
    final String version = project.getVersion();
    final String artifactBase = FINALNAME_PREFIX + version;
    try {
        final File targetFile = new File(distributionDirectory(targetDirectory), artifactBase + ".zip");
        final ZipArchiveOutputStream zip = new ZipArchiveOutputStream(targetFile);
        try {
            zip.setLevel(9);
            appendRecursively(sourceDirectory, artifactBase, zip, new byte[8192]);
            /*
             * At this point, all the "application/sis-console/src/main/artifact" and sub-directories
             * have been zipped.  Now generate the Pack200 file and zip it directly (without creating
             * a temporary "sis.pack.gz" file).
             */
            final Packer packer = new Packer(project.getName(), project.getUrl(), version, targetDirectory);
            final ZipArchiveEntry entry = new ZipArchiveEntry(
                    artifactBase + '/' + LIB_DIRECTORY + '/' + FATJAR_FILE + PACK_EXTENSION);
            entry.setMethod(ZipArchiveEntry.STORED);
            zip.putArchiveEntry(entry);
            packer.preparePack200(FATJAR_FILE + ".jar").pack(new FilterOutputStream(zip) {
                /*
                 * Closes the archive entry, not the ZIP file.
                 */
                @Override
                public void close() throws IOException {
                    zip.closeArchiveEntry();
                }
            });
        } finally {
            zip.close();
        }
    } catch (IOException e) {
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
}

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

/**
  * Adds a new entry to the archive, takes care of duplicates as well.
  *  @param in                 the stream to read data for the entry from.
  * @param zOut               the stream to write to.
  * @param vPath              the name this entry shall have in the archive.
  * @param lastModified       last modification time for the entry.
  * @param fromArchive        the original archive we are copying this
  * @param symlinkDestination//from  ww w  . ja  v a2  s . c  o m
  */
@SuppressWarnings({ "JavaDoc" })
protected void zipFile(InputStreamSupplier in, ConcurrentJarCreator zOut, String vPath, long lastModified,
        File fromArchive, int mode, String symlinkDestination) throws IOException, ArchiverException {
    getLogger().debug("adding entry " + vPath);

    entries.put(vPath, vPath);

    if (!skipWriting) {
        ZipArchiveEntry ze = new ZipArchiveEntry(vPath);
        setTime(ze, lastModified);

        ze.setMethod(doCompress ? ZipArchiveEntry.DEFLATED : ZipArchiveEntry.STORED);
        ze.setUnixMode(UnixStat.FILE_FLAG | mode);

        InputStream payload;
        if (ze.isUnixSymlink()) {
            ZipEncoding enc = ZipEncodingHelper.getZipEncoding(getEncoding());
            final byte[] bytes = enc.encode(symlinkDestination).array();
            payload = new ByteArrayInputStream(bytes);
            zOut.addArchiveEntry(ze, createInputStreamSupplier(payload));
        } else {
            zOut.addArchiveEntry(ze, wrappedRecompressor(ze, in));
        }
    }
}

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

protected void zipDir(PlexusIoResource dir, ConcurrentJarCreator zOut, String vPath, int mode,
        String encodingToUse) throws IOException {
    if (addedDirs.update(vPath)) {
        return;//from   ww  w.j ava 2  s  .  c  om
    }

    getLogger().debug("adding directory " + vPath);

    if (!skipWriting) {
        final boolean isSymlink = dir instanceof SymlinkDestinationSupplier;

        if (isSymlink && vPath.endsWith(File.separator)) {
            vPath = vPath.substring(0, vPath.length() - 1);
        }

        ZipArchiveEntry ze = new ZipArchiveEntry(vPath);

        /*
         * ZipOutputStream.putNextEntry expects the ZipEntry to
         * know its size and the CRC sum before you start writing
         * the data when using STORED mode - unless it is seekable.
         *
         * This forces us to process the data twice.
         */

        if (isSymlink) {
            mode = UnixStat.LINK_FLAG | mode;
        }

        if (dir != null && dir.isExisting()) {
            setTime(ze, dir.getLastModified());
        } else {
            // ZIPs store time with a granularity of 2 seconds, round up
            setTime(ze, System.currentTimeMillis());
        }
        if (!isSymlink) {
            ze.setSize(0);
            ze.setMethod(ZipArchiveEntry.STORED);
            // This is faintly ridiculous:
            ze.setCrc(EMPTY_CRC);
        }
        ze.setUnixMode(mode);

        if (!isSymlink) {
            zOut.addArchiveEntry(ze, createInputStreamSupplier(new ByteArrayInputStream("".getBytes())));
        } else {
            String symlinkDestination = ((SymlinkDestinationSupplier) dir).getSymlinkDestination();
            ZipEncoding enc = ZipEncodingHelper.getZipEncoding(encodingToUse);
            final byte[] bytes = enc.encode(symlinkDestination).array();
            ze.setMethod(ZipArchiveEntry.DEFLATED);
            zOut.addArchiveEntry(ze, createInputStreamSupplier(new ByteArrayInputStream(bytes)));
        }
    }
}

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

private InputStreamSupplier wrappedRecompressor(final ZipArchiveEntry ze, final InputStreamSupplier other) {

    return new InputStreamSupplier() {
        public InputStream get() {
            InputStream is = other.get();
            byte[] header = new byte[4];
            try {
                int read = is.read(header);
                boolean compressThis = doCompress;
                if (!recompressAddedZips && isZipHeader(header)) {
                    compressThis = false;
                }/*from  w w  w . j  a  v  a2 s  . co m*/

                ze.setMethod(compressThis ? ZipArchiveEntry.DEFLATED : ZipArchiveEntry.STORED);

                return maybeSequence(header, read, is);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    };
}

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

/**
 * Adds an archive entry to this archive.
 * <p/>/*  www . j a v  a  2s. c  om*/
 * This method is expected to be called from a single client thread
 *
 * @param zipArchiveEntry The entry to add. Compression method
 * @param source          The source input stream supplier
 * @throws java.io.IOException
 */

public void addArchiveEntry(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier source)
        throws IOException {
    final int method = zipArchiveEntry.getMethod();
    if (method == -1)
        throw new IllegalArgumentException("Method must be set on the supplied zipArchiveEntry");
    if (zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink()) {
        final ByteArrayInputStream payload = new ByteArrayInputStream(new byte[] {});
        directories.addArchiveEntry(
                createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload)));
        payload.close();
    } else if ("META-INF".equals(zipArchiveEntry.getName())) {
        InputStream payload = source.get();
        if (zipArchiveEntry.isDirectory())
            zipArchiveEntry.setMethod(ZipEntry.STORED);
        metaInfDir.addArchiveEntry(
                createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload)));
        payload.close();
    } else if ("META-INF/MANIFEST.MF".equals(zipArchiveEntry.getName())) {
        InputStream payload = source.get();
        if (zipArchiveEntry.isDirectory())
            zipArchiveEntry.setMethod(ZipEntry.STORED);
        manifest.addArchiveEntry(
                createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload)));
        payload.close();
    } else {
        parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, source);
    }
}

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

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

    ZipArchiveEntry archiveEntry = new ZipArchiveEntry(name);
    archiveEntry.setMethod(ZipEntry.DEFLATED);
    InputStreamSupplier supp = new InputStreamSupplier() {
        public InputStream get() {
            return new ByteArrayInputStream(("").getBytes());
        }//from   w  w w. ja  v a 2  s  .co  m
    };

    addEntry(archiveEntry, supp);
}

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

private void createFileFromString(final String name, final String content)
        throws IOException, ExecutionException, InterruptedException {

    ZipArchiveEntry archiveEntry = new ZipArchiveEntry(name);
    archiveEntry.setMethod(ZipEntry.DEFLATED);
    InputStreamSupplier supp = new InputStreamSupplier() {
        public InputStream get() {
            try {
                return new ByteArrayInputStream(content.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//from   www  . j  av  a2 s.  c o  m
            }
            return null;
        }
    };

    addEntry(archiveEntry, supp);
}

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);/*from   www  .  ja v a 2 s.  c o m*/
}

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.  j  a va  2 s .  com
        return true;
    }
    return false;
}