qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java Zip文件解壓縮

          為了解壓縮zip都折騰兩天了,查看了許多谷歌百度來的code,
            真實無語了,絕大多數是不能用的。這可能跟我的開發環境有關吧。
            我用的是Ubuntu14.04,eclipse 用的是STS3.5,jdk81.8.0_20
            經過兩天的努力檢驗了無數的code終于讓我找到一個還能用的可以解決中文亂碼問題。
            這個項目用maven構建的依賴jar坐標如下
            <!-- 用于zip文件解壓縮 -->
            <dependency>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.7.0</version>
            </dependency>
            代碼如下:
          package com.uujava.mbfy.test;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import org.apache.tools.zip.ZipOutputStream;
          /**
          * @author k
          * @date 2014年10月7日 上午8:04:51
          * @Description: TODO(用于壓縮和解壓縮zip文件)
          * 尚須解決bug:
          * 這里采用硬編碼這定文件編碼,是否有辦法讀取壓縮文件時判斷起內部文件名編碼。
          */
          public final class ZipUtils {
          public static void main(String[] args) throws Exception {
          // ZipFile("/home/k/Documents/testzip/寬屏透明html5產品展示.zip", "/home/k/Documents/testzip/index.html");
          unZipFile("/home/k/Documents/testzip/寬屏透明html5產品展示.zip",
          "/home/k/Documents/testzip/zip");
          }
          public static void zip(ZipOutputStream out, File f, String base,
          boolean first) throws Exception {
          if (first) {
          if (f.isDirectory()) {
          out.putNextEntry(new org.apache.tools.zip.ZipEntry("/"));
          base = base + f.getName();
          first = false;
          } else
          base = f.getName();
          }
          if (f.isDirectory()) {
          File[] fl = f.listFiles();
          base = base + "/";
          for (int i = 0; i < fl.length; i++) {
          zip(out, fl[i], base + fl[i].getName(), first);
          }
          } else {
          out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
          FileInputStream in = new FileInputStream(f);
          int b;
          System.out.println(base);
          while ((b = in.read()) != -1) {
          out.write(b);
          }
          in.close();
          }
          }
          @SuppressWarnings("unchecked")
          public static void unZipFileByOpache(org.apache.tools.zip.ZipFile zipFile,
          String unZipRoot) throws Exception, IOException {
          java.util.Enumeration e = zipFile.getEntries();
          System.out.println(zipFile.getEncoding());
          org.apache.tools.zip.ZipEntry zipEntry;
          while (e.hasMoreElements()) {
          zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
          InputStream fis = zipFile.getInputStream(zipEntry);
          if (zipEntry.isDirectory()) {
          } else {
          File file = new File(unZipRoot + File.separator
          + zipEntry.getName());
          File parentFile = file.getParentFile();
          parentFile.mkdirs();
          FileOutputStream fos = new FileOutputStream(file);
          byte[] b = new byte[1024];
          int len;
          while ((len = fis.read(b, 0, b.length)) != -1) {
          fos.write(b, 0, len);
          }
          fos.close();
          fis.close();
          }
          }
          }
          public static void ZipFile(String zipFileName, String inputFileName)
          throws Exception {
          org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(
          new FileOutputStream(zipFileName));
          out.setEncoding("gbk");// 設置的和文件名字格式一樣或開發環境編碼設置一樣的話就能正常顯示了
          File inputFile = new File(inputFileName);
          zip(out, inputFile, "", true);
          System.out.println("zip done");
          out.close();
          }
          public static void unZipFile(String unZipFileName, String unZipPath)
          throws Exception {
          org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(
          unZipFileName, "gbk");
          unZipFileByOpache(zipFile, unZipPath);
          System.out.println("unZip Ok");
          }
          }

          posted on 2014-10-09 10:28 順其自然EVO 閱讀(181) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 厦门市| 太和县| 中西区| 孝昌县| 綦江县| 新龙县| 天镇县| 安庆市| 泰和县| 鹰潭市| 长垣县| 老河口市| 会东县| 华宁县| 雷山县| 孝昌县| 射阳县| 察哈| 易门县| 新乡市| 昌吉市| 宁明县| 甘南县| 乌什县| 旌德县| 哈巴河县| 平顶山市| 黄骅市| 瓮安县| 滦平县| 景德镇市| 安塞县| 葫芦岛市| 九龙坡区| 林芝县| 清水县| 景谷| 扎赉特旗| 洪湖市| 霍山县| 晋江市|