zip

import java.util.*;
import java.io.*;
import org.apache.tools.zip.*; 
/**
 * Author:lixiaoning
 * Date:2009-4-7
 */
public class Zipper
{
    private static String unrarCmd = "C:\\Program Files\\WinRAR\\UnRar x "; 
/**   
     * 压缩文件,支持中文路径及中文文件名 支持rar格式以及zip格式
     * compressFileName(指定压缩后的文件及路径):path+"/tempfile/test.zip"   
     * args(所要压缩的文件):
     * String args[]={path+"/tempfile/download_123.txt",
     * path+"/tempfile/download_456.txt",path+"/tempfile/download_789.txt"}   
     */  
public static boolean createZip(String compressFileName,String args[]){    
        boolean flag = false;    
        try{            
            byte b[] = new byte[512];       
            ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(compressFileName));       
            for(int i = 0; i < args.length; i++){       
               InputStream in = new FileInputStream(args[i]);     
               File file=new File(args[i]);    
               String filename = file.getName();//取得文件名    
//             ZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/')); //压缩后带路径     
               ZipEntry e = new ZipEntry(filename);         //压缩后不带路径    
               zout.putNextEntry(e);       
               int len=0;    
               while((len=in.read(b)) != -1){    
                  zout.write(b,0,len);    
               }    
               zout.closeEntry();                
             }    
             zout.close();    
             flag = true;    
        }catch(Exception e){    
            e.printStackTrace();    
        }    
        return flag;    
    }    
     /**   
      * 解压文件,支持中文路径及中文文件名 支持rar以及zip格式  
      * @param zipFile   
      * @param outFilePath   
      * @param mode   
      */   
     public static boolean unfile(String zipFile,String outFilePath,int mode){
    boolean flag=false;
    try{    
             File file = new File(zipFile);    
             String fileName = file.getName();    
             if(mode == 1)    
             {    
                 outFilePath += File.separator;  //文件当前路径下    
             }else{    
                 outFilePath += File.separator+fileName.substring(0,fileName.length()-4)+File.separator;    
             }    
             File tmpFileDir = new File(outFilePath);    
             tmpFileDir.mkdirs();    
 
             String unfile=zipFile.substring(zipFile.length()-3); 
             if(unfile=="zip"||unfile.equals("zip")){
            ZipFile zf = new ZipFile(zipFile);    
            FileOutputStream fos;    
            System.out.println("正在解压文件..." + zipFile);
            byte[] buf = new byte[1024];    
            for(Enumeration em = zf.getEntries(); em.hasMoreElements();){    
                 ZipEntry ze = (ZipEntry) em.nextElement();    
                 if(ze.isDirectory())    
                 {    
                     continue;    
                 }    
                 DataInputStream dis = new DataInputStream(zf.getInputStream(ze) );    
                 String currentFileName = ze.getName();    
                 int dex = currentFileName.lastIndexOf('/');    
                 String currentoutFilePath = outFilePath;    
                 if(dex > 0)    
                 {    
                      currentoutFilePath += currentFileName.substring(0,dex)+File.separator;    
                      File currentFileDir = new File(currentoutFilePath);    
                      currentFileDir.mkdirs();                        
                 }                   
                 fos = new FileOutputStream(outFilePath + ze.getName ( ));    
                 int readLen = 0;    
                 while((readLen = dis.read(buf,0,1024)) > 0 )    
                 {    
                  fos.write(buf , 0 ,readLen);    
                 }    
                 dis.close();    
                 fos.close();
             }
             System.out.println("成功解压文件: " + zipFile);
             flag = true;  
              }
              if(unfile=="rar"||unfile.equals("rar")){
                 unrarCmd += zipFile + " " + outFilePath;
                 try {   
                     System.out.println("正在解压文件..." + zipFile);
                     Runtime rt = Runtime.getRuntime();
                     Process p = rt.exec(unrarCmd);
                     System.out.println("成功解压文件: " + zipFile);
                     flag=true;
                 } catch (Exception e) {   
                     System.out.println(e.getMessage());      
                 }              
              }
 
            }catch(Exception e){    
                e.printStackTrace();    
            }    
    return flag;
     }
public static void main(String[] args)
{
try
{
Zipper zip=new Zipper();
String zipFile="F:\\新建文件夹.zip";
String outFilePath="F:\\";
int mode=1;
//测试解压
zip.unfile(zipFile, outFilePath, mode);
//测试压缩
// if(Zipper.createZip("F:\\新建文件夹.rar",aa)){
// System.out.println("成功压缩");
// }
}
catch (Exception e)
{
e.printStackTrace();
}


}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值