使用zip對文件或文件夾進行壓縮, 解壓縮

使用zip對文件或文件夾進行壓縮, 解壓縮
Java代碼 複製代碼 收藏代碼
  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.FileOutputStream;
  4. import java.io.IOException; 
  5. import java.util.zip.ZipEntry; 
  6. import java.util.zip.ZipInputStream; 
  7. import java.util.zip.ZipOutputStream;
  8.  
  9. /**
  10. * 對文件或文件夾進行壓縮和解壓
  11. *
  12. */ 
  13. public class ZipUtil { 
  14.     /**得到當前系統的分隔符*/ 
  15. //  private static String separator = System.getProperty("file.separator"); 
  16.  
  17.     /**
  18.      * 添加到壓縮文件中
  19.      * @param out
  20.      * @param f
  21.      * @param base
  22.      * @throws Exception
  23.      */ 
  24.     private void directoryZip(ZipOutputStream out, File f, String base) throws Exception { 
  25.         // 如果傳入的是目錄 
  26.         if (f.isDirectory()) { 
  27.             File[] fl = f.listFiles(); 
  28.             // 創建壓縮的子目錄 
  29.             out.putNextEntry(new ZipEntry(base + "/")); 
  30.             if (base.length() == 0) { 
  31.                 base = ""
  32.             } else
  33.                 base = base + "/"
  34.             } 
  35.             for (int i = 0; i < fl.length; i++) { 
  36.                 directoryZip(out, fl[i], base + fl[i].getName()); 
  37.             } 
  38.         } else
  39.             // 把壓縮文件加入rar中 
  40.             out.putNextEntry(new ZipEntry(base)); 
  41.             FileInputStream in = new FileInputStream(f); 
  42.             byte[] bb = new byte[10240]; 
  43.             int aa = 0
  44.             while ((aa = in.read(bb)) != -1) { 
  45.                 out.write(bb, 0, aa); 
  46.             } 
  47.             in.close(); 
  48.         } 
  49.     } 
  50.  
  51.     /**
  52.      * 壓縮文件
  53.      *
  54.      * @param zos
  55.      * @param file
  56.      * @throws Exception
  57.      */ 
  58.     private void fileZip(ZipOutputStream zos, File file) throws Exception { 
  59.         if (file.isFile()) { 
  60.             zos.putNextEntry(new ZipEntry(file.getName())); 
  61.             FileInputStream fis = new FileInputStream(file); 
  62.             byte[] bb = new byte[10240]; 
  63.             int aa = 0
  64.             while ((aa = fis.read(bb)) != -1) { 
  65.                 zos.write(bb, 0, aa); 
  66.             } 
  67.             fis.close(); 
  68.             System.out.println(file.getName()); 
  69.         } else
  70.             directoryZip(zos, file, ""); 
  71.         } 
  72.     } 
  73.  
  74.     /**
  75.      * 解壓縮文件
  76.      *
  77.      * @param zis
  78.      * @param file
  79.      * @throws Exception
  80.      */ 
  81.     private void fileUnZip(ZipInputStream zis, File file) throws Exception { 
  82.         ZipEntry zip = zis.getNextEntry(); 
  83.         if (zip == null
  84.             return
  85.         String name = zip.getName(); 
  86.         File f = new File(file.getAbsolutePath() + "/" + name); 
  87.         if (zip.isDirectory()) { 
  88.             f.mkdirs(); 
  89.             fileUnZip(zis, file); 
  90.         } else
  91.             f.createNewFile(); 
  92.             FileOutputStream fos = new FileOutputStream(f); 
  93.             byte b[] = new byte[10240]; 
  94.             int aa = 0
  95.             while ((aa = zis.read(b)) != -1) { 
  96.                 fos.write(b, 0, aa); 
  97.             } 
  98.             fos.close(); 
  99.             fileUnZip(zis, file); 
  100.         } 
  101.     } 
  102.      
  103.     /**
  104.      * 根據filePath創建相應的目錄
  105.      * @param filePath
  106.      * @return
  107.      * @throws IOException
  108.      */ 
  109.     private File mkdirFiles(String filePath) throws IOException{ 
  110.         File file = new File(filePath); 
  111.         if(!file.getParentFile().exists()){ 
  112.             file.getParentFile().mkdirs(); 
  113.         } 
  114.         file.createNewFile(); 
  115.          
  116.         return file; 
  117.     } 
  118.  
  119.     /**
  120.      * 對zipBeforeFile目錄下的文件壓縮,保存爲指定的文件zipAfterFile
  121.      *
  122.      * @param zipBeforeFile     壓縮之前的文件
  123.      * @param zipAfterFile      壓縮之後的文件
  124.      */ 
  125.     public void zip(String zipBeforeFile, String zipAfterFile) { 
  126.         try
  127.              
  128.             ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(mkdirFiles(zipAfterFile))); 
  129.             fileZip(zos, new File(zipBeforeFile)); 
  130.             zos.close(); 
  131.         } catch (Exception e) { 
  132.             e.printStackTrace(); 
  133.         } 
  134.     } 
  135.  
  136.     /**
  137.      * 解壓縮文件unZipBeforeFile保存在unZipAfterFile目錄下
  138.      *
  139.      * @param unZipBeforeFile       解壓之前的文件
  140.      * @param unZipAfterFile        解壓之後的文件
  141.      */ 
  142.     public void unZip(String unZipBeforeFile, String unZipAfterFile) { 
  143.         try
  144.             ZipInputStream zis = new ZipInputStream(new FileInputStream(unZipBeforeFile)); 
  145.             File f = new File(unZipAfterFile); 
  146.             f.mkdirs(); 
  147.             fileUnZip(zis, f); 
  148.             zis.close(); 
  149.         } catch (Exception e) { 
  150.             e.printStackTrace(); 
  151.         } 
  152.     } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章