java基础之zip(压缩、解压)

本程序依赖第三方包Ant.jar。因为java自带的java.utils.zip.ZipOutputStream对一些敏感中文路径会抛出异常。

package javax.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
 * @author hubiao
 * 	zip文件:压缩、解压
 * 		[支持中文路径]
 * 		用到工具类jar:org.apache.tools.zip.*
 */
@SuppressWarnings("unchecked")
public class ZIPUtils{
	public static void main(String[] args) throws Exception {
		/*压缩*/
		Compression(new File("F:\\aaaa"),null);
		
		/*解压*/
	//	Decompression(null,"F://aaaa.zip");
		
		//ZipOutputStream zos = new ZipOutputStream(os,Charset.forName("GBK"));//解决中文路径
	}
	/**
	 * 	递归打包成zip
	 * @param outPut  zip输出流
	 * @param listFiles	目录子文件
	 * @param fuPath	父级目录名称
	 * @throws Exception
	 */
	private static void createZip(org.apache.tools.zip.ZipOutputStream outPut,File[] listFiles,String fuPath) throws Exception {
		for(File f : listFiles)
		{
			String name = fuPath==null?f.getName():fuPath+"/"+f.getName();;
			if(f.isDirectory())
			{
				System.out.println("创建目录["+name+"]");
				outPut.putNextEntry(new ZipEntry(name+"/"));//这个/一定要加,不然无法创建文件夹咯!
				createZip(outPut,f.listFiles(),name);
			}else{
				System.out.println("创建文件["+name+"]");
				outPut.putNextEntry(new ZipEntry(name));
				InputStream is = new FileInputStream(f);
				byte[] bys = new byte[1024];
				int len = 0;
				while((len = is.read(bys))!=-1)
					outPut.write(bys, 0, len);
				is.close();
				outPut.flush();
			}
		}
	}
	/**
	 * 	解压文件
	 * @param savePath	解压保存路径
	 * @param targetFile zip源文件路径
	 * @throws IOException 解压错误
	 */
	private static void Decompression(String savePath, String target) throws IOException {
		long start = System.currentTimeMillis();
		//设置根路径或默认路径(解决路径含有  xx\xxx | aa/aaa )
		File targetFile = new File(target);
		//默认保存路径
		if(savePath==null)
			savePath = targetFile.toString().substring(0, targetFile.toString().lastIndexOf("."));
		else
			savePath = savePath.concat(targetFile.toString().substring(targetFile.toString().lastIndexOf("\\"), targetFile.toString().lastIndexOf(".")));
		
		//遍历所有zip文件
		ZipFile zipFile = new ZipFile(targetFile);
		Enumeration<ZipEntry> entries = zipFile.getEntries();
		OutputStream os = null;
		while(entries.hasMoreElements())
		{
			ZipEntry ze = entries.nextElement();
			String name = ze.getName();
			InputStream is = zipFile.getInputStream(ze);
			//创建文件夹
			int last = name.lastIndexOf("/");
			File destFile = null;
			if(last!=-1){
				destFile = new File(savePath,name.substring(0,last));
				destFile.mkdirs();
				if(name.indexOf(".")==-1)
					continue;
				destFile = new File(destFile,name.substring(last));//文件夹+文件名称
			}else{
				destFile = new File(savePath,name);//文件名称
			}
			System.out.println("正在解压["+name+"]");
			
			//读取zip
			byte[] bys = new byte[1024];
			int len = 0;
			os = new FileOutputStream(destFile);
			while((len = is.read(bys))!=-1)
			{
				os.write(bys,0,len);
				os.flush();
			}
			os.close();
		}
		long end = System.currentTimeMillis();
		System.out.println("****************解压完毕,消耗["+(end-start)+"(ms)];****************");
	}
	/**
	 * @param destDir	被压缩的目录
	 * @param target    压缩保存目录	
	 * @throws Exception 
	 */
	public static void Compression(File destDir, String saveDir) throws Exception {
		long start = System.currentTimeMillis();
		if(destDir==null || !destDir.exists())
			throw new RuntimeException(destDir.toString()+"  不存在!");
		if(saveDir==null)
			saveDir = destDir.toString()+".zip";
		ZipOutputStream outPut = new ZipOutputStream(new FileOutputStream(new File(saveDir)));
		outPut.setEncoding("GBK");//设置编码
		createZip(outPut,destDir.listFiles(),null);
		outPut.flush();
		outPut.close();
		long end = System.currentTimeMillis();
		System.out.println("****************压缩完毕,消耗["+(end-start)+"(ms)];****************");
	}
}
如:压缩运行结果:

创建目录[2013]
创建目录[2013/20130508]
创建文件[2013/20130508/20130508001.jpg]
创建文件[2013/20130508/20130508002.jpg]
创建文件[2013/20130508/20130508003.jpg]
创建文件[2013/20130508/20130508004.jpg]
创建文件[2013/20130508/2013050801.xml]
创建文件[2013/20130508/2013050802 - 副本.xml]
创建文件[2013/20130508/2013050802.xml]
创建文件[2013/20130508/2013050803 - 副本.xml]
创建文件[2013/20130508/2013050803.xml]
创建文件[2013/20130508/2013050804.xml]
创建目录[2013/20130508/images]
创建文件[2013/20130508/images/20130508011.jpg]
创建文件[2013/20130508/images/20130508021.jpg]
创建文件[2013/20130508/images/20130508041.jpg]
创建文件[2013/20130508/images/201305080410.jpg]
创建文件[2013/20130508/images/201305080411.jpg]
创建文件[2013/20130508/images/201305080412.jpg]
创建文件[2013/20130508/images/201305080413.jpg]
创建文件[2013/20130508/images/20130508042.jpg]
创建文件[2013/20130508/images/20130508043.jpg]
创建文件[2013/20130508/images/20130508044.jpg]
创建文件[2013/20130508/images/20130508045.jpg]
创建文件[2013/20130508/images/20130508046.jpg]
创建文件[2013/20130508/images/20130508047.jpg]
创建文件[2013/20130508/images/20130508048.jpg]
创建文件[2013/20130508/images/20130508049.jpg]
创建目录[2013/s]
创建文件[2013/s/2013050802.xml]
创建文件[2013/s/2013050803.xml]
创建文件[null/2013050801.xml]
创建文件[null/2013050802 - 副本.xml]
创建目录[车]
创建目录[车/要在]
创建目录[顺要要]
创建目录[顺要要/新建文件夹]
创建目录[顺要要/新建文件夹/sfds]
创建目录[顺要要/新建文件夹/sfds/aadf]
创建目录[顺要要/新建文件夹/新建文件夹 - 副本]
创建目录[顺要要/新建文件夹 - 副本]
****************压缩完毕,消耗[188(ms)];****************






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