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

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

Introduction

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

Prototype

public ZipFile(String name) throws IOException 

Source Link

Document

Opens the given file for reading, assuming "UTF8".

Usage

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);//from  w w w  .  j  ava2  s  .  c  o 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 .  ja v  a 2 s  .  c  om*/
        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);/*from   w  w  w.j a  v a 2 s .  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(EXTRA_FIELD)))
                        .doesNotThrowAnyException();
    }
}

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

@Test
void archiveShouldWriteEmptyValidArchiveWhenNoMessage() throws Exception {
    testee.archive(NO_MAILBOXES, Stream.of(), output);
    try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) {
        assertThatZip(zipFile).hasNoEntry();
    }//w  w  w  .j  a va2 s  .  co  m
}

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

@Test
void archiveShouldWriteOneMessageWhenOne() throws Exception {
    testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1), output);

    try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) {
        assertThatZip(zipFile).containsOnlyEntriesMatching(
                hasName(MESSAGE_ID_1.serialize()).hasStringContent(MESSAGE_CONTENT_1));
    }//w w w.j av a  2s.c  o m
}

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

@Test
void archiveShouldWriteTwoMessagesWhenTwo() throws Exception {
    testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1, MESSAGE_2), output);

    try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) {
        assertThatZip(zipFile).containsOnlyEntriesMatching(
                hasName(MESSAGE_ID_1.serialize()).hasStringContent(MESSAGE_CONTENT_1),
                hasName(MESSAGE_ID_2.serialize()).hasStringContent(MESSAGE_CONTENT_2));
    }//w  ww. j a  va2 s . co  m
}

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

@Test
void archiveShouldWriteMetadata() throws Exception {
    testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1), output);

    try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) {
        assertThatZip(zipFile).containsOnlyEntriesMatching(
                hasName(MESSAGE_ID_1.serialize()).containsExtraFields(new SizeExtraField(SIZE_1))
                        .containsExtraFields(new UidExtraField(MESSAGE_UID_1_VALUE))
                        .containsExtraFields(new MessageIdExtraField(MESSAGE_ID_1.serialize()))
                        .containsExtraFields(new MailboxIdExtraField(MAILBOX_ID_1))
                        .containsExtraFields(new InternalDateExtraField(MESSAGE_1.getInternalDate()))
                        .containsExtraFields(new FlagsExtraField(MESSAGE_1.createFlags())));
    }/*from www . jav a 2 s .c  o m*/
}

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

@Test
void archiveShouldWriteOneMailboxWhenPresent() throws Exception {
    testee.archive(ImmutableList.of(MAILBOX_1), Stream.of(), output);

    try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) {
        assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(MAILBOX_1.getName() + "/").isDirectory());
    }//  w w w .j  a  v  a2  s . co  m
}

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

@Test
void archiveShouldWriteMailboxesWhenPresent() throws Exception {
    testee.archive(ImmutableList.of(MAILBOX_1, MAILBOX_2), Stream.of(), output);

    try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) {
        assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(MAILBOX_1.getName() + "/").isDirectory(),
                hasName(MAILBOX_2.getName() + "/").isDirectory());
    }/* ww w .  j a va2  s .com*/
}

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

@Test
void archiveShouldWriteMailboxHierarchyWhenPresent() throws Exception {
    testee.archive(ImmutableList.of(MAILBOX_1, MAILBOX_1_SUB_1, MAILBOX_2), Stream.of(), output);

    try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) {
        assertThatZip(zipFile).containsOnlyEntriesMatching(hasName(MAILBOX_1.getName() + "/").isDirectory(),
                hasName(MAILBOX_1_SUB_1.getName() + "/").isDirectory(),
                hasName(MAILBOX_2.getName() + "/").isDirectory());
    }//from   w  w  w .  j ava2 s  . c o  m
}