All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.basics.util.extend.ZipUtil Maven / Gradle / Ivy

package cn.basics.util.extend;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import cn.basics.util.DataUtil;

/**
 * @ClassName: ZipUtil
 * @Description: TODO(java压缩、解压工具)
 * @author [email protected] (苟志强)
 * @date 2017-6-6 下午4:40:17
 */
public class ZipUtil {
	
	public static void main(String[] args){
		String dir = "E:\\test";// 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径 
		String zipPath = "E:\\";//压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹 
		String zipFileName = "test.zip";//压缩文件名
		try{
			zip(dir, zipPath, zipFileName);
		}catch (Exception e){
			e.printStackTrace();
		}

		String zipFilePath = "E:\\test.zip";//要解压的zip路径
		String unzipFilePath = "E:\\test2";//解压路径
		try{
			unzip(zipFilePath, unzipFilePath, true);
		}catch (Exception e){
			e.printStackTrace();
		}
	} 

	/** 
	 * 递归压缩文件夹 
	 * @param srcRootDir 压缩文件夹根目录的子路径 
	 * @param file 当前递归压缩的文件或目录对象 
	 * @param zos 压缩文件存储对象 
	 * @throws Exception 
	 */
	private static void zip(String srcRootDir, File file, ZipOutputStream zos) throws Exception{
		if (file == null){
			return;
		}
		//如果是文件,则直接压缩该文件
		if (file.isFile()){
			int count, bufferLen = 1024;
			byte data[] = new byte[bufferLen];

			//获取文件相对于压缩文件夹根目录的子路径
			String subPath = file.getAbsolutePath();
			int index = subPath.indexOf(srcRootDir);
			if (index != -1){
				subPath = subPath.substring(srcRootDir.length() + File.separator.length());
			}
			ZipEntry entry = new ZipEntry(subPath);
			zos.putNextEntry(entry);
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
			while ((count = bis.read(data, 0, bufferLen)) != -1){
				zos.write(data, 0, count);
			}
			bis.close();
			zos.closeEntry();
		}else{//如果是目录,则压缩整个目录
			//压缩目录中的文件或子目录
			File[] childFileList = file.listFiles();
			for (int n=0; n entries = (Enumeration)zip.entries();
		//循环对压缩包里的每一个文件进行解压 
		while(entries.hasMoreElements()){
			entry = entries.nextElement();
			//构建压缩包中一个文件解压后保存的文件全路径
			entryFilePath = unzipFilePath + File.separator + entry.getName();
			//构建解压后保存的文件夹路径
			index = entryFilePath.lastIndexOf(File.separator);
			if (index != -1){
				entryDirPath = entryFilePath.substring(0, index);
			}else{
				entryDirPath = "";
			} 
			entryDir = new File(entryDirPath);
			//如果文件夹路径不存在,则创建文件夹
			if (!entryDir.exists() || !entryDir.isDirectory()){
				entryDir.mkdirs();
			}

			//创建解压文件
			entryFile = new File(entryFilePath);
			if (entryFile.exists()){
				//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
				SecurityManager securityManager = new SecurityManager();
				securityManager.checkDelete(entryFilePath);
				//删除已存在的目标文件
				entryFile.delete(); 
			}

			//写入文件
			bos = new BufferedOutputStream(new FileOutputStream(entryFile));
			bis = new BufferedInputStream(zip.getInputStream(entry));
			while ((count = bis.read(buffer, 0, bufferSize)) != -1){
				bos.write(buffer, 0, count);
			}
			bos.flush();
			bos.close();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy