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

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

Introduction

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

Prototype

public String getEncoding() 

Source Link

Document

The encoding to use for filenames and the file comment.

Usage

From source file:com.silverpeas.util.ZipManagerTest.java

/**
* Test of compressPathToZip method, of class ZipManager.
*
* @throws Exception//from   www .  ja  va 2  s  .  c  om
*/
@Test
public void testCompressPathToZip() throws Exception {
    String path = PathTestUtil.TARGET_DIR + "test-classes" + separatorChar + "ZipSample";
    String outfilename = PathTestUtil.TARGET_DIR + "temp" + separatorChar + "testCompressPathToZip.zip";
    ZipManager.compressPathToZip(path, outfilename);
    ZipFile zipFile = new ZipFile(new File(outfilename), CharEncoding.UTF_8);
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.getEntries();
        assertThat(zipFile.getEncoding(), is(CharEncoding.UTF_8));
        int nbEntries = 0;
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            nbEntries++;
        }
        assertThat(nbEntries, is(5));
        assertThat(zipFile.getEntry("ZipSample/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2b/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2a/simple.txt"), is(notNullValue()));

        ZipEntry accentuatedEntry = zipFile.getEntry("ZipSample/level1/level2a/s\u00efmplifi\u00e9.txt");
        if (accentuatedEntry == null) {
            accentuatedEntry = zipFile.getEntry("ZipSample/level1/level2a/"
                    + new String("smplifi.txt".getBytes("UTF-8"), Charset.defaultCharset()));
        }
        assertThat(accentuatedEntry, is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2c/"), is(nullValue()));
    } finally {
        zipFile.close();
    }
}

From source file:org.itstechupnorth.walrus.zip.ArchiveManagerTest.java

public void testOneRound() throws Exception {

    final File file = file("round.zip");
    ArchiveSaver manager = new ArchiveSaver(file);
    manager.open();//  w  w w . j av a  2 s. co m
    ArticleBuffer buffer = new ArticleBuffer();
    final String title = "Hello";
    buffer.title(title.toCharArray(), 0, 5);
    buffer.text("World".toCharArray(), 0, 5);
    manager.save(buffer);
    manager.close();

    ZipFile zip = new ZipFile(file);
    assertEquals("UTF8", zip.getEncoding());
    @SuppressWarnings("rawtypes")
    final Enumeration entries = zip.getEntries();
    assertEquals(true, entries.hasMoreElements());
    final ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement();
    assertEquals(title, entry.getComment());
    assertEquals(false, entries.hasMoreElements());
}

From source file:org.silverpeas.core.util.ZipUtilTest.java

/**
* Test of compressPathToZip method, of class ZipManager.
*
* @throws Exception//from w ww. j av a2 s.com
*/
@Test
public void testCompressPathToZip(MavenTestEnv mavenTestEnv) throws Exception {
    File path = new File(mavenTestEnv.getResourceTestDirFile(), "ZipSample");
    File outfile = new File(tempDir, "testCompressPathToZip.zip");
    ZipUtil.compressPathToZip(path, outfile);
    ZipFile zipFile = new ZipFile(outfile, CharEncoding.UTF_8);
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.getEntries();
        assertThat(zipFile.getEncoding(), is(CharEncoding.UTF_8));
        int nbEntries = 0;
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            nbEntries++;
        }
        assertThat(nbEntries, is(5));
        assertThat(zipFile.getEntry("ZipSample/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2b/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2a/simple.txt"), is(notNullValue()));

        ZipEntry accentuatedEntry = zipFile.getEntry("ZipSample/level1/level2a/s\u00efmplifi\u00e9.txt");
        if (accentuatedEntry == null) {
            accentuatedEntry = zipFile.getEntry("ZipSample/level1/level2a/"
                    + new String("smplifi.txt".getBytes("UTF-8"), Charset.defaultCharset()));
        }
        assertThat(accentuatedEntry, is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2c/"), is(nullValue()));
    } finally {
        zipFile.close();
    }
}