Example usage for org.apache.commons.compress.archivers.zip ZipEncoding encode

List of usage examples for org.apache.commons.compress.archivers.zip ZipEncoding encode

Introduction

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

Prototype

ByteBuffer encode(String name) throws IOException;

Source Link

Document

Encode a filename or a comment to a byte array suitable for storing it to a serialized zip entry.

Usage

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//  ww w . java 2s  .  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;// ww  w  .  j a  v  a 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)));
        }
    }
}