zip文件解压工具类

java解压zip文件

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

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

public class MyZipUtil {
    /**
     * 解压zip文件
     * @throws IOException 
     */
    public static String unZIP(String zipPath,String outPath ){
        ZipFile zipFile=null;
        try {
            zipFile = new ZipFile(zipPath,"GBK");             
    //压缩文件的实列,并设置编码
        //获取压缩文中的所以项
        for(Enumeration<ZipEntry> enumeration = zipFile.getEntries();enumeration.hasMoreElements();)
        {
            OutputStream os = null;
            BufferedOutputStream bos =null;
            InputStream is =null;
            BufferedInputStream bis =null;
            CheckedInputStream cos =null;
            ZipEntry zipEntry = null;
            try {
            zipEntry = enumeration.nextElement();//获取元素
            //排除空文件夹
            if(!zipEntry.getName().endsWith(File.separator))
            {
                System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息
                //创建解压目录
                File f = new File(outPath);
                //判断是否存在解压目录
                if(!f.exists())
                {
                    f.mkdirs();//创建解压目录
                }
                os = new FileOutputStream(outPath+zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/")+1));//创建解压后的文件
                bos = new BufferedOutputStream(os);//带缓的写出流
                is = zipFile.getInputStream(zipEntry);//读取元素
                bis = new BufferedInputStream(is);//读取流的缓存流
                cos = new CheckedInputStream(bis, new CRC32());//检查读取流,采用CRC32算法,保证文件的一致性
                byte [] b = new byte[1024*8];//字节数组,每次读取1024个字节
                //循环读取压缩文件的值
                while(cos.read(b)!=-1)
                {
                    bos.write(b);//写入到新文件
                }
                bos.flush();
                os.flush();
        
                
                
                
            }
            else
            {
                //如果为空文件夹,则创建该文件夹
                new File(outPath+zipEntry.getName()).mkdirs();
            }
            
            } catch (Exception e) {
                return "1";
            }finally {
                if(cos!=null){                    
                    cos.close();
                }
                if(bis!=null){                    
                    bis.close();
                }
                if(is!=null){                    
                    is.close();
                }
                if(bos!=null){                    
                    bos.close();
                }
                if(os!=null){                
                    os.close();
                }
                if(zipEntry!=null){                    
                    zipEntry.clone();
                }
            }
        }
        System.out.println("解压完成");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }finally {
            
            try {
                zipFile.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        return "0";
    }
}

猜你喜欢

转载自www.cnblogs.com/qq376324789/p/10213226.html