Java Zip压缩 解压缩

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
	/*
	 * 压缩文件或文件夹中的所有文件
	 * @param filepath 文件所在目录
	 * @param zippath 压缩后zip路径
	 * @param zipname 压缩后zip文件名
	 * @param dirFlag zip文件中第一层是否包含一级目录
	*/
	public static void zip2File(String filepath, String zippath, String zipname, boolean dirFlag) throws Exception {
		File file = new File(filepath);
        File zipPath = new File(zippath);
        if (!zipPath.exists())  
        {
        	zipPath.mkdirs();
        }
        ZipOutputStream zipOut = null;
        try {
            File zipFile = new File(zippath + File.separator + zipname);
            zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
	        if(file.isDirectory()){
	        	File[] files = file.listFiles();
	            for(File fileSec:files){
	            	if(dirFlag){
	            		_recursionZip(zipOut, fileSec, file.getName() + File.separator);
	                }else{
	                	_recursionZip(zipOut, fileSec, "");
	                }
	            }
	        }else{
	        	_recursionZip(zipOut, file, "");
	        }
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }finally{
	    	if(zipOut != null){
                zipOut.close();
            }
	    }
	}
    //web用这个
	public static void zip2Stream(String filepath, OutputStream out, boolean dirFlag) throws Exception{
		File file = new File(filepath);
        if (!file.exists())  
        {
            return;
        }  
        ZipOutputStream zipOut = null;
        try {
            zipOut = new ZipOutputStream(out);
	        if(file.isDirectory()){
	        	File[] files = file.listFiles();
	            for(File fileSec:files){
	            	if(dirFlag){
	            		_recursionZip(zipOut, fileSec, file.getName() + File.separator);
	                }else{
	                	_recursionZip(zipOut, fileSec, "");
	                }
	            }
	        }else{
	        	_recursionZip(zipOut, file, "");
	        }
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }finally{
            if(zipOut != null){
            	zipOut.close();
            }
        }
	}
	/*
	 * 迭代生成压缩文件
	 * @param zipOut zip输出流
	 * @param file 文件/目录
	 * @param baseDir 上级目录
	*/
	private static void _recursionZip(ZipOutputStream zipOut, File file, String baseDir) throws Exception{
		if(file.isDirectory()){
			File[] files = file.listFiles();
			for(File fileSec:files){
				_recursionZip(zipOut, fileSec, baseDir + file.getName() + File.separator);
	        }
	    }else{
	        byte[] buf = new byte[1024];
	        InputStream input = new FileInputStream(file);
	        zipOut.putNextEntry(new ZipEntry(baseDir + file.getName()));
	        int len;
	        while((len = input.read(buf)) != -1){
	        	zipOut.write(buf, 0, len);
	        }
	        zipOut.closeEntry();
	        input.close();
	    }
	}
	
	/*
	 * 解压一个zip文件(文件)
	 * @param zippath zip文件目录名(包括目录和名称)
	 * @param filepath 解压路径
	*/
	public static void unzipFromFile(String zippath, String filepath) throws Exception{
		File zip = new File(zippath);
        File filePath = new File(filepath);
        if (!filePath.exists())  
        {
        	filePath.mkdirs();
        }
        ZipInputStream zipInput = null;
        try {
        	zipInput = new ZipInputStream(new FileInputStream(zip));
    		_loopUnzip(zipInput, filepath);
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }finally{
            if(zipInput != null){
                zipInput.close();
            }
        }
	}
	/*
	 * 循环解压缩Zip文件
	 * 这里为什么要加上loop,只是为了跟压缩函数中的recursion保持一致而已
	 * @param zipInput zip输入流
	 * @param filepath 解压路径
	*/
	private static void _loopUnzip(ZipInputStream zipInput, String filepath) throws Exception{
		ZipEntry entry = null;
		while((entry = zipInput.getNextEntry()) != null){
			//不会解压空文件夹
        	if(entry.isDirectory()){
            	continue;
            }
            String entryFilePath = filepath + File.separator + entry.getName();
            File entryFile = new File(entryFilePath);
            if(!entryFile.getParentFile().exists()){
            	entryFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(entryFile);
			int temp = 0 ;
			while((temp=zipInput.read()) != -1){
				out.write(temp);
			}
			out.close();
			zipInput.closeEntry();
		}
	}
}

参考文献:

https://blog.csdn.net/u013087513/article/details/52151227

https://blog.csdn.net/qq_27828109/article/details/78019760

https://blog.csdn.net/carl_jiang/article/details/70599628

猜你喜欢

转载自blog.csdn.net/qq_24309981/article/details/82831148