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

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

Introduction

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

Prototype

public void putArchiveEntry(ArchiveEntry archiveEntry) throws IOException 

Source Link

Usage

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

public void saveTo(ZipArchiveOutputStream out) throws IOException {
    saving = true;//from w w  w  . ja  va 2 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.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);
    container.closeArchiveEntry();// ww w  . j a va  2  s.  c om
}

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());//from  w ww .j  a v a 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   www. j  a v a2  s. com
    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.
 * //  w w w .j a v a 2s. 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/* ww w . j a v a2  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);/*from   ww w .  j  a  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}./*from   w ww  .j a v a  2  s  . com*/
 * @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
 *//*w  ww. j  a  v a 2 s  .  c o  m*/
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);
        }
    }
}

From source file:org.opengion.fukurou.util.ZipArchive.java

/**
 * ZIP????// ww w. ja v a2 s . c o  m
 * ???File?????????
 * ???????????????
 * ??????
 *
 * @param list   ZIP???
 * @param zos    ZIPOutputStream
 * @param prefix ?
 * @param files  ??
 * @throws IOException ????
 * @og.rev 4.1.0.2 (2008/02/01) ?
 * @og.rev 5.1.9.0 (2010/08/01) ? ?BufferedInputStream ??????
 */
private static void addZipEntry(final List<File> list, final ZipArchiveOutputStream zos, final String prefix,
        final File[] files) {
    File tmpFile = null;
    try {
        for (File fi : files) {
            tmpFile = fi; // ?
            list.add(fi);
            if (fi.isDirectory()) {
                String entryName = prefix + fi.getName() + "/";
                ZipArchiveEntry zae = new ZipArchiveEntry(entryName);
                zos.putArchiveEntry(zae);
                zos.closeArchiveEntry();

                addZipEntry(list, zos, entryName, fi.listFiles());
            } else {
                String entryName = prefix + fi.getName();
                ZipArchiveEntry zae = new ZipArchiveEntry(entryName);
                zos.putArchiveEntry(zae);
                InputStream is = new BufferedInputStream(new FileInputStream(fi));
                IOUtils.copy(is, zos);
                zos.closeArchiveEntry();
                Closer.ioClose(is);
            }
        }
    } catch (FileNotFoundException ex) {
        String errMsg = "??????[??=" + tmpFile
                + "]";
        throw new RuntimeException(errMsg, ex);
    } catch (IOException ex) {
        String errMsg = "ZIP?????[??=" + tmpFile + "]";
        throw new RuntimeException(errMsg, ex);
    }
}