import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import de.idyl.winzipaes.AesZipFileDecrypter;
import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESDecrypterBC;
import de.idyl.winzipaes.impl.AESEncrypterBC;
import de.idyl.winzipaes.impl.ExtZipEntry;
import de.idyl.winzipaes.impl.ExtZipOutputStream;
//http://xjlsgcjdtc.iteye.com/blog/1439514
public class Test_winzip_aes {
public static void main(String[] args) throws Exception {
test001();
//test002();
}
private static void test002() throws Exception {
AESDecrypterBC bc = new AESDecrypterBC();
AesZipFileDecrypter azfd = new AesZipFileDecrypter(new File(
"c:/ZipTest/winzip_aes.zip"), bc);
ExtZipEntry zipEntry=new ExtZipEntry("c:/fuck");
azfd.extractEntry(zipEntry, new File("test.txt"), "123456!");
}
private static void test001() throws Exception, Exception {
AESEncrypterBC bc = new AESEncrypterBC();
AesZipFileEncrypter azfe = new AesZipFileEncrypter(new File(
"c:/ZipTest/winzip_aes.zip"), bc);
azfe.setEncoding("UTF-8"); // 编码格式是在这传进去的
String key="123456!";
File file=new File("d:/test.html");
azfe.add(file, file.getName(), key);
file=new File("d:/test.txt");
azfe.add(file, file.getName(), key);
file=new File("d:/test.png");
azfe.add(file, file.getName(), key);
azfe.close();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import net.sf.jazzlib.ZipEntry;
import net.sf.jazzlib.ZipOutputStream;
//http://jazzlib.sourceforge.net/
//A pure java implementation of the java.util.zip library
public class TestJazzlib {
private static void zipFile(File[] subs, String baseName, ZipOutputStream zos)
throws IOException {
for (int i = 0; i < subs.length; i++) {
File f = subs[i];
zos.putNextEntry(new ZipEntry(baseName + f.getName()));
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
}
public static void main(String[] args) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("d:/tmp/jazzlib.zip"));
File[] files = new File[3];
files[0] = new File("d:/test.txt");
files[1] = new File("d:/test.xls");
files[2] = new File("d:/test.bmp");
zipFile(files, "", zos);
zos.flush();
zos.close();
}
}
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
//https://github.com/ymnk/jzlib
//JZlib 是纯 Java 实现的一个新的 zlib 压缩和解压缩包。
public class TestJzlib {
private static final byte[] INVALID = {120, -38, 98, 102, -30, 112, 76, 78, -50, 47, -51, 43, 41, 102, 100, 96, 96, -80, -25, -11, 77, 45, 46, 78, 76, 79, 117, 78, -51, 43, 73, 45, 98, 100, -80, -29, -25, 101, -51, -55, 79, -49, -52, 99, 100, 20, 41, 73, 73, -116, -49, -51, 79, -118, 79, -52, 75, 41, -54, -49, 76, -47, 51, 51, -41, -77, -112, 116, -124, 112, 20, -126, 93, -68, 21, -110, 74, 51, 115, 74, 20, -46, -14, -117, 20, 42, 44, -52, 88, 77, -12, 76, -12, -116, -22, -21, -103, 67, -4, -125, -21, 89, 83, -117, -77, -13, -117, 88, 10, -127, 96, 127, 123, 125, 61, 64};
public void testJzlib() throws IOException {
final byte[] source = INVALID;
ByteArrayInputStream input = new ByteArrayInputStream(source);
InputStream stream = new com.jcraft.jzlib.ZInputStream(input);
byte[] bytes = new byte[512];
int read = stream.read(bytes);
System.out.println("Read: " + read);
System.out.println("Bytes: " + Arrays.toString(bytes));
}
public void testNative() throws IOException {
final byte[] source = INVALID;
ByteArrayInputStream input = new ByteArrayInputStream(source);
InputStream stream = new java.util.zip.InflaterInputStream(input);
byte[] bytes = new byte[512];
int read = stream.read(bytes);
System.out.println("Read: " + read);
System.out.println("Bytes: " + Arrays.toString(bytes));
}
public static void main(String[] args) throws IOException {
TestJzlib lib=new TestJzlib();
lib.testJzlib();
lib.testNative();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.ZipInputStream;
import net.lingala.zip4j.io.ZipOutputStream;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.unzip.UnzipUtil;
import net.lingala.zip4j.util.Zip4jConstants;
//http://www.lingala.net/zip4j/ 需要翻墙
//http://www.cnblogs.com/kgdxpr/archive/2013/08/01/3230174.html
public class TestZip4j {
/**
Zip4j是一个Java操作zip压缩格式的开源项目,功能强大而且使用方便,能完全满足Java操作Zip压缩文件。
默认采用UTF-8编码,所以支持中文,同时也支持密码,而且支持多种压缩算法。
*/
public static void main(String[] args) throws Exception {
//testEncryptFiles();
//testAES();
//testDir();
//testFileToDir();
//testAddFile();
//test分隔压缩文件();
//test创建ZIP流();
//test从ZIP中删除文件();
//test获取ZIP中文件一览();
//test解压所有文件();
//test解压所有文件到流();
test解压单个文件();
}
private static void test解压单个文件() throws Exception {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip");
if (zipFile.isEncrypted()) {
zipFile.setPassword("123456!");
}
zipFile.extractFile("test.png", "c:\\ZipTest4\\");
}
private static void test解压所有文件到流() throws Exception {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip");
if (zipFile.isEncrypted()) {
zipFile.setPassword("123456!");
}
List fileHeaderList = zipFile.getFileHeaders();
for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
if (fileHeader != null) {
String outFilePath = "c:\\ZipTest3\\" + System.getProperty("file.separator") + fileHeader.getFileName();
File outFile = new File(outFilePath);
if (fileHeader.isDirectory()) {
outFile.mkdirs();
continue;
}
File parentDir = outFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
ZipInputStream is = zipFile.getInputStream(fileHeader);
OutputStream os = new FileOutputStream(outFile);
int readLen = -1;
byte[] buff = new byte[4096];
while ((readLen = is.read(buff)) != -1) {
os.write(buff, 0, readLen);
}
os.close();
os = null;
is.close();
is = null;
UnzipUtil.applyFileAttributes(fileHeader, outFile);
System.out.println("Done extracting: " + fileHeader.getFileName());
} else {
System.err.println("fileheader is null. Shouldn't be here");
}
}
}
private static void test解压所有文件() throws ZipException {
//方法一
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test1.zip");
zipFile.extractAll("c:\\ZipTest1");
//方法二
ZipFile zipFile2 = new ZipFile("c:\\ZipTest\\test2.zip");
if (zipFile2.isEncrypted()) {
zipFile2.setPassword("123456!");
}
List fileHeaderList = zipFile2.getFileHeaders();
for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
zipFile2.extractFile(fileHeader, "c:\\ZipTest2\\");
}
}
private static void test获取ZIP中文件一览() throws ZipException {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test4.zip");
List<FileHeader> fileHeaderList = zipFile.getFileHeaders();
for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = fileHeaderList.get(i);
System.out.println("****File Details for: " + fileHeader.getFileName() + "*****");
System.out.println("Name: " + fileHeader.getFileName());
System.out.println("Compressed Size: " + fileHeader.getCompressedSize());
System.out.println("Uncompressed Size: " + fileHeader.getUncompressedSize());
System.out.println("CRC: " + fileHeader.getCrc32());
System.out.println("************************************************************");
}
}
private static void test从ZIP中删除文件() throws Exception {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test5.zip");
//删除指定文件
zipFile.removeFile("add.html");
//删除第一个文件
if (zipFile.getFileHeaders() != null && zipFile.getFileHeaders().size() > 0) {
zipFile.removeFile((FileHeader)zipFile.getFileHeaders().get(0));
} else {
System.out.println("This cannot be demonstrated as zip file does not have any files left");
}
}
private static void test创建ZIP流() throws Exception {
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("d:/test.txt"));
filesToAdd.add(new File("d:/test.png"));
filesToAdd.add(new File("d:/test.html"));
ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(new File("c:\\ZipTest\\test8.zip")));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
// Set password
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword("test123!");
for (int i = 0; i < filesToAdd.size(); i++) {
File file = (File)filesToAdd.get(i);
outputStream.putNextEntry(file,parameters);
if (file.isDirectory()) {
outputStream.closeEntry();
continue;
}
InputStream inputStream = new FileInputStream(file);
byte[] readBuff = new byte[4096];
int readLen = -1;
while ((readLen = inputStream.read(readBuff)) != -1) {
outputStream.write(readBuff, 0, readLen);
}
outputStream.closeEntry();
inputStream.close();
}
outputStream.finish();
outputStream.close();
}
private static void test分隔压缩文件() throws ZipException {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test7.zip");
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("d:/test.txt"));
filesToAdd.add(new File("d:/test.png"));
filesToAdd.add(new File("d:/test.html"));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.createZipFile(filesToAdd, parameters, true, 65536);
}
private static void testAddFile() throws Exception {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setFileNameInZip("add.html");
parameters.setSourceExternalStream(true);
InputStream is = new FileInputStream("d:/add.html");
zipFile.addStream(is, parameters);
is.close();
}
private static void testFileToDir() throws ZipException {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test5.zip");
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("d:/test.txt"));
filesToAdd.add(new File("d:/test.png"));
filesToAdd.add(new File("d:/test.html"));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setRootFolderInZip("test2/");
zipFile.addFiles(filesToAdd, parameters);
}
private static void testDir() throws ZipException {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test4.zip");
String folderToAdd = "D:\\html\\about";
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.addFolder(folderToAdd, parameters);
}
private static void testAES() throws ZipException {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip");
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("d:/test.txt"));
filesToAdd.add(new File("d:/test.png"));
filesToAdd.add(new File("d:/test.html"));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//设置密码
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword("123456!");
zipFile.addFiles(filesToAdd, parameters);
}
private static void testEncryptFiles() throws ZipException {
ZipFile zipFile = new ZipFile("c:\\date.zip");
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("d:/test.txt"));
filesToAdd.add(new File("d:/test.png"));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//设置密码
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
parameters.setPassword("123456");
zipFile.addFiles(filesToAdd, parameters);
}
}
import java.io.File;
import org.zeroturnaround.zip.NameMapper;
import org.zeroturnaround.zip.ZipUtil;
//https://github.com/zeroturnaround/zt-zip/tree/master
public class TestZT_zip {
public static void main(String[] args) {
final String prefix = "doc/";
ZipUtil.unpack(new File("d:/tmp/demo.zip"), new File("d:/tmp/demo"), new NameMapper() {
public String map(String name) {
return name.startsWith(prefix) ? name.substring(prefix.length()) : name;
}
});
ZipUtil.pack(new File("D:\\html\\about"), new File("d:/tmp/pack.zip"), new NameMapper() {
public String map(String name) {
// TODO Auto-generated method stub
return name.startsWith(prefix) ? name.substring(prefix.length()) : name;
}
});
}
}
源码: http://pan.baidu.com/s/1hqFxq76