1. 程式人生 > >Java實現壓縮與解壓

Java實現壓縮與解壓

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; /**  * 壓縮與解壓    * @author hulujie  *  */ public class ZipUtil {          /**      * 壓縮檔案      *       * @param srcfile      *            File[] 需要壓縮的檔案列表  "F:\\asd"      * @param zipfile      *            File 壓縮後的檔案 "f:\\qwe_16_V6.0.0_1.zip";      * @throws IOException       * @throws FileNotFoundException       */     public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) throws IOException  {                                    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipfile));             for(int i = 0; i < srcfile.length; ++i){                 if(srcfile[i].isDirectory()){                     zipDir(zipOut,srcfile[i],srcfile[i].getName());                 }else{                     zipFile(zipOut,srcfile[i],null);                 }             }         zipOut.close();     }     /**      * 遞迴壓縮資料夾 faterName的目的就是為了維持原來的目錄      * @author hulujie      * @since 2016年9月2日 下午5:33:26      * @param zipOut      * @param dirFile      * @param faterName      * @return      * @throws IOException      */     private static ZipOutputStream zipDir(ZipOutputStream zipOut,File dirFile,String faterName) throws IOException{         File fileList[] = dirFile.listFiles();                  for (int i = 0; i < fileList.length; i++) {             if(fileList[i].isDirectory()){                 zipDir(zipOut,fileList[i],faterName+File.separator+fileList[i].getName());             }else{                 zipFile(zipOut,fileList[i],faterName);             }         }         return zipOut;     }          /**      * 壓縮單個檔案      * @author hulujie      * @since 2016年9月2日 下午5:40:26      * @param zipOut      * @param file      * @throws IOException      */     private static void zipFile(ZipOutputStream zipOut,File file,String faterName) throws IOException{                  InputStream input = null;                      byte[] buf = new byte[1024];         input = new FileInputStream(file);         if(faterName!=null){              zipOut.putNextEntry(new ZipEntry(faterName+File.separator+file.getName()));         }else{             zipOut.putNextEntry(new ZipEntry(File.separator+file.getName()));         }                 int temp = 0;         while((temp = input.read(buf)) != -1){             zipOut.write(buf,0,temp);         }         input.close();     }               /**      * zip解壓縮      *       * @param zipfile      *            File 需要解壓縮的檔案 "f:\\qwe_16_V6.0.0_1.zip";      * @param descDir      *            String 解壓後的目標目錄 "F:\\asd";      */     public  static void unFiles(File file, String outputDirectory) throws IOException {                  File outzipFile = new File(outputDirectory);         if(!outzipFile.exists()){             outzipFile.mkdirs();         }         byte[] buf = new  byte[1024];                  File outFile = null;         ZipFile zipFile = new ZipFile(file);         ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));         ZipEntry entry = null;         InputStream input = null;         OutputStream output = null;         while((entry = zipInput.getNextEntry()) != null){             outFile = new File(outputDirectory + File.separator + entry.getName());                          if(entry.isDirectory()){                 outFile.mkdir();             }else{                                  if(!outFile.getParentFile().exists()){                     outFile.getParentFile().mkdirs();                 }                                  outFile.createNewFile();                  input = zipFile.getInputStream(entry);                  output = new FileOutputStream(outFile);                  int temp = 0;                  while((temp = input.read(buf)) != -1){                      output.write(buf,0,temp);                  }                  input.close();                  output.close();             }         }         zipFile.close();         zipInput.close();     }          public static void main(String[] args){                  String dirPath = "F:\\asd";         String zipFilePath = "f:\\qwe_16_V6.0.0_1.zip";                  try {             File file = new File(dirPath);             File zipFile = new File(zipFilePath);             if(!zipFile.exists()){                 zipFile.createNewFile();             }                          ZipFiles(file.listFiles(),zipFile);              //            unFiles(zipFile,dirPath);                      } catch (IOException e) {             e.printStackTrace();         }                       } }