一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java實(shí)現(xiàn)的zip壓縮及解壓縮工具類示例

Java實(shí)現(xiàn)的zip壓縮及解壓縮工具類示例

2021-03-18 12:19soundfly Java教程

這篇文章主要介紹了Java實(shí)現(xiàn)的zip壓縮及解壓縮工具類,結(jié)合實(shí)例形式分析了java對文件的進(jìn)行zip壓縮及解壓縮的具體操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)的zip壓縮及解壓縮工具類。分享給大家供大家參考,具體如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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);// 從當(dāng)前目錄開始
      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();
//   }
//
// }
}

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

原文鏈接:http://blog.csdn.net/soundfly/article/details/16861881

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产午夜精品久久久久 | 十六一下岁女子毛片免费 | 免费在线看 | 日韩一区二区三区不卡视频 | 国产偷啪 | 精品无码一区在线观看 | 日本护士撒尿xxxxhd | 精品国产福利一区二区在线 | 香蕉tv亚洲专区在线观看 | 国产91一区二区在线播放不卡 | 亚洲激情一区 | 国产网站免费观看 | 俄罗斯一级毛片免费播放 | 无耻之徒第十一季在线观看 | 希岛爱理作品在线观看 | 久久国产精品免费网站 | 国产大乳美女挤奶视频 | 韩国女主播一区二区视频 | 午夜dj免费视频观看社区 | 日本一区二区视频免费播放 | 91精品乱码一区二区三区 | 热门小说同人h改编h | aaa一级最新毛片 | 特黄特级高清免费视频毛片 | julia ann全部在线hd | 国产良家 | 5x视频在线观看 | 俺来操| 校园纯肉H教室第一次 | 国产欧美精品一区二区三区 | 男人的天堂久久精品激情a 男人的天堂va | 星球大战成人h无删减版 | 国产思妍小仙女一二区 | 91精品久久国产青草 | 久久国产主播福利在线 | 干操视频 | 久久精品观看影院2828 | 农村妇女野外性生话免费视频 | 青青青在线观看国产精品 | 87影院在线观看视频在线观看 | 黄动漫车车好快的车车双女主 |