欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

apache ant進(jìn)行zip解壓縮操作示例分享

 更新時(shí)間:2014年02月10日 09:12:11   作者:  
本文主要介紹了使用apache ant進(jìn)行zip解壓縮操作的方法,可以解決中文編碼和首層父類無法創(chuàng)建問題,需要的朋友可以參考下

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

復(fù)制代碼 代碼如下:

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("目標(biāo)文件無法創(chuàng)建!!!");
  BufferedOutputStream bos = null;
  FileOutputStream out = null;
  ZipOutputStream zOut = null;
  try {
   // 創(chuàng)建文件輸出對(duì)象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)
      // 遞歸進(jìn)入本方法
      zip(zOut, f, base + f.getName());
   }
   // 如果文件句柄是文件
   else {
    if (base == "") {
     base = file.getName();
    }
    // 填入文件句柄
    zOut.putNextEntry(new ZipEntry(base));
    // 開始?jí)嚎s
    // 從文件入流讀,寫入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
  *            解壓的目的地點(diǎn)
  * @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正式版破解與激活(親測(cè)可用)");
  dir = new String("F:/111.JPG");
  zip(dir, "f:/BZBXB/zipant.zip");
  unZip("f:/BZBXB/zipant.zip", "f:/XX/xx/");
 }
}

相關(guān)文章

  • Java中控制流程語句的深入講解

    Java中控制流程語句的深入講解

    流程控制語句顧名思義就是控制程序走向的語句,其中包括條件語句,分支語句和循環(huán)語句,這篇文章主要給大家介紹了關(guān)于Java中控制流程語句的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • 解析SpringBoot自定義參數(shù)校驗(yàn)注解

    解析SpringBoot自定義參數(shù)校驗(yàn)注解

    這篇文章主要介紹了SpringBoot自定義參數(shù)校驗(yàn)注解,引入依賴,spring validation是在hibernate-validator上做了一層封裝,文中提到了定義參數(shù)校驗(yàn)注解與處理器的示例代碼,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • Java程序連接數(shù)據(jù)庫的常用的類和接口介紹

    Java程序連接數(shù)據(jù)庫的常用的類和接口介紹

    這篇文章主要介紹了Java程序連接數(shù)據(jù)庫的常用的類和接口,包括Connection類和Statement類等,需要的朋友可以參考下
    2015-10-10
  • 完美解決idea無法搜索下載插件的問題

    完美解決idea無法搜索下載插件的問題

    這篇文章主要介紹了完美解決idea無法搜索下載插件的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 解決mybatis無法給帶有下劃線屬性賦值問題

    解決mybatis無法給帶有下劃線屬性賦值問題

    這篇文章主要介紹了解決mybatis無法給帶有下劃線屬性賦值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 使用maven打包生成doc文檔和打包源碼

    使用maven打包生成doc文檔和打包源碼

    這篇文章主要介紹了使用maven打包生成doc文檔和打包源碼的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 通過volatile驗(yàn)證線程之間的可見性

    通過volatile驗(yàn)證線程之間的可見性

    這篇文章主要介紹了通過volatile驗(yàn)證線程之間的可見性,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot 注解事務(wù)聲明式事務(wù)的方式

    SpringBoot 注解事務(wù)聲明式事務(wù)的方式

    springboot使用上述注解的幾種方式開啟事物,可以達(dá)到和xml中聲明的同樣效果,但是卻告別了xml,使你的代碼遠(yuǎn)離配置文件。今天就扒一扒springboot中事務(wù)使用注解的玩法,感興趣的朋友一起看看吧
    2017-09-09
  • dubbo之@Reference注解作用說明

    dubbo之@Reference注解作用說明

    這篇文章主要介紹了dubbo之@Reference注解作用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 使用RestTemplate調(diào)用https接口跳過證書驗(yàn)證

    使用RestTemplate調(diào)用https接口跳過證書驗(yàn)證

    這篇文章主要介紹了使用RestTemplate調(diào)用https接口跳過證書驗(yàn)證,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論