Java實現的zip壓縮及解壓縮工具類樣本,javazip工具類樣本

來源:互聯網
上載者:User

Java實現的zip壓縮及解壓縮工具類樣本,javazip工具類樣本

本文執行個體講述了Java實現的zip壓縮及解壓縮工具類。分享給大家供大家參考,具體如下:

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;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.util.Enumeration;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;import org.apache.tools.zip.ZipOutputStream;public class ZipUtil {  private static final int BUFFEREDSIZE = 1024;  /**   * 壓縮檔   *   * @param zipFileName   *      儲存的壓縮包檔案路徑   * @param filePath   *      需要壓縮的檔案夾或者檔案路徑   * @param isDelete   *      是否刪除源檔案   * @throws Exception   */  public void zip(String zipFileName, String filePath, boolean isDelete) throws Exception {    zip(zipFileName, new File(filePath), isDelete);  }  /**   * 壓縮檔   *   * @param zipFileName   *      儲存的壓縮包檔案路徑   * @param inputFile   *      需要壓縮的檔案夾或者檔案   * @param isDelete   *      是否刪除源檔案   * @throws Exception   */  public void zip(String zipFileName, File inputFile, boolean isDelete) throws Exception {    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));    if (!inputFile.exists()) {      throw new FileNotFoundException("在指定路徑未找到需要壓縮的檔案!");    }    zip(out, inputFile, "", isDelete);    out.close();  }  /**   * 遞迴壓縮方法   *   * @param out   *      壓縮包輸出資料流   * @param f   *      需要壓縮的檔案   * @param base   *      壓縮的路徑   * @param isDelete   *      是否刪除源檔案   * @throws Exception   */  private void zip(ZipOutputStream out, File inputFile, String base, boolean isDelete) throws Exception {    if (inputFile.isDirectory()) { // 如果是目錄      File[] inputFiles = inputFile.listFiles();      out.putNextEntry(new ZipEntry(base + "/"));      base = base.length() == 0 ? "" : base + "/";      for (int i = 0; i < inputFiles.length; i++) {        zip(out, inputFiles[i], base + inputFiles[i].getName(), isDelete);      }    } else { // 如果是檔案      if (base.length() > 0) {        out.putNextEntry(new ZipEntry(base));      } else {        out.putNextEntry(new ZipEntry(inputFile.getName()));      }      FileInputStream in = new FileInputStream(inputFile);      try {        int len;        byte[] buff = new byte[BUFFEREDSIZE];        while ((len = in.read(buff)) != -1) {          out.write(buff, 0, len);        }      } catch (IOException e) {        throw e;      } finally {        in.close();      }    }    if (isDelete) {      inputFile.delete();    }  }  /**   * 解壓縮   *   * @param zipFilePath   *      壓縮包路徑   * @param fileSavePath   *      解壓路徑   * @param isDelete   *      是否刪除源檔案   * @throws Exception   */  public void unZip(String zipFilePath, String fileSavePath, boolean isDelete) throws Exception {    try {      (new File(fileSavePath)).mkdirs();      File f = new File(zipFilePath);      if ((!f.exists()) && (f.length() <= 0)) {        throw new Exception("要解壓的檔案不存在!");      }      ZipFile zipFile = new ZipFile(f);      String strPath, gbkPath, strtemp;      File tempFile = new File(fileSavePath);// 從目前的目錄開始      strPath = tempFile.getAbsolutePath();// 輸出的絕對位置      Enumeration<ZipEntry> e = zipFile.getEntries();      while (e.hasMoreElements()) {        org.apache.tools.zip.ZipEntry zipEnt = e.nextElement();        gbkPath = zipEnt.getName();        if (zipEnt.isDirectory()) {          strtemp = strPath + File.separator + gbkPath;          File dir = new File(strtemp);          dir.mkdirs();          continue;        } else {          // 讀寫檔案          InputStream is = zipFile.getInputStream(zipEnt);          BufferedInputStream bis = new BufferedInputStream(is);          gbkPath = zipEnt.getName();          strtemp = strPath + File.separator + gbkPath;          // 建目錄          String strsubdir = gbkPath;          for (int i = 0; i < strsubdir.length(); i++) {            if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {              String temp = strPath + File.separator + strsubdir.substring(0, i);              File subdir = new File(temp);              if (!subdir.exists())                subdir.mkdir();            }          }          FileOutputStream fos = new FileOutputStream(strtemp);          BufferedOutputStream bos = new BufferedOutputStream(fos);          int len;          byte[] buff = new byte[BUFFEREDSIZE];          while ((len = bis.read(buff)) != -1) {            bos.write(buff, 0, len);          }          bos.close();          fos.close();        }      }    } catch (Exception e) {      e.printStackTrace();      throw e;    }    if (isDelete) {      new File(zipFilePath).delete();    }  }// public static void main(String[] args) {//   ZipUtil cpr = new ZipUtil();//   try {//     cpr.zip("C:/Users/Lenovo User/Desktop/test中文.zip", "C:/Users/Lenovo User/Desktop/建立檔案夾", false);//     cpr.unZip("C:/Users/Lenovo User/Desktop/test中文.zip", "C:/Users/Lenovo User/Desktop/建立檔案夾2", false);//   } catch (Exception e) {//     e.printStackTrace();//   }//// }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.