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 containsExactlyEntriesMatchingShouldNotThrowWhenRightOrder() throws Exception {
    try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {

        ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"),
                ENTRY_NAME);/*from   w ww  .  j a v  a 2 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)) {
        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);//from   www. 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)) {
        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  .  ja  va2 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).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();/* ww  w.  j a v  a  2s.  c o m*/
    }

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

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);//from  www .j  ava2s  . 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);//from   w  w w .j av a 2  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),
                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);/*  w w w .  j  a  v  a2  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)) {
        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 w w w.j a  v  a2  s. co 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 ww  .java 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)) {
        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);// w w w .  j  a  va  2 s . co  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();
    }
}