javazip壓縮和解壓縮

java原生的zip壓縮和解壓縮

缺點:含中文名的文件和文件夾名稱會變亂碼,但是通過這個類壓縮和解壓縮還是能能還原回去

第一次寫博客,希望有什麼不足之處,請大家提出

zip壓縮工具:

package com.wqj.file.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * ZIP壓縮工具
 *
 * @author  wqj
 * @since 1.0
 */
public class ZipUtils {
	private static final String EXT = ".zip";
	private static final String ZIPSEPARATOR = "/";
	/**
	 * 壓縮文件或文件夾
	 * @param destPath 壓縮文件保存路徑
	 * @param srcFile 需要解壓的文件或文件夾
	 * @throws IOException
	 */
	public static void zipFile(String destPath,File srcFile) throws IOException{
		File destFile = new File(destPath);
		zipFile(destFile, srcFile);
	}
	/**
	 * 壓縮文件或文件夾
	 * @param destFile 壓縮後的目錄文件
	 * @param srcFile 需壓縮的文件或文件夾
	 * @throws IOException
	 */
	public static void zipFile(File destFile,File srcFile) throws IOException{
		//對輸出文件做CRC32校驗
		CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(destFile), new CRC32());
		ZipOutputStream zipOut = new ZipOutputStream(cos);
		zipFile(srcFile, zipOut, "");
		zipOut.flush();
		zipOut.close();
	}
	/**
	 * 可以遞歸調用壓縮方法
	 * @param srcFile 需壓縮的源文件
	 * @param zipOut 壓縮輸出流
	 * @param basePath 上級名稱
	 * @throws IOException
	 */
	public static void zipFile(File srcFile,ZipOutputStream zipOut,String basePath) throws IOException{
			//判斷是目錄就直接putNextEntry
			if(srcFile.isDirectory()){
				File[] files = srcFile.listFiles();
				if(files.length == 0){
					zipOut.putNextEntry(new ZipEntry(basePath+srcFile.getName()+ZIPSEPARATOR));
					zipOut.closeEntry();
					System.out.println(basePath+srcFile.getName()+ZIPSEPARATOR);
				}
				//地櫃調用壓縮方法
				for(File file:files){
					zipFile(file,zipOut,basePath+srcFile.getName()+ZIPSEPARATOR);
				}
			//如果是文件對象節將該對象寫入壓縮文件
			}else{
				zipOut.putNextEntry(new ZipEntry(basePath+srcFile.getName()));
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
				System.out.println(basePath+srcFile.getName());
				int read = 0;
				byte[] buffer = new byte[1024];
				while((read = bis.read(buffer)) != -1){
					zipOut.write(buffer, 0, read);
				}
				bis.close();
				zipOut.closeEntry();
			}
	}
	/**
	 * 壓縮文件或文件夾
	 * @param compressPath 需要壓縮的文件或文件夾路徑
	 * @throws IOException
	 */
	public static void zipFile(String compressPath) throws IOException{
		File srcFile = new File(compressPath);
		if(!srcFile.exists()){
			throw new FileNotFoundException("not found file: "+srcFile.getAbsoluteFile());
		}
		String parentPath = srcFile.getParent();
		String name = srcFile.getName();
		String destPath = parentPath+ZIPSEPARATOR + name + EXT;
		zipFile(destPath,srcFile);
	}
	/**
	 * 創建文件
	 * @param file 需要創建的file對象
	 * @return
	 * @throws IOException
	 */
	public static boolean createFile(File file) throws IOException{
		if(!file.exists()){
			mkdir(file.getParentFile());
		}
		return file.createNewFile();
	}
	/**
	 *  創建目錄
	 * @param dir 需要創建的目錄
	 */
	public static void mkdir(File dir){
		if(!dir.exists()){
			mkdir(dir.getParentFile());
		}
		dir.mkdir();
	}
	/**
	 *	如果file對象父級不存在就遞歸創建父級
	 * @param file
	 */
	public static void fileProber(File file){
		File parentFile = file.getParentFile();
		if(!parentFile.exists()){
			fileProber(parentFile);
			parentFile.mkdir();
		}

	}
	public static void unzipFile(String unzipPath) throws Exception{
		File unzipFile = new File(unzipPath);
		String parentPath = unzipFile.getParent();
		String destPath = parentPath+ZIPSEPARATOR;
		unzipFile(unzipFile,destPath);
	}
	public static void unzipFile(File unzipFile,String destPath) throws Exception{
		CheckedInputStream cis = new CheckedInputStream(new FileInputStream(unzipFile), new CRC32());
		ZipInputStream zipIn = new ZipInputStream(cis);
		unzipFile(zipIn,destPath);
		zipIn.close();
	}
	public static void unzipFile(ZipInputStream zipIn,String destPath) throws Exception{
		File destFile = new File(destPath);
		unzipFile(zipIn, destFile);
	}
	public static void unzipFile(ZipInputStream zipIn,File destFile) throws Exception{
		ZipEntry entry = null;
		while((entry = zipIn.getNextEntry()) != null){
			String dir = destFile.getPath()+ZIPSEPARATOR+entry.getName();
			File dirFile = new File(dir);
			//檢查父級文件夾是否存在,不存在則創建父級
			fileProber(dirFile);
			//如果是文件夾類型就創建文件夾
			if(entry.isDirectory()){
				dirFile.mkdir();
			}else{
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dirFile));
				byte[] buffer = new byte[1024];
				int read = 0;
				while((read = zipIn.read(buffer)) != -1){
					bos.write(buffer, 0, read);
				}
				bos.close();
			}
			zipIn.closeEntry();

		}
	}
	public static void main(String[] args) {
		try {
//			ZipUtils.zipFile("F:\\qq聊天圖庫");
			ZipUtils.unzipFile(new File("F:\\qq聊天圖庫.zip"),"C:\\Test");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
壓縮後:

解壓縮後:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章