Java压缩解压ZIP之Zip4j

Zip4j是一个Java操作zip压缩格式的开源项目,功能强大而且使用方便,能完全满足Java操作Zip压缩文件。默认采用UTF-8编码,所以支持中文,同时也支持密码,而且支持多种压缩算法。 Zt-ZIP也不错,有兴趣的可以试试。

版本:zip4j_1.3.1.jar

http://www.lingala.net/zip4j/(需FQ)

压缩

1、文件压缩
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test1.zip");

ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\文件.doc"));
filesToAdd.add(new File("c:\\ZipTest\\파일.xls"));
filesToAdd.add(new File("c:\\ZipTest\\ファイル.ppt"));

ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 

zipFile.addFiles(filesToAdd, parameters);	


2、设置密码
--标准
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip");

ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\文件.doc"));
filesToAdd.add(new File("c:\\ZipTest\\파일.xls"));
filesToAdd.add(new File("c:\\ZipTest\\ファイル.ppt"));

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_STANDARD);	
parameters.setPassword("test123!");

zipFile.addFiles(filesToAdd, parameters);


--AES
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test3.zip");

ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\文件.doc"));
filesToAdd.add(new File("c:\\ZipTest\\파일.xls"));
filesToAdd.add(new File("c:\\ZipTest\\ファイル.ppt"));

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!");

zipFile.addFiles(filesToAdd, parameters);


3、文件夹压缩
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test4.zip");

String folderToAdd = "c:\\ZipTest";

ZipParameters parameters = new ZipParameters();		
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);		
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

zipFile.addFolder(folderToAdd, parameters);


4、将文件压缩到指定文件夹中
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test5.zip");

ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\文件.doc"));
filesToAdd.add(new File("c:\\ZipTest\\파일.xls"));
filesToAdd.add(new File("c:\\ZipTest\\ファイル.ppt"));

ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

parameters.setRootFolderInZip("test2/");

zipFile.addFiles(filesToAdd, parameters);


5、往ZIP中添加文件
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test1.zip");

ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setFileNameInZip("文件2.doc");
parameters.setSourceExternalStream(true);

InputStream is = new FileInputStream("c:\\ZipTest\\文件2.doc");

zipFile.addStream(is, parameters);

is.close();


6、分割压缩文件
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test7.zip");

ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\文件.doc"));
filesToAdd.add(new File("c:\\ZipTest\\파일.xls"));
filesToAdd.add(new File("c:\\ZipTest\\ファイル.ppt"));

ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 

// SplitLenth has to be greater than 65536 bytes
// zipFile.createZipFileFromFolder()
zipFile.createZipFile(filesToAdd, parameters, true, 65536);


7、创建ZIP流
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\文件.doc"));
filesToAdd.add(new File("c:\\ZipTest\\파일.xls"));
filesToAdd.add(new File("c:\\ZipTest\\ファイル.ppt"));

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();


8、从ZIP中删除文件
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test1.zip");

//删除指定文件
zipFile.removeFile("sample.txt");

//删除第一个文件
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");
}


9、获取ZIP中文件一览
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test5.zip");

List fileHeaderList = zipFile.getFileHeaders();

for (int i = 0; i < fileHeaderList.size(); i++) {
	FileHeader 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("************************************************************");
}


解压

1、解压所有文件
//方法一
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("test123!");
}
List fileHeaderList = zipFile2.getFileHeaders();
for (int i = 0; i < fileHeaderList.size(); i++) {
	FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
	zipFile2.extractFile(fileHeader, "c:\\ZipTest2\\");
}


2、解压所有文件到流
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip");		
if (zipFile.isEncrypted()) {
	zipFile.setPassword("test123!");
}

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");
	}
}


3、解压单个文件
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip");
if (zipFile.isEncrypted()) {
	zipFile.setPassword("test123!");
}

zipFile.extractFile("文件.doc", "c:\\ZipTest4\\");

你可能感兴趣的:(java压缩)