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

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

Introduction

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

Prototype

public ZipArchiveEntry(ZipArchiveEntry entry) throws ZipException 

Source Link

Document

Creates a new zip entry with fields taken from the specified zip entry.

Usage

From source file:org.itstechupnorth.walrus.base.ArticleBuffer.java

public void saveTo(ZipArchiveOutputStream out) throws IOException {
    saving = true;//from w  ww.  java2 s .co  m
    final int originalPosition = buffer.position();
    // System.out.println(moniker() + "saving@" + buffer.position());
    try {
        buffer.flip();
        encoder.reset();
        final String name = NAME_PREFIX + getTitleHash() + NAME_SUFFIX;
        final ZipArchiveEntry entry = new ZipArchiveEntry(name);
        entry.setComment(getTitle());
        out.putArchiveEntry(entry);

        outBuffer.clear();
        boolean more = true;
        while (more) {
            final CoderResult result = encoder.encode(buffer, outBuffer, true);
            out.write(outBuffer.array(), 0, outBuffer.position());
            outBuffer.clear();
            more = CoderResult.OVERFLOW.equals(result);
        }

        out.closeArchiveEntry();
    } finally {
        buffer.clear();
        buffer.position(originalPosition);
        // System.out.println(moniker() + "saved@" + buffer.position());
        saving = false;
    }
}

From source file:org.jason.mapmaker.server.util.ZipUtil.java

/**
 * Compress a List of File objects into a single ZIP file. User is responsible for deleting the files which
 * have been compressed//  www  .  ja  v a  2s .  c  o  m
 *
 * @param filename filename for the generated zip file
 * @param fileList List<File> containing the File objects to add to the archive
 * @return a File object representing the zip archive file
 * @throws FileNotFoundException unable to find the file when creating the OutputStream
 * @throws ArchiveException      something went wrong creating
 *                               the output archive
 * @throws IOException
 */
public static File compress(String filename, List<File> fileList) throws FileNotFoundException,

        ArchiveException,

        IOException {

    // Create the File object that will later be returned
    File zipfile = new File(tmpFile, filename);

    // create the output stream and the archiveoutput stream (as a zip file)
    final OutputStream os = new FileOutputStream(zipfile);
    ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            os);

    // cycle throught the files in the list
    for (File file : fileList) {
        // add an anonymous ZipArchiveEntry to the archiveoutputstream, then copy the streams
        aos.putArchiveEntry(new ZipArchiveEntry(file.getName()));
        IOUtils.copy(new FileInputStream(file), aos);
        aos.closeArchiveEntry();

    }

    // cleanup. Everybody do their part.
    aos.close();
    os.close();

    return zipfile;
}

From source file:org.jwebsocket.util.Tools.java

/**
 * Compress a byte array using zip compression
 *
 * @param aBA//from www .j a v a2  s  .c  om
 * @param aBase64Encode if TRUE, the result is Base64 encoded
 * @return
 * @throws Exception
 */
public static byte[] zip(byte[] aBA, Boolean aBase64Encode) throws Exception {
    ByteArrayOutputStream lBAOS = new ByteArrayOutputStream();
    ArchiveOutputStream lAOS = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            lBAOS);
    ZipArchiveEntry lZipEntry = new ZipArchiveEntry("temp.zip");
    lZipEntry.setSize(aBA.length);
    lAOS.putArchiveEntry(lZipEntry);
    lAOS.write(aBA);
    lAOS.closeArchiveEntry();
    lAOS.flush();
    lAOS.close();

    if (aBase64Encode) {
        aBA = Base64.encodeBase64(lBAOS.toByteArray());
    } else {
        aBA = lBAOS.toByteArray();
    }

    return aBA;
}

From source file:org.mycore.services.zipper.MCRZipServlet.java

@Override
protected void sendCompressedDirectory(MCRPath file, BasicFileAttributes attrs,
        ZipArchiveOutputStream container) throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(getFilename(file) + "/");
    entry.setTime(attrs.lastModifiedTime().toMillis());
    container.putArchiveEntry(entry);//  w  ww. j  a v  a2  s. c  om
    container.closeArchiveEntry();
}

From source file:org.mycore.services.zipper.MCRZipServlet.java

@Override
protected void sendCompressedFile(MCRPath file, BasicFileAttributes attrs, ZipArchiveOutputStream container)
        throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(getFilename(file));
    entry.setTime(attrs.lastModifiedTime().toMillis());
    entry.setSize(attrs.size());/*  w w w.  ja va 2s  .  c  om*/
    container.putArchiveEntry(entry);
    try {
        Files.copy(file, container);
    } finally {
        container.closeArchiveEntry();
    }
}

From source file:org.mycore.services.zipper.MCRZipServlet.java

@Override
protected void sendMetadataCompressed(String fileName, byte[] content, long lastModified,
        ZipArchiveOutputStream container) throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(fileName);
    entry.setSize(content.length);//from  w w w  .  ja  v  a 2 s  . c o m
    entry.setTime(lastModified);
    container.putArchiveEntry(entry);
    container.write(content);
    container.closeArchiveEntry();
}

From source file:org.ngrinder.common.util.CompressionUtil.java

/**
 * Zip the given src into the given output stream.
 * /*from ww w  .j  av a2s . c  om*/
 * @param src
 *            src to be zipped
 * @param os
 *            output stream
 * @param charsetName
 *            character set to be used
 * @param includeSrc
 *            true if src will be included.
 * @throws IOException
 *             IOException
 */
public static void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException {
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
    zos.setEncoding(charsetName);
    FileInputStream fis;

    int length;
    ZipArchiveEntry ze;
    byte[] buf = new byte[8 * 1024];
    String name;

    Stack<File> stack = new Stack<File>();
    File root;
    if (src.isDirectory()) {
        if (includeSrc) {
            stack.push(src);
            root = src.getParentFile();
        } else {
            File[] fs = src.listFiles();
            for (int i = 0; i < fs.length; i++) {
                stack.push(fs[i]);
            }
            root = src;
        }
    } else {
        stack.push(src);
        root = src.getParentFile();
    }

    while (!stack.isEmpty()) {
        File f = stack.pop();
        name = toPath(root, f);
        if (f.isDirectory()) {
            File[] fs = f.listFiles();
            for (int i = 0; i < fs.length; i++) {
                if (fs[i].isDirectory()) {
                    stack.push(fs[i]);
                } else {
                    stack.add(0, fs[i]);
                }
            }
        } else {
            ze = new ZipArchiveEntry(name);
            zos.putArchiveEntry(ze);
            fis = new FileInputStream(f);
            while ((length = fis.read(buf, 0, buf.length)) >= 0) {
                zos.write(buf, 0, length);
            }
            fis.close();
            zos.closeArchiveEntry();
        }
    }
    zos.close();
}

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Zip the given src into the given output stream.
 *
 * @param src         src to be zipped//  w w w  .j a  va 2 s. c  o  m
 * @param os          output stream
 * @param charsetName character set to be used
 * @param includeSrc  true if src will be included.
 * @throws IOException IOException
 */
public static void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException {
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
    zos.setEncoding(charsetName);
    FileInputStream fis = null;

    int length;
    ZipArchiveEntry ze;
    byte[] buf = new byte[8 * 1024];
    String name;

    Stack<File> stack = new Stack<File>();
    File root;
    if (src.isDirectory()) {
        if (includeSrc) {
            stack.push(src);
            root = src.getParentFile();
        } else {
            File[] fs = checkNotNull(src.listFiles());
            for (int i = 0; i < fs.length; i++) {
                stack.push(fs[i]);
            }

            root = src;
        }
    } else {
        stack.push(src);
        root = src.getParentFile();
    }

    while (!stack.isEmpty()) {
        File f = stack.pop();
        name = toPath(root, f);
        if (f.isDirectory()) {
            File[] fs = checkNotNull(f.listFiles());
            for (int i = 0; i < fs.length; i++) {
                if (fs[i].isDirectory()) {
                    stack.push(fs[i]);
                } else {
                    stack.add(0, fs[i]);
                }
            }
        } else {
            ze = new ZipArchiveEntry(name);
            zos.putArchiveEntry(ze);
            try {
                fis = new FileInputStream(f);
                while ((length = fis.read(buf, 0, buf.length)) >= 0) {
                    zos.write(buf, 0, length);
                }
            } finally {
                IOUtils.closeQuietly(fis);
            }
            zos.closeArchiveEntry();
        }
    }
    zos.close();
}

From source file:org.ngrinder.script.util.CompressionUtil.java

public void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException {
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
    zos.setEncoding(charsetName);//w  w  w.j  a  va2 s. co m
    FileInputStream fis;

    int length;
    ZipArchiveEntry ze;
    byte[] buf = new byte[8 * 1024];
    String name;

    Stack<File> stack = new Stack<File>();
    File root;
    if (src.isDirectory()) {
        if (includeSrc) {
            stack.push(src);
            root = src.getParentFile();
        } else {
            File[] fs = src.listFiles();
            for (int i = 0; i < fs.length; i++) {
                stack.push(fs[i]);
            }
            root = src;
        }
    } else {
        stack.push(src);
        root = src.getParentFile();
    }

    while (!stack.isEmpty()) {
        File f = stack.pop();
        name = toPath(root, f);
        if (f.isDirectory()) {
            File[] fs = f.listFiles();
            for (int i = 0; i < fs.length; i++) {
                if (fs[i].isDirectory())
                    stack.push(fs[i]);
                else
                    stack.add(0, fs[i]);
            }
        } else {
            ze = new ZipArchiveEntry(name);
            zos.putArchiveEntry(ze);
            fis = new FileInputStream(f);
            while ((length = fis.read(buf, 0, buf.length)) >= 0) {
                zos.write(buf, 0, length);
            }
            fis.close();
            zos.closeArchiveEntry();
        }
    }
    zos.close();
}

From source file:org.onehippo.forge.content.exim.repository.jaxrs.util.ZipCompressUtils.java

/**
 * Add a ZIP entry to {@code zipOutput} with the given {@code entryName} and {@code bytes} starting from
 * {@code offset} in {@code length}./*from  w  w  w .java  2 s  .co  m*/
 * @param entryName ZIP entry name
 * @param bytes the byte array to fill in for the ZIP entry
 * @param offset the starting offset index to read from the byte array
 * @param length the length to read from the byte array
 * @param zipOutput ZipArchiveOutputStream instance
 * @throws IOException if IO exception occurs
 */
public static void addEntryToZip(String entryName, byte[] bytes, int offset, int length,
        ZipArchiveOutputStream zipOutput) throws IOException {
    ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
    entry.setSize(length);

    try {
        zipOutput.putArchiveEntry(entry);
        zipOutput.write(bytes, offset, length);
    } finally {
        zipOutput.closeArchiveEntry();
    }
}