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

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

Introduction

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

Prototype

public void closeArchiveEntry() throws IOException 

Source Link

Document

Writes all necessary data for this entry.

Usage

From source file:org.fl.modules.excel.poi.exportExcel.multi.SXSSFWorkBookOperation.java

public void compressFiles2Zip() {
    org.apache.commons.io.output.ByteArrayOutputStream byteArrayOutputStream = new org.apache.commons.io.output.ByteArrayOutputStream();
    try {//w  ww. ja v a  2  s. c o m
        byteArrayOutputStream = write(byteArrayOutputStream);
        ZipArchiveOutputStream zaos = null;
        zaos = new ZipArchiveOutputStream(byteArrayOutputStream);
        // Use Zip64 extensions for all entries where they are required
        zaos.setUseZip64(Zip64Mode.AsNeeded);
        if (byteArrayOutputStream != null) {
            ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry("excel");
            zaos.putArchiveEntry(zipArchiveEntry);
            try {
                byteArrayOutputStream.writeTo(zaos);
                zaos.closeArchiveEntry();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

public void saveTo(ZipArchiveOutputStream out) throws IOException {
    saving = true;//from w w w  . ja  v  a  2s  .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.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);/*from w w w  .j  av a2  s .com*/
    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());//ww w  .j  ava 2  s. c o m
    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  ww.ja v a 2 s  .  c om*/
    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 w  w w.ja v a2s  .  com*/
 * @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/*from w w w .j  a va  2s.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);// ww w. ja  v  a 2s . c  o 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}./*w w  w.j  av a 2 s  .  c o 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();
    }
}

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

/**
 * Add ZIP entries to {@code zipOutput} by selecting all the descendant files under the {@code baseFolder},
 * starting with the ZIP entry name {@code prefix}.
 * @param baseFolder base folder to find child files underneath
 * @param prefix the prefix of ZIP entry name
 * @param zipOutput ZipArchiveOutputStream instance
 * @throws IOException if IO exception occurs
 *///from  w  w  w . j a  va  2  s  .  c om
public static void addFileEntriesInFolderToZip(File baseFolder, String prefix, ZipArchiveOutputStream zipOutput)
        throws IOException {
    for (File file : baseFolder.listFiles()) {
        String entryName = (StringUtils.isEmpty(prefix)) ? file.getName() : (prefix + "/" + file.getName());

        if (file.isFile()) {
            ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
            entry.setSize(file.length());
            InputStream input = null;

            try {
                zipOutput.putArchiveEntry(entry);
                input = new FileInputStream(file);
                IOUtils.copyLarge(input, zipOutput);
            } finally {
                IOUtils.closeQuietly(input);
                zipOutput.closeArchiveEntry();
            }
        } else {
            addFileEntriesInFolderToZip(file, entryName, zipOutput);
        }
    }
}