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

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

Introduction

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

Prototype

public ZipArchiveOutputStream(File file) throws IOException 

Source Link

Document

Creates a new ZIP OutputStream writing to a File.

Usage

From source file:org.apache.ant.compress.util.ZipStreamFactory.java

/**
 * @param stream the stream to write to, should be buffered
 * @param encoding the encoding of the entry names
 *///from w w w . j a v  a  2  s.  co  m
public ArchiveOutputStream getArchiveStream(OutputStream stream, String encoding) throws IOException {
    ZipArchiveOutputStream o = new ZipArchiveOutputStream(stream);
    o.setEncoding(encoding);
    return o;
}

From source file:org.apache.ant.compress.util.ZipStreamFactory.java

/**
 * @param file the file to write to//from  w w  w . j av  a 2  s  .  c  om
 * @param encoding the encoding of the entry names
 */
public ArchiveOutputStream getArchiveOutputStream(File file, String encoding) throws IOException {
    ZipArchiveOutputStream o = new ZipArchiveOutputStream(file);
    o.setEncoding(encoding);
    return o;
}

From source file:org.apache.camel.processor.aggregate.ZipAggregationStrategy.java

@Override
public void onCompletion(Exchange exchange) {
    List<Exchange> list = exchange.getProperty(Exchange.GROUPED_EXCHANGE, List.class);
    try {//from w ww.ja  v  a2  s.  c  o  m
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ZipArchiveOutputStream zout = new ZipArchiveOutputStream(bout);
        for (Exchange item : list) {
            String name = item.getProperty(ZIP_ENTRY_NAME,
                    item.getProperty(Exchange.FILE_NAME, item.getExchangeId(), String.class), String.class);
            byte[] body = item.getIn().getBody(byte[].class);
            ZipArchiveEntry entry = new ZipArchiveEntry(name);
            entry.setSize(body.length);
            zout.putArchiveEntry(entry);
            zout.write(body);
            zout.closeArchiveEntry();
        }
        zout.close();
        exchange.getIn().setBody(bout.toByteArray());
        exchange.removeProperty(Exchange.GROUPED_EXCHANGE);
    } catch (Exception e) {
        throw new RuntimeException("Unable to zip exchanges!", e);
    }
}

From source file:org.apache.james.mailbox.backup.ZipAssertTest.java

@Test
public void hasNoEntryShouldNotThrowWhenEmpty() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
        archiveOutputStream.finish();//  w  ww .j a v a  2 s  .com
    }

    try (ZipFile zipFile = new ZipFile(destination)) {
        assertThatCode(() -> assertThatZip(zipFile).hasNoEntry()).doesNotThrowAnyException();
    }
}

From source file:org.apache.james.mailbox.backup.ZipAssertTest.java

@Test
public void hasNoEntryShouldThrowWhenNotEmpty() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);//  w  w w. j a  v  a 2s .c om
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();

        archiveOutputStream.finish();
    }

    try (ZipFile zipFile = new ZipFile(destination)) {
        assertThatThrownBy(() -> assertThatZip(zipFile).hasNoEntry()).isInstanceOf(AssertionError.class);
    }
}

From source file:org.apache.james.mailbox.backup.ZipAssertTest.java

@Test
public void containsExactlyEntriesMatchingShouldNotThrowWhenBothEmpty() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
        archiveOutputStream.finish();/*  ww w  . ja v a 2s. c om*/
    }

    try (ZipFile zipFile = new ZipFile(destination)) {
        assertThatCode(() -> assertThatZip(zipFile).containsOnlyEntriesMatching()).doesNotThrowAnyException();
    }
}

From source file:org.apache.james.mailbox.backup.ZipAssertTest.java

@Test
public void containsExactlyEntriesMatchingShouldNotThrowWhenRightOrder() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);//from   w w w. java 2 s  . c  om
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();

        ZipArchiveEntry archiveEntry2 = (ZipArchiveEntry) archiveOutputStream
                .createArchiveEntry(new File("any"), ENTRY_NAME_2);
        archiveOutputStream.putArchiveEntry(archiveEntry2);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT_2), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();

        archiveOutputStream.finish();
    }

    try (ZipFile zipFile = new ZipFile(destination)) {
        assertThatCode(() -> assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(ENTRY_NAME),
                hasName(ENTRY_NAME_2))).doesNotThrowAnyException();
    }
}

From source file:org.apache.james.mailbox.backup.ZipAssertTest.java

@Test
public void hasNameShouldThrowWhenWrongName() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);/* www  . j  a  v a2s  .co  m*/
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();
        archiveOutputStream.finish();
    }

    try (ZipFile zipFile = new ZipFile(destination)) {
        assertThatThrownBy(() -> assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(ENTRY_NAME_2)))
                .isInstanceOf(AssertionError.class);
    }
}

From source file:org.apache.james.mailbox.backup.ZipAssertTest.java

@Test
public void isDirectoryShouldThrowWhenNotADirectory() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);/*w  w  w  .jav a2 s .  c om*/
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();
        archiveOutputStream.finish();
    }

    try (ZipFile zipFile = new ZipFile(destination)) {
        assertThatThrownBy(
                () -> assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(ENTRY_NAME).isDirectory()))
                        .isInstanceOf(AssertionError.class);
    }
}

From source file:org.apache.james.mailbox.backup.ZipAssertTest.java

@Test
public void isDirectoryShouldNotThrowWhenDirectory() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream
                .createArchiveEntry(new Directory("any"), DIRECTORY_NAME);
        archiveOutputStream.putArchiveEntry(archiveEntry);
        archiveOutputStream.closeArchiveEntry();
        archiveOutputStream.finish();/*from  ww  w  .  ja  v a  2 s .co  m*/
    }

    try (ZipFile zipFile = new ZipFile(destination)) {
        assertThatCode(
                () -> assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(DIRECTORY_NAME).isDirectory()))
                        .doesNotThrowAnyException();
    }
}