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.james.mailbox.backup.ZipAssertTest.java

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

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);//w  ww .  j  ava  2 s.co  m
        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 containsExactlyEntriesMatchingShouldThrowWhenExpectingMoreEntries() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);/* w  w  w.  ja  v  a 2s. c o m*/
        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)) {
        assertThatThrownBy(() -> assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(ENTRY_NAME),
                hasName(ENTRY_NAME_2), hasName("extraEntry"))).isInstanceOf(AssertionError.class);
    }
}

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

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

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);//from w w w  .j  a  v  a2 s.c  o m
        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)) {
        assertThatThrownBy(() -> assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(ENTRY_NAME)))
                .isInstanceOf(AssertionError.class);
    }
}

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

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

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);/*from  ww  w.j  av a  2  s  . c  o m*/
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();

        archiveOutputStream.finish();
    }

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

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

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

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);/*from   w w w .java 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)
                .containsOnlyEntriesMatching(hasName(ENTRY_NAME).hasStringContent(STRING_ENTRY_CONTENT_2)))
                        .isInstanceOf(AssertionError.class);
    }
}

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

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

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);//from w  ww . ja v a2  s.  c  o  m
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();

        archiveOutputStream.finish();
    }

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

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

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

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);/*  w  w  w.  jav  a 2  s.  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).containsExtraFields(EXTRA_FIELD)))
                        .isInstanceOf(AssertionError.class);
    }
}

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

@Test
public void containsExactlyExtraFieldsShouldNotThrowWhenUnexpectedField() 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  o  m*/
        archiveEntry.addExtraField(EXTRA_FIELD);
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();

        archiveOutputStream.finish();
    }

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

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

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

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);//  w  ww  .j  a va 2  s . co  m
        archiveEntry.addExtraField(EXTRA_FIELD);
        archiveOutputStream.putArchiveEntry(archiveEntry);
        IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();

        archiveOutputStream.finish();
    }

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

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

@Override
public void archive(List<Mailbox> mailboxes, Stream<MailboxMessage> messages, OutputStream destination)
        throws IOException {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
        storeMailboxes(mailboxes, archiveOutputStream);
        storeMessages(messages, archiveOutputStream);
        archiveOutputStream.finish();//w w  w. j a  v a 2  s.  c  o m
    }
}