您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 4浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

java解压.zip文件

小志的博客 发布时间:2018-01-16 15:55:23 ,浏览量:4

package test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; 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 Test {    /** * 解压缩 *  * @param zipFilePath *            要解压的文件 * @param unzipFilePath *            解压到某文件夹 * @return */     public static void unzip(String zipFilePath, String unzipFilePath) throws Exception{         ZipFile zipFile = new ZipFile(zipFilePath,"GBK");//压缩文件的实列,并设置编码            //获取压缩文中的所以项            for(Enumeration enumeration = zipFile.getEntries();enumeration.hasMoreElements();)            {                ZipEntry zipEntry = enumeration.nextElement();//获取元素                String str1=zipEntry.getName();              //System.out.println(str1);              //排除空文件夹                if((zipEntry.getName()).indexOf("/")!=-1 && (zipEntry.getName()).indexOf(".")!=-1)                {                   // System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息                  String str=zipEntry.getName();                                    String strs=str.substring(0, str.lastIndexOf("/"));                  //创建解压目录                    File f = new File(unzipFilePath+"\\"+strs);                    //判断是否存在解压目录                    if(!f.exists())                    {                        f.mkdirs();//创建解压目录                    }                    OutputStream os = new FileOutputStream(unzipFilePath+"\\"+zipEntry.getName());//创建解压后的文件                    BufferedOutputStream bos = new BufferedOutputStream(os);//带缓的写出流                    InputStream is = zipFile.getInputStream(zipEntry);//读取元素                    BufferedInputStream bis = new BufferedInputStream(is);//读取流的缓存流                    CheckedInputStream cos = new CheckedInputStream(bis, new CRC32());//检查读取流,采用CRC32算法,保                    证文件的一致性                    byte [] b = new byte[1024];//字节数组,每次读取1024个字节                    //循环读取压缩文件的值                    while(cos.read(b)!=-1)                    {                        bos.write(b);//写入到新文件                    }                    cos.close();                    bis.close();                    is.close();                    bos.close();                    os.close();                }                else if((zipEntry.getName()).indexOf(".")!=-1)               {   \//如果为空文件夹,则创建该文件夹               //System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息                  //String str=zipEntry.getName();                                    //String strs=str.substring(0, str.lastIndexOf("."));                  //创建解压目录                    File f = new File(unzipFilePath);                    //判断是否存在解压目录                    if(!f.exists())                    {                        f.mkdirs();//创建解压目录                    }                    OutputStream os = new FileOutputStream(unzipFilePath+"\\"+zipEntry.getName());//创建解压后的文件                    BufferedOutputStream bos = new BufferedOutputStream(os);//带缓的写出流                    InputStream is = zipFile.getInputStream(zipEntry);//读取元素                    BufferedInputStream bis = new BufferedInputStream(is);//读取流的缓存流                    CheckedInputStream cos = new CheckedInputStream(bis, new CRC32());//检查读取流,采用CRC32算法,保                   证文件的一致性                    byte [] b = new byte[1024];//字节数组,每次读取1024个字节                    //循环读取压缩文件的值                    while(cos.read(b)!=-1)                    {                        bos.write(b);//写入到新文件                    }                    cos.close();                    bis.close();                    is.close();                    bos.close();                    os.close();                 }            }            //System.out.println("解压完成");            zipFile.close();               }       public static void main(String[] args) throws Exception { //zipFilePath 要解压的文件  String zipFilePath="D:\\test.zip"; //unzipFilePath 解压到某文件夹 String unzipFilePath="D:\\test"; //调用解压缩方法 unzip(zipFilePath,unzipFilePath);      }

}

解压后如下图:

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.2146s