Java Zip使用

文件压缩与解压缩实现
本文介绍了一个Java类,用于实现文件夹的压缩与解压缩功能。该类提供了压缩文件夹为ZIP格式的方法,并允许指定是否保留文件路径结构。同时,也提供了从ZIP文件中解压缩出文件的功能。

文件压缩及解压

public abstract class ZipFileUtil {

	protected final Logger logger = LoggerFactory.getLogger(ZipFileUtil.class);
	
	public static void main(String args[]){
		zip(new File("z:/config/config2"),false,new File("z:/config/config2.swf_back"));
	}

	/**
	 * 文件压缩
	 * @param folder 文件夹
	 * @param hasPath 是否包含文件目录结构
	 * @param zipFile 压缩后的ZIP文件
	 * @return
	 */
	public static boolean zip(File folder,boolean hasPath,File zipFile){
		boolean ret = false;
		ZipOutputStream out =  null;
		try{
			 BufferedInputStream origin = null;
	         FileOutputStream dest = new FileOutputStream(zipFile);
	         out = new ZipOutputStream(new BufferedOutputStream(dest));
	         byte data[] = new byte[1024];
	         File files[] = folder.listFiles();
	         for(int i = 0; i < files.length; i++){
	        	 FileInputStream fi = new FileInputStream(files[i]);
	             origin = new BufferedInputStream(fi,1024);
	             ZipEntry entry = new ZipEntry(files[i].getName());
	             out.putNextEntry(entry);
	             int count=-1;
	             while ((count = origin.read(data,0,1024)) != -1) {
	                  out.write(data, 0, count);
	             }
	             origin.close();
	          
	         }
	         out.close();
	         ret = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(out!=null)out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	
	@SuppressWarnings("unchecked")
	public static boolean unzip(File zipFile,boolean hasPath,File targetFolder){
		boolean ret = false;
		ZipFile _zipFile = null;
		try{
			String filePath =  targetFolder.getAbsolutePath()+"/";
	        _zipFile = new ZipFile(zipFile);
	        Enumeration emu = _zipFile.entries();
	        while(emu.hasMoreElements()){
                ZipEntry entry = (ZipEntry)emu.nextElement();
                //会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
                if (entry.isDirectory()) {
                    new File(filePath + entry.getName()).mkdirs();
                    continue;
                }
				System.out.println("-->"+filePath + entry.getName());

				BufferedInputStream bis = new BufferedInputStream(_zipFile.getInputStream(entry));
                File file = new File(filePath + entry.getName());
                //加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件
                //而这个文件所在的目录还没有出现过,所以要建出目录来。
                File parent = file.getParentFile();
                if(parent != null && (!parent.exists())){
                    parent.mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos,1024);           
                
                int count;
                byte data[] = new byte[1024];
                while ((count = bis.read(data, 0,1024)) != -1){
                    bos.write(data, 0, count);
                }
                bos.flush();
                bos.close();
                bis.close();
	        }
	        ret = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(_zipFile!=null)_zipFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	
}	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值