关于java解压zip与rar的问题

来源:互联网 发布:能听金庸小说的软件 编辑:程序博客网 时间:2024/05/16 09:30

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

     下面是一个对zip,rar进行解压的程序(从http://topic.csdn.net/u/20090227/11/fd8c30ee-ce56-49be-bdea-d19d22a0da37.html转载,供大家一起享用,另外希望对unrar有比较发解的朋友,能发表一个人见解),

    

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.     }}