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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - apache ant進行zip解壓縮操作示例分享

apache ant進行zip解壓縮操作示例分享

2019-11-05 11:40java教程網 JAVA教程

本文主要介紹了使用apache ant進行zip解壓縮操作的方法,可以解決中文編碼和首層父類無法創(chuàng)建問題,需要的朋友可以參考下

需要導入ant.jar包,apache網站(http://ant.apache.org/bindownload.cgi)下載即可。

 

復制代碼代碼如下:


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipOutputStream;

 

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.zip.ZipEntry;

import com.xyq.io.util.CloseIoUtil;

public class ZipUtil {

 private static final String ENCODE = "UTF-8";

 public static void zip(String inputFilePath, String zipFileName) {

  File inputFile = new File(inputFilePath);
  if (!inputFile.exists())
   throw new RuntimeException("原始文件不存在!!!");
  File basetarZipFile = new File(zipFileName).getParentFile();
  if (!basetarZipFile.exists() && !basetarZipFile.mkdirs())
   throw new RuntimeException("目標文件無法創(chuàng)建!!!");
  BufferedOutputStream bos = null;
  FileOutputStream out = null;
  ZipOutputStream zOut = null;
  try {
   // 創(chuàng)建文件輸出對象out,提示:注意中文支持
   out = new FileOutputStream(new String(zipFileName.getBytes(ENCODE)));
   bos = new BufferedOutputStream(out);
   // 將文件輸出ZIP輸出流接起來
   zOut = new ZipOutputStream(bos);
   zip(zOut, inputFile, inputFile.getName());
   CloseIoUtil.closeAll(zOut, bos, out);

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private static void zip(ZipOutputStream zOut, File file, String base) {

  try {
   // 如果文件句柄是目錄
   if (file.isDirectory()) {
    // 獲取目錄下的文件
    File[] listFiles = file.listFiles();
    // 建立ZIP條目
    zOut.putNextEntry(new ZipEntry(base + "/"));
    base = (base.length() == 0 ? "" : base + "/");
    if (listFiles != null && listFiles.length > 0)
     // 遍歷目錄下文件
     for (File f : listFiles)
      // 遞歸進入本方法
      zip(zOut, f, base + f.getName());
   }
   // 如果文件句柄是文件
   else {
    if (base == "") {
     base = file.getName();
    }
    // 填入文件句柄
    zOut.putNextEntry(new ZipEntry(base));
    // 開始壓縮
    // 從文件入流讀,寫入ZIP 出流
    writeFile(zOut, file);
   }

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private static void writeFile(ZipOutputStream zOut, File file)
   throws IOException {

  FileInputStream in = null;
  BufferedInputStream bis = null;
  in = new FileInputStream(file);
  bis = new BufferedInputStream(in);
  int len = 0;
  byte[] buff = new byte[2048];
  while ((len = bis.read(buff)) != -1)
   zOut.write(buff, 0, len);
  zOut.flush();
  CloseIoUtil.closeAll(bis, in);
 }

 /****
  * 解壓
  * 
  * @param zipPath
  *            zip文件路徑
  * @param destinationPath
  *            解壓的目的地點
  * @param ecode
  *            文件名的編碼字符集
  */
 public static void unZip(String zipPath, String destinationPath) {

  File zipFile = new File(zipPath);
  if (!zipFile.exists())
   throw new RuntimeException("zip file " + zipPath
     + " does not exist.");

  Project proj = new Project();
  Expand expand = new Expand();
  expand.setProject(proj);
  expand.setTaskType("unzip");
  expand.setTaskName("unzip");
  expand.setSrc(zipFile);
  expand.setDest(new File(destinationPath));
  expand.setEncoding(ENCODE);
  expand.execute();
  System.out.println("unzip done!!!");
 }

 public static void main(String[] args) {

  String dir = new String("F:\\我的備份\\文檔\\MyEclipse+9.0正式版破解與激活(親測可用)");
  dir = new String("F:/111.JPG");
  zip(dir, "f:/BZBXB/zipant.zip");
  unZip("f:/BZBXB/zipant.zip", "f:/XX/xx/");
 }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品亚洲综合在线第一区 | 精品国产福利一区二区在线 | 久久综久久美利坚合众国 | 欧美大片一区二区三区 | 91大片淫黄大片在线天堂 | 1024免费观看完整版在线播放 | 国产区1 | poren18美女| 东北疯狂xxxxbbbb中国 | 精品国产欧美一区二区 | 欧美日韩亚洲区久久综合 | 日本黄色网页 | 好大好粗好舒服 | 久久99r66热这里有精品 | 四虎最新免费网址 | 日日精品| 国产未成女年一区二区 | 全色黄大色黄大片爽一次 | 日本aaaaa高清免费看 | 亚洲精品永久免费 | 天天操天天射天天色 | 青青草99热这里都是精品 | 亚洲国产精品久久久久久 | 国产一区二区三区久久精品小说 | 精品国产精品人妻久久无码五月天 | 亚洲精品一区在线观看 | 99 久久99久久精品免观看 | 国产未成女年一区二区 | 日本人护士免费xxxx视频 | 亚洲欧美一区二区三区在线观看 | 美女扒开尿口让男生添 漫画 | 嫩草视频在线观看视频播放 | 久久亚洲精品AV成人无 | 五月天中文在线 | jk制服白丝超短裙流白浆 | 香蕉国产精品偷在线播放 | 国内永久第一免费福利视频 | 风间由美被义子中文字幕 | 国产免费一区二区三区免费视频 | 俄罗斯女同和女同xx | 亚洲AV无码乱码在线观看浪潮 |