关于java解压zip与rar的问题

    因为想用rar的解压缩在8100,文件的解压缩问题,大家都知道,比较常见的压缩文件有rar,zip,然而rar,zip的区别又在哪?说一点,zip 压缩算法是免费开放的,任何人可以免费使用。但是 RAR 就不一样了, 
这个压缩算法已经受到专利权的保护,如果要使用 RAR 算法必须向其专利所有人支付费用。所以在一般的开源网站,apache,sourceforge等开源网站上的开源项目一般都用zip格式.本人所学语言主要为java自然会想用java去解压,java去解压zip比较容易,apache提供的开源项目ant,我在网上找一下,找到了sourceforge的开源项目unrar专用于压缩,解压rar.只可惜没有文档(让人即喜,又悲).

     下面是一个对zip,rar进行解压的程序

      

Java代码 

  1. import org.apache.tools.tar.TarEntry;  
  2. import org.apache.tools.tar.TarOutputStream;  
  3. import org.apache.tools.zip.ZipEntry;  
  4. import org.apache.tools.zip.ZipFile;  
  5. import org.apache.tools.zip.ZipOutputStream;  

Java代码 

  1. import de.innosystec.unrar.Archive;  

Java代码 

  1. /**  
  2.  *  *   
  3.  * @version 创建时间:Feb 26, 2009 6:01:11 PM  
  4.  * 类说明:压缩、解压文件公用类 
  5.  * 
  6.  */  
  7. public class Decompression {  
  8. private static final int BUFFEREDSIZE = 1024;  
  9.         
  10.     /** 
  11.      * 解压zip格式的压缩文件到指定位置 
  12.      * @param zipFileName 压缩文件 
  13.      * @param extPlace 解压目录 
  14.      * @throws Exception 
  15.      */  
  16.     @SuppressWarnings("unchecked")  
  17.     public synchronized void unzip(String zipFileName, String extPlace) throws Exception {  
  18.         try {  
  19.             (new File(extPlace)).mkdirs();  
  20.             File f = new File(zipFileName);  
  21.             ZipFile zipFile = new ZipFile(zipFileName);  
  22.             if((!f.exists()) && (f.length() <= 0)) {  
  23.                 throw new Exception("要解压的文件不存在!");  
  24.             }  
  25.             String strPath, gbkPath, strtemp;  
  26.             File tempFile = new File(extPlace);  
  27.             strPath = tempFile.getAbsolutePath();  
  28.             java.util.Enumeration e = zipFile.getEntries();  
  29.             while(e.hasMoreElements()){  
  30.                 org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();  
  31.                 gbkPath=zipEnt.getName();  
  32.                 if(zipEnt.isDirectory()){  
  33.                     strtemp = strPath + File.separator + gbkPath;  
  34.                     File dir = new File(strtemp);  
  35.                     dir.mkdirs();  
  36.                     continue;  
  37.                 } else {  
  38.                     //读写文件  
  39.                     InputStream is = zipFile.getInputStream(zipEnt);  
  40.                     BufferedInputStream bis = new BufferedInputStream(is);  
  41.                     gbkPath=zipEnt.getName();  
  42.                     strtemp = strPath + File.separator + gbkPath;  
  43.                     
  44.                     //建目录  
  45.                     String strsubdir = gbkPath;  
  46.                     for(int i = 0; i < strsubdir.length(); i++) {  
  47.                         if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {  
  48.                             String temp = strPath + File.separator + strsubdir.substring(0, i);  
  49.                             File subdir = new File(temp);  
  50.                             if(!subdir.exists())  
  51.                             subdir.mkdir();  
  52.                         }  
  53.                     }  
  54.                     FileOutputStream fos = new FileOutputStream(strtemp);  
  55.                     BufferedOutputStream bos = new BufferedOutputStream(fos);  
  56.                     int c;  
  57.                     while((c = bis.read()) != -1) {  
  58.                         bos.write((byte) c);  
  59.                     }  
  60.                     bos.close();  
  61.                     fos.close();  
  62.                 }  
  63.             }  
  64.         } catch(Exception e) {  
  65.             e.printStackTrace();  
  66.             throw e;  
  67.         }  
  68.     }  
  69.         
  70.     /** 
  71.      * 解压zip格式的压缩文件到指定位置 
  72.      * @param zipFileName 压缩文件 
  73.      * @param extPlace 解压目录 
  74.      * @throws Exception 
  75.      */  
  76.     @SuppressWarnings("unchecked")  
  77.     public synchronized void unzip(String zipFileName, String extPlace,boolean whether) throws Exception {  
  78.         try {  
  79.             (new File(extPlace)).mkdirs();  
  80.             File f = new File(zipFileName);  
  81.             ZipFile zipFile = new ZipFile(zipFileName);  
  82.             if((!f.exists()) && (f.length() <= 0)) {  
  83.                 throw new Exception("要解压的文件不存在!");  
  84.             }  
  85.             String strPath, gbkPath, strtemp;  
  86.             File tempFile = new File(extPlace);  
  87.             strPath = tempFile.getAbsolutePath();  
  88.             java.util.Enumeration e = zipFile.getEntries();  
  89.             while(e.hasMoreElements()){  
  90.                 org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();  
  91.                 gbkPath=zipEnt.getName();  
  92.                 if(zipEnt.isDirectory()){  
  93.                     strtemp = strPath + File.separator + gbkPath;  
  94.                     File dir = new File(strtemp);  
  95.                     dir.mkdirs();  
  96.                     continue;  
  97.                 } else {  
  98.                     //读写文件  
  99.                     InputStream is = zipFile.getInputStream(zipEnt);  
  100.                     BufferedInputStream bis = new BufferedInputStream(is);  
  101.                     gbkPath=zipEnt.getName();  
  102.                     strtemp = strPath + File.separator + gbkPath;  
  103.                     
  104.                     //建目录  
  105.                     String strsubdir = gbkPath;  
  106.                     for(int i = 0; i < strsubdir.length(); i++) {  
  107.                         if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {  
  108.                             String temp = strPath + File.separator + strsubdir.substring(0, i);  
  109.                             File subdir = new File(temp);  
  110.                             if(!subdir.exists())  
  111.                             subdir.mkdir();  
  112.                         }  
  113.                     }  
  114.                     FileOutputStream fos = new FileOutputStream(strtemp);  
  115.                     BufferedOutputStream bos = new BufferedOutputStream(fos);  
  116.                     int c;  
  117.                     while((c = bis.read()) != -1) {  
  118.                         bos.write((byte) c);  
  119.                     }  
  120.                     bos.close();  
  121.                     fos.close();  
  122.                 }  
  123.             }  
  124.         } catch(Exception e) {  
  125.             e.printStackTrace();  
  126.             throw e;  
  127.         }  
  128.     }  
  129.     /** 
  130.      * 压缩zip格式的压缩文件 
  131.      * @param inputFilename 压缩的文件或文件夹及详细路径 
  132.      * @param zipFilename 输出文件名称及详细路径 
  133.      * @throws IOException 
  134.      */  
  135.     public synchronized void zip(String inputFilename, String zipFilename) throws IOException {  
  136.         zip(new File(inputFilename), zipFilename);  
  137.     }  
  138.         
  139.     /** 
  140.      * 压缩zip格式的压缩文件 
  141.      * @param inputFile 需压缩文件 
  142.      * @param zipFilename 输出文件及详细路径 
  143.      * @throws IOException 
  144.      */  
  145.     public synchronized void zip(File inputFile, String zipFilename) throws IOException {  
  146.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));  
  147.         try {  
  148.             zip(inputFile, out, "");  
  149.         } catch (IOException e) {  
  150.             throw e;  
  151.         } finally {  
  152.             out.close();  
  153.         }  
  154.     }  
  155.         
  156.     /** 
  157.      * 压缩zip格式的压缩文件 
  158.      * @param inputFile 需压缩文件 
  159.      * @param out 输出压缩文件 
  160.      * @param base 结束标识 
  161.      * @throws IOException 
  162.      */  
  163.     @SuppressWarnings("unused")  
  164.     private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws IOException {  
  165.         if (inputFile.isDirectory()) {  
  166.             File[] inputFiles = inputFile.listFiles();  
  167.             out.putNextEntry(new ZipEntry(base + "/"));  
  168.             base = base.length() == 0 ? "" : base + "/";  
  169.             for (int i = 0; i < inputFiles.length; i++) {  
  170.                 zip(inputFiles[i], out, base + inputFiles[i].getName());  
  171.             }  
  172.         } else {  
  173.             if (base.length() > 0) {  
  174.                 out.putNextEntry(new ZipEntry(base));  
  175.             } else {  
  176.                 out.putNextEntry(new ZipEntry(inputFile.getName()));  
  177.             }  
  178.             FileInputStream in = new FileInputStream(inputFile);  
  179.             try {  
  180.                 int c;  
  181.                 byte[] by = new byte[BUFFEREDSIZE];  
  182.                 while ((c = in.read(by)) != -1) {  
  183.                     out.write(by, 0, c);  
  184.                 }  
  185.             } catch (IOException e) {  
  186.                 throw e;  
  187.             } finally {  
  188.                 in.close();  
  189.             }  
  190.         }  
  191.     }  
  192.     /** 
  193.      * 解压rar格式的压缩文件到指定目录下 
  194.      * @param rarFileName 压缩文件 
  195.      * @param extPlace 解压目录 
  196.      * @throws Exception 
  197.      */  
  198.     public synchronized void unrar(String rarFileName, String extPlace) throws Exception{  
  199.         try {  
  200.             (new File(extPlace)).mkdirs();  
  201.             // 构建测解压缩类  
  202.             Archive archive = new Archive();  
  203.             archive.setEnabledLog(false); //不输出日志  
  204.             // 设置rar文件  
  205.             archive.setFile(rarFileName);  
  206.             archive.setExtractPath(extPlace);  
  207.             archive.extractAllFile();  
  208.         } catch (Exception e) {  
  209.             // TODO: handle exception  
  210.         }  
  211.     }}  

Java代码 

  1.     

 (下面附ant,unrar相关的jar)

  • java-unrar.zip (505.9 KB)
  • 描述: (解压rar所需的jar)
  • 下载次数: 109
  • ant.jar (1.2 MB)
  • 描述: 用于解压zipjar
  • 下载次数: 70

转载于:https://www.cnblogs.com/shenhaocn/archive/2010/01/13/1646510.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值