Java实现对zip和rar文件的解压缩

通过java实现对zip和rar文件的解压缩

  1 package com.svse.test;
  2 
  3 import java.io.File;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.OutputStream;
  8 import java.util.Enumeration;
  9 
 10 import org.apache.tools.zip.ZipEntry;
 11 import org.apache.tools.zip.ZipFile;
 12 
 13 import de.innosystec.unrar.Archive;
 14 import de.innosystec.unrar.rarfile.FileHeader;
 15 /**
 16 * zip和rar解压缩工具类
 17 * @author lenovo
 18 *
 19 */
 20 public class ZipAndRarTools {
 21 
 22  /**
 23  * 解压rar
 24  * @param sourceRarPath 需要解压的rar文件全路径
 25  * @param destDirPath 需要解压到的文件目录
 26  * @throws Exception
 27  */
 28   public static void unrar(String sourceRarPath, String destDirPath) throws Exception { 
 29    File sourceRar=new File(sourceRarPath);
 30   File destDir=new File(destDirPath);
 31   Archive archive = null; 
 32   FileOutputStream fos = null; 
 33   System.out.println("Starting 开始解压..."); 
 34   try { 
 35     archive = new Archive(sourceRar); 
 36     FileHeader fh = archive.nextFileHeader(); 
 37     int count = 0; 
 38     File destFileName = null; 
 39     while (fh != null) { 
 40       System.out.println((++count) + ") " + fh.getFileNameString()); 
 41       String compressFileName = fh.getFileNameString().trim(); 
 42       destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName); 
 43       if (fh.isDirectory()) { 
 44         if (!destFileName.exists()) { 
 45           destFileName.mkdirs(); 
 46           } 
 47         fh = archive.nextFileHeader(); 
 48         continue; 
 49         } 
 50       if (!destFileName.getParentFile().exists()) { 
 51         destFileName.getParentFile().mkdirs(); 
 52         }
 53 
 54 
 55       fos = new FileOutputStream(destFileName); 
 56       archive.extractFile(fh, fos); 
 57       fos.close(); 
 58       fos = null; 
 59       fh = archive.nextFileHeader(); 
 60       } 
 61 
 62       archive.close(); 
 63       archive = null; 
 64       System.out.println("Finished 解压完成!"); 
 65      } catch (Exception e) { 
 66          throw e; 
 67       } finally { 
 68        if (fos != null) { 
 69          try { 
 70           fos.close(); 
 71           fos = null; 
 72         } catch (Exception e) { 
 73        } 
 74      } 
 75      if (archive != null) { 
 76       try { 
 77         archive.close(); 
 78         archive = null; 
 79      } catch (Exception e) { 
 80      } 
 81     } 
 82    } 
 83  } 
 84 
 85 
 86  /** 
 87  * 解压Zip文件 
 88  * @param zipFileName 需要解压缩的文件位置
 89  * @param descFileName 将文件解压到某个路径
 90  * @throws IOException 
 91  */ 
 92   public static void unZip(String zipFileName,String descFileName) throws IOException{  
 93     System.out.println("文件解压开始...");
 94     String descFileNames=descFileName;
 95     if(!descFileNames.endsWith(File.separator)){
 96       descFileNames=descFileNames+File.separator;
 97     }
 98    try {
 99       ZipFile zipFile=new ZipFile(zipFileName);
100       ZipEntry entry=null;
101     String entryName=null;
102     String descFileDir=null;
103     byte[] buf=new byte[4096];
104     int readByte=0;
105     @SuppressWarnings("rawtypes")
106     Enumeration enums=zipFile.getEntries();
107     while(enums.hasMoreElements()){
108       entry =(ZipEntry) enums.nextElement();
109       entryName=entry.getName();
110       descFileDir=descFileNames+entryName;
111       if(entry.isDirectory()){
112          new File(descFileDir).mkdir();
113          continue;
114       }else{
115         new File(descFileDir).getParentFile().mkdir();
116           }
117        File file=new File(descFileDir);
118      OutputStream os=new FileOutputStream(file);
119      InputStream is=zipFile.getInputStream(entry);
120         while((readByte=is.read(buf))!=-1){
121           os.write(buf, 0, readByte);
122         }
123           os.close();
124           is.close();
125        }
126          zipFile.close();
127          System.out.println("文件解压成功!");
128     } catch (Exception e) {
129        System.out.println("文件解压失败!");
130        e.printStackTrace();
131     }
132 
133    } 
134 
135    public static void main(String[] args) throws Exception {
136       //ZipAndRarTools.unrar(newFile("D:\\存放资料的压缩包\\员工材料.rar"),newFile("D:\\存放资料的非压缩包\\"));
137 
138      ZipAndRarTools.unZip("D:\\rarTest\\jar包和配置文件资源.zip", "D:\\rarTest");
139      ZipAndRarTools.unrar("D:\\rarTest\\rar压缩包.rar", "D:\\rarTest");
140 
141     }
142 
143 }

 

package com.svse.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
/**
* zip和rar解压缩工具类
* @author lenovo
*
*/
public class ZipAndRarTools {

 /**
 * 解压rar
 * @param sourceRarPath 需要解压的rar文件全路径
 * @param destDirPath 需要解压到的文件目录
 * @throws Exception
 */
  public static void unrar(String sourceRarPath, String destDirPath) throws Exception { 
   File sourceRar=new File(sourceRarPath);
  File destDir=new File(destDirPath);
  Archive archive = null
  FileOutputStream fos = null
  System.out.println("Starting 开始解压..."); 
  try
    archive = new Archive(sourceRar); 
    FileHeader fh = archive.nextFileHeader(); 
    int count = 0; 
    File destFileName = null
    while (fh != null) { 
      System.out.println((++count) + ") " + fh.getFileNameString()); 
      String compressFileName = fh.getFileNameString().trim(); 
      destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName); 
      if (fh.isDirectory()) { 
        if (!destFileName.exists()) { 
          destFileName.mkdirs(); 
          } 
        fh = archive.nextFileHeader(); 
        continue
        } 
      if (!destFileName.getParentFile().exists()) { 
        destFileName.getParentFile().mkdirs(); 
        }


      fos = new FileOutputStream(destFileName); 
      archive.extractFile(fh, fos); 
      fos.close(); 
      fos = null
      fh = archive.nextFileHeader(); 
      } 

      archive.close(); 
      archive = null
      System.out.println("Finished 解压完成!"); 
     } catch (Exception e) { 
         throw e; 
      } finally
       if (fos != null) { 
         try
          fos.close(); 
          fos = null
        } catch (Exception e) { 
       } 
     } 
     if (archive != null) { 
      try
        archive.close(); 
        archive = null
     } catch (Exception e) { 
     } 
    } 
   } 
 } 


 /** 
 * 解压Zip文件 
 * @param zipFileName 需要解压缩的文件位置
 * @param descFileName 将文件解压到某个路径
 * @throws IOException 
 */ 
  public static void unZip(String zipFileName,String descFileName) throws IOException{  
    System.out.println("文件解压开始...");
    String descFileNames=descFileName;
    if(!descFileNames.endsWith(File.separator)){
      descFileNames=descFileNames+File.separator;
    }
   try {
      ZipFile zipFile=new ZipFile(zipFileName);
      ZipEntry entry=null;
    String entryName=null;
    String descFileDir=null;
    byte[] buf=new byte[4096];
    int readByte=0;
    @SuppressWarnings("rawtypes")
    Enumeration enums=zipFile.getEntries();
    while(enums.hasMoreElements()){
      entry =(ZipEntry) enums.nextElement();
      entryName=entry.getName();
      descFileDir=descFileNames+entryName;
      if(entry.isDirectory()){
         new File(descFileDir).mkdir();
         continue;
      }else{
        new File(descFileDir).getParentFile().mkdir();
          }
       File file=new File(descFileDir);
     OutputStream os=new FileOutputStream(file);
     InputStream is=zipFile.getInputStream(entry);
        while((readByte=is.read(buf))!=-1){
          os.write(buf, 0, readByte);
        }
          os.close();
          is.close();
       }
         zipFile.close();
         System.out.println("文件解压成功!");
    } catch (Exception e) {
       System.out.println("文件解压失败!");
       e.printStackTrace();
    }

   } 

   public static void main(String[] args) throws Exception {
      //ZipAndRarTools.unrar(newFile("D:\\存放资料的压缩包\\员工材料.rar"),newFile("D:\\存放资料的非压缩包\\"));

     ZipAndRarTools.unZip("D:\\rarTest\\jar包和配置文件资源.zip", "D:\\rarTest");
     ZipAndRarTools.unrar("D:\\rarTest\\rar压缩包.rar", "D:\\rarTest");

    }

}

posted @ 2018-10-12 11:37  zsq_fengchen  阅读(16153)  评论(4编辑  收藏  举报