java解压zip文件 解决多目录出错

本文介绍了一种解决Zip文件解压时中文乱码的方法,通过使用Apache工具包中的ZipEntry和ZipOutputStream类,并针对文件层级较多的情况提供了解压方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1.   出现的问题
  2.   内容的中文乱码问题,这个问题网上很多人给出了解决方法,主要有两种方法:一是修改sun的源码;另一个是使用开源的类库org.apache.tools.zip.ZipOutputStream和org.apache.tools.zip.ZipEntry,这两个类ant.jar中有,可以直接下载使用即可。这个体现在getEntries()这个方法会报错。
  3.   这个段代码解决了文件的层级过多带来的解压不彻底的问题。
  4.   

  5. import java.io.BufferedOutputStream;  
  6. import java.io.File;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.util.Enumeration;  
  11. import org.apache.tools.zip.ZipEntry;  
  12. import org.apache.tools.zip.ZipFile;  
  13. /** 
  14.  *  解压Zip文件工具类 
  15.  * @author 
  16.  * 
  17.  */  
  18. public class ZipUtil {  
  19.   
  20.     private static final int buffer = 2048;  
  21.   
  22.       public static void main(String[] args)  
  23.         {  
  24.             unZip("D:\\test.zip");  
  25.         }  
  26.     /** 
  27.      * 解压Zip文件 
  28.      * @param path 文件目录 
  29.      */  
  30.     public static void unZip(String path)  
  31.         {  
  32.          int count = -1;  
  33.          String savepath = "";  
  34.   
  35.          File file = null;  
  36.          InputStream is = null;  
  37.          FileOutputStream fos = null;  
  38.          BufferedOutputStream bos = null;  
  39.   
  40.          savepath = path.substring(0, path.lastIndexOf(".")) + File.separator; //保存解压文件目录  
  41.          new File(savepath).mkdir(); //创建保存目录  
  42.          ZipFile zipFile = null;  
  43.          try  
  44.          {  
  45.              zipFile = new ZipFile(path,"gbk"); //解决中文乱码问题  
  46.              Enumeration<?> entries = zipFile.getEntries();  
  47.   
  48.              while(entries.hasMoreElements())  
  49.              {  
  50.                  byte buf[] = new byte[buffer];  
  51.   
  52.                  ZipEntry entry = (ZipEntry)entries.nextElement();  
  53.   
  54.                  String filename = entry.getName();  
  55.                  boolean ismkdir = false;  
  56.                  if(filename.lastIndexOf("/") != -1){ //检查此文件是否带有文件夹  
  57.                     ismkdir = true;  
  58.                  }  
  59.                  filename = savepath + filename;  
  60.   
  61.                  if(entry.isDirectory()){ //如果是文件夹先创建  
  62.                     file = new File(filename);  
  63.                     file.mkdirs();  
  64.                      continue;  
  65.                  }  
  66.                  file = new File(filename);  
  67.                  if(!file.exists()){ //如果是目录先创建  
  68.                     if(ismkdir){  
  69.                     new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs(); //目录先创建  
  70.                     }  
  71.                  }  
  72.                  file.createNewFile(); //创建文件  
  73.   
  74.                  is = zipFile.getInputStream(entry);  
  75.                  fos = new FileOutputStream(file);  
  76.                  bos = new BufferedOutputStream(fos, buffer);  
  77.   
  78.                  while((count = is.read(buf)) > -1)  
  79.                  {  
  80.                      bos.write(buf, 0, count);  
  81.                  }  
  82.                  bos.flush();  
  83.                  bos.close();  
  84.                  fos.close();  
  85.   
  86.                  is.close();  
  87.              }  
  88.   
  89.              zipFile.close();  
  90.   
  91.          }catch(IOException ioe){  
  92.              ioe.printStackTrace();  
  93.          }finally{  
  94.                 try{  
  95.                 if(bos != null){  
  96.                     bos.close();  
  97.                 }  
  98.                 if(fos != null) {  
  99.                     fos.close();  
  100.                 }  
  101.                 if(is != null){  
  102.                     is.close();  
  103.                 }  
  104.                 if(zipFile != null){  
  105.                     zipFile.close();  
  106.                 }  
  107.                 }catch(Exception e) {  
  108.                     e.printStackTrace();  
  109.                 }  
  110.             }  
  111.         }  
  112. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值