實現Java代碼壓縮解壓

來源:互聯網
上載者:User

標籤:java壓縮解壓   壓縮解壓jar包   壓縮   

原因:某個時間想對伺服器上的zip中的某些檔案進行修改
本來以為很簡單的事情,在網上找了好些代碼,結果效果都不是很理想。


實現對象:對各種類型的檔案夾(包含子檔案或子檔案夾)


下面介紹一下自己綜合網上代碼自己寫的

首先要倒入一個jar包:(零積分下載)

package com.tzx.test2;import java.io.File;  import java.io.FileNotFoundException;  import java.io.IOException;  import org.apache.tools.zip.ZipOutputStream;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.InputStream;  import java.util.Enumeration;  import org.apache.tools.zip.ZipEntry;  import org.apache.tools.zip.ZipFile;   /** * 壓縮解壓zip檔案 * @author STVEN_KING * @date: 2015年4月1日 下午4:09:11 * */public class ZipUtil {    /** * 壓縮檔夾 * @param filesDirth  目標檔案  E:/a/b * @param zipFilePath  存放壓縮檔的路徑名 E:/a/b.zip * @param deleteFile 是否刪除檔案夾及目錄 * @return boolean  是否壓縮成功 * */    public static boolean doZip(String filesDirPath, String zipFilePath,boolean deleteFile) {          return doZip(new File(filesDirPath), zipFilePath,deleteFile);      }        private static boolean doZip(File inputFile, String zipFileName,boolean deleteFile) {          ZipOutputStream out = null;          try {              out = new ZipOutputStream(new FileOutputStream(zipFileName));              boolean result = doZip(out, inputFile, "");            return result;          } catch (FileNotFoundException ex) {              ex.printStackTrace();              return false;          } catch (IOException ex) {              ex.printStackTrace();              return false;          } finally {              try {                  out.close();                  if (deleteFile) {                deleteDir(inputFile);}            } catch (IOException ex) {                  ex.printStackTrace();                  return false;              }          }      }        private static boolean doZip(ZipOutputStream out, File f, String base) {          try {              if (f.isDirectory()) {                  File[] fl = f.listFiles();                  if (fl.length == 0) {                    out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));                }                for (int i = 0; i < fl.length; i++) {                      doZip(out, fl[i], base + "/" + fl[i].getName());                  }              } else {                  out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));                  FileInputStream in = new FileInputStream(f);                  int b;                  while ((b = in.read()) != -1) {                      out.write(b);                  }                  in.close();              }              return true;          } catch (IOException ex) {              ex.printStackTrace();              return false;          }      }        /**     * 解壓zip檔案     * @param zipFilePath  壓縮檔路徑名  E:/b/a.zip     * @param base  解壓檔案存放路徑  E:/b/c     * @param deleteFile 是否刪除壓縮包     * @return boolean 是否解壓成功     * */    public static boolean unZip(String zipFilePath, String base, boolean deleteFile) {          try {              File file = new File(zipFilePath);              if (!file.exists()) {                  throw new RuntimeException("解壓檔案不存在!");              }              ZipFile zipFile = new ZipFile(file);              Enumeration e = zipFile.getEntries();              while (e.hasMoreElements()) {                  ZipEntry zipEntry = (ZipEntry) e.nextElement();                  if (zipEntry.isDirectory()) {                      String name = zipEntry.getName();                      name = name.substring(0, name.length() - 1);                      File f = new File(base+ "/" + name);                      f.mkdirs();                  } else {                      File f = new File(base + zipEntry.getName());                      f.getParentFile().mkdirs();                      f.createNewFile();                      InputStream is = zipFile.getInputStream(zipEntry);                      FileOutputStream fos = new FileOutputStream(f);                      int length = 0;                      byte[] b = new byte[1024];                      while ((length = is.read(b, 0, 1024)) != -1) {                          fos.write(b, 0, length);                      }                      is.close();                      fos.close();                  }              }              if (zipFile != null) {                  zipFile.close();              }              if (deleteFile) {                  file.deleteOnExit();            }              return true;          } catch (IOException ex) {              return false;          }      }        /**     * 遞迴刪除目錄下的所有檔案及子目錄下所有檔案     * @param dir 將要刪除的檔案目錄     * @return boolean      */    private static boolean deleteDir(File dir) {        if (dir.isDirectory()) {            String[] children = dir.list();//遞迴刪除目錄中的子目錄下            for (int i=0; i<children.length; i++) {                boolean success = deleteDir(new File(dir, children[i]));                if (!success) {                    return false;                }            }        }        // 目錄此時為空白,可以刪除        return dir.delete();    }}  



實現Java代碼壓縮解壓

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.