zip格式压缩和解压缩(支持中文和文件嵌套解压缩)

我在公司项目过程需要用到zip文件的压缩与解压的功能,于是自己研究了一下,找了一些,自己写了一个压缩和解压缩的代码,支持中文,可以文件嵌套(注意其中所用的类是ant.jar中的包中的类,我用的是1.6.0版本)

/**
   *用于对指定源文件路径下的所有文件压缩到指定的文件中
   * 
@param des String 需要压缩成的文件名
   * 
@param src String 压缩文件源路径,可以是文件或文件夹
   * 
@return boolean  如果压缩成功返回true,否则表示失败
   
*/

  
public   static   boolean  File2Zip(String des, String src)  {

    
boolean success = true//压缩成功标志
    File srcpath = new File(src);
    ZipOutputStream out 
= null;
    
try {
      out 
= new ZipOutputStream(new FileOutputStream(new File(
          des))); 
//创建压缩输出流
      out.setEncoding("gbk");
      compress(srcpath, out, 
""); //开始压缩
    }

    
catch (FileNotFoundException ex) {
          success 
= false;
    }

    
catch (IOException ex1) {
      success 
= false;
    }

    
finally {
      
if (out != null{
        
try {
          out.close();
        }

        
catch (IOException ex2) {
        }

      }

    }

    
return success;
  }


  
/**
   *
   * 
@param src File
   * 
@param out ZipOutputStream
   * 
@param base String
   * 
@throws IOException
   
*/

  
public   static   void  compress(File src, ZipOutputStream out, String base)  throws
      IOException 
{
    
if (src.isDirectory()) //如果是目录
      File[] files = src.listFiles(); //列举出目录中所有的内容
      out.putNextEntry(new ZipEntry(base + "/")); //放入一级目录
      base = base.length() == 0 ? "" : base + "/";
      
for (int i = 0; i < files.length; i++{
        compress(files[i], out, base 
+ files[i].getName());
      }


    }

    
else //如果是文件
        if("".equals(base)){
            out.putNextEntry(
new ZipEntry(base+"/"));
            base
=src.getName();
        }

      out.putNextEntry(
new ZipEntry(base));
      FileInputStream in 
= new FileInputStream(src);
      
byte []data=new byte[4096];
      
int b;
      
while((b=in.read(data))!=-1){
        out.write(data,
0,b);
      }

      in.close();

    }

  }

  
/**
   * 解压缩文件
   * 
@param src String  zip所在路径c:/test/kk.zip
   * 
@param des String 希望存放的目录
 * 
@throws IOException
   * 
@throws IOException
   
*/

  
public   static   void  decompress(String src, String des)  throws  IOException
     
{
      ZipFile file
=null;
      FileOutputStream fout
=null;
      DataOutputStream dout
=null;
      
try{
        file
=new ZipFile(src);
        InputStream in
=null;
      des
=des.replace('/''/');
      
if(des.startsWith("//")){
          des
=des.replaceFirst("//""//");
      }

     Enumeration  en
=file.getEntries();
   
while(en.hasMoreElements()){
     ZipEntry entry
=(ZipEntry)en.nextElement();
     
if(entry.isDirectory()){//文件夹
         File directory=new File(des+"/"+entry.getName());
         
if(!directory.exists())
             directory.mkdirs();
     }
else{//文件
         String path=entry.getName();
         path
=path.replace('/''/');
         
if(path.startsWith("//")){
             path
=path.replaceFirst("//""//");
            }

         
int pos=path.lastIndexOf('/');
         
if(pos!=-1){
             path
=path.substring(0, pos+1);
             File d
=new File(des+"/"+path);
             
if(!d.exists())
               d.mkdirs();
         }

         
try{
         File files
=new File(entry.getName());
         File f
=new File(des+"/"+files.getPath());

         fout
=new FileOutputStream(f);
         dout
=new DataOutputStream(fout);
         in
=file.getInputStream(entry);

         
byte[] b=new byte[4096];
          
int len = 0;
          
while ( (len = in.read(b)) != -1{
            dout.write(b, 
0, len);
          }


         }
catch(IOException e){
             
throw e;
         }
finally{
             
if(fout!=null)
              fout.close();
             
if(dout!=null)
              dout.close();
             
if(in!=null)
              in.close();
         }

     }

   }

      }
catch(IOException e){
          
throw e;
      }
finally{
          
if(file!=null)
              file.close();
          
if(fout!=null)
              fout.close();
          
if(dout!=null)
              dout.close();
      }

  }

  
/**
   * 解压缩zip文件
   * 
@param src String
   * 
@param des String
   * 
@return boolean 成功返回true;
   
*/

  
public   static   boolean  zipDecompress(String src, String des)  {
    
boolean success = true;
    
try {
      decompress(src, des);
    }

    
catch (FileNotFoundException ex) {
        ex.printStackTrace();
      success 
= false;
    }

    
catch (IOException ex) {
        ex.printStackTrace();
      success 
= false;
    }


    
return success;
  }

该类有很多不完善的地方,各位可以提意见,共同交流,谢谢

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值