java zip压缩解压

使用java自带的zip包压缩、解压文件/文件夹。显示进度。在windows XP SP3 + jdk1.7下测试不存在中文路径乱码问题。

import java.io.*;  
import java.util.zip.*;

public class Zip {
	
	int filesCount;//总数量
	int now;//已执行数量
	ZipInputStream Zin;
	
	/**
	 * 压缩文件或文件夹
	 * @param srcPath 被压缩的文件或文件夹
	 * @param targetFolderPath 压缩文件存放路径
	 * @throws Exception
	 */
    public void zip(String srcPath,String targetFolderPath) throws Exception
    {  
    	filesCount = getCount(srcPath);
    	now = 0;
    	
    	File fsrc = new File(srcPath);
    	File ftg = new File(targetFolderPath);
    	if(!fsrc.exists() || !ftg.exists()){
    		throw new Exception("Zip::zip 路径不存在");
    	}
    	
        ZipOutputStream out = new ZipOutputStream(
        		new FileOutputStream(targetFolderPath + File.separator + fsrc.getName() + ".zip"));  
        BufferedOutputStream bo = new BufferedOutputStream(out);  
        zip(out, fsrc, fsrc.getName(), bo);  
        bo.close();  
        out.close(); //输出流关闭
    }  
  
    private void zip(ZipOutputStream out, File f, String base, BufferedOutputStream bo) throws Exception 
    { 
        if (f.isDirectory()) {
        	
            File[] fl = f.listFiles();  
            if (fl.length == 0) {
            	//创建zip压缩进入点base
                out.putNextEntry(new ZipEntry(base + File.separator));   
            }  
            for (int i = 0; i < fl.length; i++) {
            	//递归遍历子文件夹  
                zip(out, fl[i], base + File.separator + fl[i].getName(), bo); 
            }    
        } 
        else {  
            out.putNextEntry(new ZipEntry(base)); //创建zip压缩进入点base
            bo.write(FileOperate.readAllBytes(f));
            bo.flush();//此处重要,如没有该句,可能会造成文件不完整。
        }  
        
        now++;
        System.out.println(now*100/filesCount);
    }
    
    /**
     * 获取压缩总数量
     * @param folderPath
     * @return
     */
	int getCount(String path)
	{
		int count = 0;
		File file = new File(path);
		
		if(file.isFile()){
			count = 1;
		}
		else if(file.isDirectory())
		{
			File[] files = file.listFiles(); 
			if(files != null){
				for(File f : files){
					count +=1;
					if(f.isDirectory()){
						count += getCount(f.getAbsolutePath());
					}
				}
			}
		}
		return count;
	}
    
	/**
	  *获得解压缩总数量
	*/
    public int getZipCount(String zipFilePath)
    {
  	  int zipFileCount = 0;

  	  try{	  
  		  Zin = new ZipInputStream(new FileInputStream(zipFilePath));
  		  
  		  while (Zin.getNextEntry()!= null){
  			  zipFileCount++;
  		  } 
  		  return zipFileCount;
  	  }catch(IOException e){
  		  e.printStackTrace();
  		  return -1;
  	  }
  	} 
    
    /**
     * 解压缩文件
     * @param zipFilePath zip源文件
     * @param targetFolder 解压到什么位置
     * @throws FileNotFoundException
     */
    public void unzip(String zipFilePath,String targetFolderPath) throws Exception
    {
    	File fsrc = new File(zipFilePath);
    	File ftg = new File(targetFolderPath);
    	
    	if(!fsrc.exists() || !ftg.exists()){
    		throw new Exception("Zip::unzip 路径不存在");
    	}
    	
    	filesCount = getZipCount(zipFilePath);
    	now = 0;
        
        Zin = new ZipInputStream(new FileInputStream(zipFilePath));
        BufferedInputStream Bin = new BufferedInputStream(Zin);    
        File Fout = null;  
        ZipEntry entry;
        
        while((entry = Zin.getNextEntry())!= null){

        	if(!entry.isDirectory()){
            	Fout = new File(targetFolderPath,entry.getName());
                
                if(!Fout.exists()){  
                    (new File(Fout.getParent())).mkdirs();  
                }  
                
                try{
                    FileOutputStream out = new FileOutputStream(Fout);  
                    BufferedOutputStream Bout=new BufferedOutputStream(out);  
                    
                    int b;  
                    while((b=Bin.read())!=-1){  
                        Bout.write(b);  
                    }  
                    
                    Bout.close();  
                    out.close();
                }
                catch(Exception ex){
                }
        	}
          
            now++;
            System.out.println(now*100/filesCount);
        }  
        Bin.close();  
        Zin.close();  
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值