Zip文件壓縮Java類代碼

jopen 12年前發布 | 24K 次閱讀 ZIP Java開發

public class ZipFileUtil {
    private final static int BUFFER=2048;
     /**

  * 解壓zip文件
  * 
  * @param zipFilePath
  *            zip文件絕對路徑
  * @param unzipDirectory
  *            解壓到的目錄
  * @throws Exception
  */

 @SuppressWarnings("unchecked")
public static boolean unzip(String zipFilePath, String unzipDirectory)
 throws Exception {
     boolean flag=true;
     try{
      // 創建文件對象
      File file = new File(zipFilePath);
      // 創建zip文件對象
      ZipFile zipFile = new ZipFile(file);
      // 創建本zip文件解壓目錄
      File unzipFile = new File(unzipDirectory + "/"
        + getSuffixName(file.getName()));
      if (unzipFile.exists())
       unzipFile.delete();
      unzipFile.mkdir();
      // 得到zip文件條目枚舉對象
      Enumeration zipEnum = zipFile.getEntries();
      // 定義輸入輸出流對象
      InputStream input = null;
      OutputStream output = null;
      // 定義對象
      ZipEntry entry = null;
      // 循環讀取條目
      while (zipEnum.hasMoreElements()) {
       // 得到當前條目
       entry = (ZipEntry) zipEnum.nextElement();
       String entryName = new String(entry.getName());
       // 用/分隔條目名稱
       String names[] = entryName.split("\\/");
       int length = names.length;
       String path = unzipFile.getAbsolutePath();
       for (int v = 0; v < length; v++) {
        if (v < length - 1) { // 最后一個目錄之前的目錄
         path += "/" + names[v] + "/";
         createDir(path);
        } else { // 最后一個
         if (entryName.endsWith("/")) // 為目錄,則創建文件夾
          createDir(unzipFile.getAbsolutePath() + "/" + entryName);
         else { // 為文件,則輸出到文件
          input = zipFile.getInputStream(entry);
          output = new FileOutputStream(new File(unzipFile
            .getAbsolutePath()
            + "/" + entryName));
          byte[] buffer = new byte[1024 * 8];
          int readLen = 0;
          while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
           output.write(buffer, 0, readLen);
          // 關閉流
          input.close();
          output.flush();
          output.close();
         }
        }
       }
      }}catch(Exception e){
          flag=false;
      }

    return flag;
}
 /**
  * 創建目錄
  * 
  * @param path
  *            目錄絕對路徑名
  */
 static void createDir(String path) {
  File dir = new File(path);
  if (dir.exists() == false)
   dir.mkdir();
 }
 /**
  * 取得文件名,不包含后綴名
  * 
  * @param name
  *            完整文件名
  * @return 文件名(不包含后綴名)
  */
 static String getSuffixName(String name) {
  return name.substring(0, name.lastIndexOf("."));
 }
/**
 * 
 * 壓縮多個文件
 * 
 * @param zipFileName
 *            壓縮后的文件名
 * 
 * @param inputFiles 待壓縮的文件列表
 * 
 */
public static boolean zipFile(String zipFileName, List<File> inputFiles){
    boolean test = true;
    try {
        BufferedInputStream origin = null;
        FileOutputStream dest = new FileOutputStream(zipFileName);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                dest));
        byte data[] = new byte[BUFFER];

        for (int i = 0; i < inputFiles.size(); i++) {
            File file=inputFiles.get(i);
            FileInputStream fi = new FileInputStream(file);
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(file.getName());
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }
        out.close();
    } catch (Exception e) {
        test = false;
        e.printStackTrace();
    }
    return test;
}
/**
 * 
 * 壓縮多個文件
 * 
 * @param zipFileName
 *            壓縮后的文件名
 * 
 * @param inputFiles 待壓縮的文件列表
 * 
 */
public static boolean zip(String zipFileName, List<String> inputFiles){
    boolean test = true;
    try {
        BufferedInputStream origin = null;
        FileOutputStream dest = new FileOutputStream(zipFileName);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                dest));
        byte data[] = new byte[BUFFER];

        for (int i = 0; i < inputFiles.size(); i++) {
            File file = new File(inputFiles.get(i));
            FileInputStream fi = new FileInputStream(file);
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(file.getName());
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }
        out.close();
    } catch (Exception e) {
        test = false;
        e.printStackTrace();
    }
    return test;
}

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!