Android Zip工具類ZipUtils

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**

  • Java utils 實現(xiàn)的Zip工具

  • @author xx
    */
    public class ZipUtils {
    private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte

    /**

    • 批量壓縮文件(夾)
    • @param resFileList 要壓縮的文件(夾)列表
    • @param zipFile 生成的壓縮文件
    • @throws IOException 當壓縮過程出錯時拋出
      */
      public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
      ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
      zipFile), BUFF_SIZE));
      for (File resFile : resFileList) {
      zipFile(resFile, zipout, "");
      }
      zipout.close();
      }

    /**

    • 批量壓縮文件(夾)
    • @param resFileList 要壓縮的文件(夾)列表
    • @param zipFile 生成的壓縮文件
    • @param comment 壓縮文件的注釋
    • @throws IOException 當壓縮過程出錯時拋出
      */
      public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
      throws IOException {
      ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
      zipFile), BUFF_SIZE));
      for (File resFile : resFileList) {
      zipFile(resFile, zipout, "");
      }
      zipout.setComment(comment);
      zipout.close();
      }

    /**

    • 解壓縮一個文件 160314

    • @param zipFile 壓縮文件

    • @param folderPath 解壓縮的目標目錄

    • @throws IOException 當解壓縮過程出錯時拋出
      */
      public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
      File desDir = new File(folderPath);
      if (!desDir.exists()) {
      desDir.mkdirs();
      }
      ZipFile zf = new ZipFile(zipFile);
      for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
      ZipEntry entry = ((ZipEntry)entries.nextElement());
      //ss:if it's a filedir,it need to be opened 160314
      //String name = entry.getName();
      //if(name.endsWith(File.separator))
      //continue;

       InputStream in = zf.getInputStream(entry);
       String str = folderPath + File.separator + entry.getName();
       str = new String(str.getBytes("8859_1"), "GB2312");
       File desFile = new File(str);
       if (!desFile.exists()) {
           File fileParentDir = desFile.getParentFile();
           if (!fileParentDir.exists()) {
               fileParentDir.mkdirs();
           }
           desFile.createNewFile();
       }
       OutputStream out = new FileOutputStream(desFile);
       byte buffer[] = new byte[BUFF_SIZE];
       int realLength;
       while ((realLength = in.read(buffer)) > 0) {
           out.write(buffer, 0, realLength);
       }
       in.close();
       out.close();
      

      }
      }

    /**

    • 解壓文件名包含傳入文字的文件

    • @param zipFile 壓縮文件

    • @param folderPath 目標文件夾

    • @param nameContains 傳入的文件匹配名

    • @throws ZipException 壓縮格式有誤時拋出

    • @throws IOException IO錯誤時拋出
      */
      public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath,
      String nameContains) throws ZipException, IOException {
      ArrayList<File> fileList = new ArrayList<File>();

      File desDir = new File(folderPath);
      if (!desDir.exists()) {
      desDir.mkdir();
      }

      ZipFile zf = new ZipFile(zipFile);
      for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
      ZipEntry entry = ((ZipEntry)entries.nextElement());
      if (entry.getName().contains(nameContains)) {
      InputStream in = zf.getInputStream(entry);
      String str = folderPath + File.separator + entry.getName();
      str = new String(str.getBytes("8859_1"), "GB2312");
      // str.getBytes("GB2312"),"8859_1" 輸出
      // str.getBytes("8859_1"),"GB2312" 輸入
      File desFile = new File(str);
      if (!desFile.exists()) {
      File fileParentDir = desFile.getParentFile();
      if (!fileParentDir.exists()) {
      fileParentDir.mkdirs();
      }
      desFile.createNewFile();
      }
      OutputStream out = new FileOutputStream(desFile);
      byte buffer[] = new byte[BUFF_SIZE];
      int realLength;
      while ((realLength = in.read(buffer)) > 0) {
      out.write(buffer, 0, realLength);
      }
      in.close();
      out.close();
      fileList.add(desFile);
      }
      }
      return fileList;
      }

    /**

    • 獲得壓縮文件內(nèi)文件列表
    • @param zipFile 壓縮文件
    • @return 壓縮文件內(nèi)文件名稱
    • @throws ZipException 壓縮文件格式有誤時拋出
    • @throws IOException 當解壓縮過程出錯時拋出
      */
      public static ArrayList<String> getEntriesNames(File zipFile) throws ZipException, IOException {
      ArrayList<String> entryNames = new ArrayList<String>();
      Enumeration<?> entries = getEntriesEnumeration(zipFile);
      while (entries.hasMoreElements()) {
      ZipEntry entry = ((ZipEntry)entries.nextElement());
      entryNames.add(new String(getEntryName(entry).getBytes("GB2312"), "8859_1"));
      }
      return entryNames;
      }

    /**

    • 獲得壓縮文件內(nèi)壓縮文件對象以取得其屬性
    • @param zipFile 壓縮文件
    • @return 返回一個壓縮文件列表
    • @throws ZipException 壓縮文件格式有誤時拋出
    • @throws IOException IO操作有誤時拋出
      */
      public static Enumeration<?> getEntriesEnumeration(File zipFile) throws ZipException,
      IOException {
      ZipFile zf = new ZipFile(zipFile);
      return zf.entries();

    }

    /**

    • 取得壓縮文件對象的注釋
    • @param entry 壓縮文件對象
    • @return 壓縮文件對象的注釋
    • @throws UnsupportedEncodingException
      */
      public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException {
      return new String(entry.getComment().getBytes("GB2312"), "8859_1");
      }

    /**

    • 取得壓縮文件對象的名稱
    • @param entry 壓縮文件對象
    • @return 壓縮文件對象的名稱
    • @throws UnsupportedEncodingException
      */
      public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {
      return new String(entry.getName().getBytes("GB2312"), "8859_1");
      }

    /**

    • 壓縮文件
    • @param resFile 需要壓縮的文件(夾)
    • @param zipout 壓縮的目的文件
    • @param rootpath 壓縮的文件路徑
    • @throws FileNotFoundException 找不到文件時拋出
    • @throws IOException 當壓縮過程出錯時拋出
      */
      private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
      throws FileNotFoundException, IOException {
      rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)
      + resFile.getName();
      rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
      if (resFile.isDirectory()) {
      File[] fileList = resFile.listFiles();
      for (File file : fileList) {
      zipFile(file, zipout, rootpath);
      }
      } else {
      byte buffer[] = new byte[BUFF_SIZE];
      BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile),
      BUFF_SIZE);
      zipout.putNextEntry(new ZipEntry(rootpath));
      int realLength;
      while ((realLength = in.read(buffer)) != -1) {
      zipout.write(buffer, 0, realLength);
      }
      in.close();
      zipout.flush();
      zipout.closeEntry();
      }
      }
      }
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末节芥,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子寓娩,更是在濱河造成了極大的恐慌,老刑警劉巖凳谦,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件艘狭,死亡現(xiàn)場離奇詭異已卸,居然都是意外死亡,警方通過查閱死者的電腦和手機瞧省,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門扯夭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人臀突,你說我怎么就攤上這事勉抓。” “怎么了候学?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵藕筋,是天一觀的道長。 經(jīng)常有香客問我梳码,道長隐圾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任掰茶,我火速辦了婚禮暇藏,結果婚禮上,老公的妹妹穿的比我還像新娘濒蒋。我一直安慰自己盐碱,他們只是感情好把兔,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著瓮顽,像睡著了一般县好。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上暖混,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天缕贡,我揣著相機與錄音,去河邊找鬼拣播。 笑死晾咪,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的贮配。 我是一名探鬼主播谍倦,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼牧嫉!你這毒婦竟也來了剂跟?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤酣藻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后鳍置,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體辽剧,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年税产,在試婚紗的時候發(fā)現(xiàn)自己被綠了怕轿。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡辟拷,死狀恐怖撞羽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情衫冻,我是刑警寧澤诀紊,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站隅俘,受9級特大地震影響邻奠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜为居,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一碌宴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蒙畴,春花似錦贰镣、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽董朝。三九已至,卻和暖如春干跛,著一層夾襖步出監(jiān)牢的瞬間子姜,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工楼入, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留哥捕,地道東北人。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓嘉熊,卻偏偏與公主長得像遥赚,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子阐肤,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 一凫佛、流的概念和作用。 流是一種有順序的孕惜,有起點和終點的字節(jié)集合愧薛,是對數(shù)據(jù)傳輸?shù)目偝苫虺橄蟆<磾?shù)據(jù)在兩設備之間的傳輸...
    布魯斯不吐絲閱讀 10,038評論 2 95
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理衫画,服務發(fā)現(xiàn)毫炉,斷路器,智...
    卡卡羅2017閱讀 134,656評論 18 139
  • DiskLurCache 使用教程 源碼解析 使用 打開緩存 打開緩存函數(shù)public static DiskLr...
    super小立立閱讀 922評論 1 1
  • 1. Java基礎部分 基礎部分的順序:基本語法削罩,類相關的語法瞄勾,內(nèi)部類的語法,繼承相關的語法弥激,異常的語法进陡,線程的語...
    子非魚_t_閱讀 31,631評論 18 399
  • 轉自http://www.open-open.com/lib/view/open1363592512046.htm...
    Feng_Sir閱讀 2,486評論 0 4