Zip4jUtil压缩解压缩文件工具类

package hrhx.dhm.zip4j.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

public class Zip4jUtil {
	
	/**
	 * 压缩文件
	 * @param fileToAdd 文件压缩路径
	 * @param files 被压缩的文件路径
	 * @throws ZipException
	 */
	public static void zipFile(String fileToAdd,String files[]) throws ZipException{
		//文件压缩路径
		ZipFile zipFile = new ZipFile(fileToAdd);
		//被压缩的文件路径
		ArrayList filesToAdd = new ArrayList();
		for(String f:files){
			filesToAdd.add(new File(f));
		}
		ZipParameters parameters = new ZipParameters();
		parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
		parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);  
		zipFile.addFiles(filesToAdd, parameters);	
	}
	
	/**
	 * 压缩文件-加密
	 * @param fileToAdd 文件压缩路径
	 * @param files 被压缩的文件路径
	 * @param password 密码
	 * @throws ZipException
	 */
	public static void zipFile(String fileToAdd,String files[],String password) throws ZipException{
		//文件压缩路径
		ZipFile zipFile = new ZipFile(fileToAdd);
		//被压缩的文件路径
		ArrayList filesToAdd = new ArrayList();
		for(String f:files){
			filesToAdd.add(new File(f));
		}
		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);  
		//--AES
		parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);  
		parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);  
		parameters.setPassword(password); 
		zipFile.addFiles(filesToAdd, parameters);	
	}
	
	/**
	 * 压缩文件夹
	 * @param folderToAdd
	 * @param dirs
	 * @throws ZipException
	 */
	public static void zipDir(String folderToAdd,String dirs) throws ZipException{
		ZipFile zipFile = new ZipFile(folderToAdd);
		ZipParameters parameters = new ZipParameters();		
		parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);		
		parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
		zipFile.addFolder(dirs, parameters);
	}
	
	/**
	 * 压缩文件夹-加密
	 * @param folderToAdd
	 * @param dirs
	 * @throws ZipException
	 */
	public static void zipDir(String folderToAdd,String dirs,String password) throws ZipException{
		ZipFile zipFile = new ZipFile(folderToAdd);
		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);  
		//--AES
		parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);  
		parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);  
		parameters.setPassword(password); 
		zipFile.addFolder(dirs, parameters);
	}
	
	/**
	 * 压缩文件列表
	 * @param folderToAdd
	 * @throws ZipException
	 */
	@SuppressWarnings("unchecked")
	public static void zipShowList(String folderToAdd) throws ZipException{
		ZipFile zipFile = new ZipFile(folderToAdd);
		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("************************************************************");
		}
	}
	
	/**
	 * 解压zip
	 * @param folderOutAdd
	 * @param dir
	 * @throws ZipException
	 */
	public static void unzip(String folderOutAdd,String dir) throws ZipException{
		ZipFile zipFile = new ZipFile(folderOutAdd);          
		zipFile.extractAll(dir); 
	}
	
	/**
	 * 解压带密码的zip
	 * @param folderOutAdd
	 * @param dir
	 * @param password
	 * @throws ZipException
	 */
	public static void unzip(String folderOutAdd,String dir,String password) throws ZipException{
		ZipFile zipFile = new ZipFile(folderOutAdd); 
		if (zipFile.isEncrypted()) {  
			zipFile.setPassword(password);  
		}      
		zipFile.extractAll(dir); 
	}
	
	
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(Zip4jUtil压缩解压缩文件工具类)