zoukankan      html  css  js  c++  java
  • 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";
        }
    }
  • 相关阅读:
    com.android.ide.common.process.PrecessException:org.gradle.process....finished with non-zero exit value 1
    android 学习笔记(1)
    C#遍历指定文件夹中的所有文件(转)
    让TextView里面的文字逐个显示的动画效果实现(1)
    This Handler class should be static or leaks might occur(null) 解决办法 (转)
    Android开发 旋转屏幕导致Activity重建解决方法(转)
    SQLite 数据库
    【Android】error opening trace file: No such file or directory (2)
    WPF中的RichTextBox
    android中的 Toast 和 AlertDialog
  • 原文地址:https://www.cnblogs.com/qq376324789/p/10213226.html
Copyright © 2011-2022 走看看