package com.tangcs.zhc.server.io;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.junit.Test;
import com.tangcs.zhc.server.ServerTestBase;
import com.tangcs.zhc.server.ServerUtils;
/**
*
* @author Warren Tang
*/
public class CompressionTest extends ServerTestBase {
/**
* http://java.sun.com/developer/technicalArticles/Programming/compression/
*/
@Test public void testDecompressZipFile() throws IOException {
ZipInputStream in = new ZipInputStream(getResourceAsStream("xls/exam-results.zip"));
String fileName;
ZipEntry entry;
while((entry = in.getNextEntry()) != null ) {
fileName = entry.getName();
OutputStream out = getTempFileAsStream(fileName);
ServerUtils.copyStream(in, out, false);
out.close();
}
in.close();
}
/**
* <p>IllegalArgumentException from ZipInputStream.getNextEntry()
* when zip file contains Non-UTF-8 (such as Chinese or Japanese) encoded characters.</p>
* <p>This is solved in JDK7:
* https://blogs.oracle.com/xuemingshen/entry/non_utf_8_encoding_in </p>
*/
@Test(expected = IllegalArgumentException.class)
public void testZipInputStreamDoesnotSupportZipFileContainingChineseNamedFiles()
throws IOException {
ZipInputStream in = new ZipInputStream(getResourceAsStream("xls/sample.zip"));
String fileName;
ZipEntry entry;
while((entry = in.getNextEntry()) != null ) {
fileName = entry.getName();
OutputStream out = getTempFileAsStream(fileName);
ServerUtils.copyStream(in, out, false);
out.close();
}
in.close();
}
/**
* For JDK5/6, use Apache Commons Compress:
* http://commons.apache.org/compress/zip.html
*/
@Test public void testAppacheCommonsCompressSupportZipFileContainingChineseNamedFiles()
throws IOException {
ZipArchiveInputStream in = new ZipArchiveInputStream(getResourceAsStream("xls/sample.zip"), "gbk", true);
ZipArchiveEntry entry;
while((entry = in.getNextZipEntry()) != null) {
String fileName = entry.getName();
OutputStream out = getTempFileAsStream(fileName);
ServerUtils.copyStream(in, out, false);
out.close();
}
in.close();
}
}