解壓文件源碼(須要引入ant.jar包,解決zip文件名爲中文出現亂碼問題)

package cn.com.burgeon.util; java

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.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest; apache

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream; 數組

/**
 * 功能:zip壓縮、解壓(支持中文文件名) 說明:使用Apache Ant提供的zip工具org.apache.tools.zip實現zip壓縮和解壓功能.
 * 解決了因爲java.util.zip包不支持漢字的問題。 使用java.util.zip包時,當zip文件中有名字爲中文的文件時, 就會出現異常:
 * "Exception in thread "main " java.lang.IllegalArgumentException at
 * java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285) <p/> <p/>
 * 注意: 一、使用時把ant.jar放到classpath中,程序中使用 import org.apache.tools.zip.*; 二、本程序使用Ant
 * 1.7.1 中的ant.jar部分類,ant-zip-1.7.1只保留Ant的zip壓縮功能,以減少ant.jar的大小。
 * 四、ZipEntry的isDirectory()方法中,目錄以"/"結尾。 <p/> <p/>
 * ------------------------------------------------ 可將主函數註釋去掉以單獨測試ZipFileUtils類。
 * Compile: javac -cp Ant.jar ZipFileUtils.java <p/> Usage:(將ant.jar直接放在當前目錄)
 * 壓縮: java -cp Ant.jar;. ZipFileUtils -zip [directoryName | fileName]... 解壓:
 * java -cp Ant.jar;. ZipFileUtils -unzip "fileName.zip" <p/>
 * ------------------------------------------------
 */
public class UnzipUtil { 函數

 private static int bufSize = 8096; // size of bytes 工具

 /**
  * 壓縮文件夾內的全部文件和目錄(不支持有中文目錄或文件名)。
  *
  * @param zipDirectory
  *            須要壓縮的文件夾名
  * @return 成功返回null,不然返回失敗信息
  */
 public static String zip(String zipDirectory) {
  File zipDir = new File(zipDirectory);
  return zip(zipDirectory, zipDir.getPath(), false);
 } 測試

 /**
  * 壓縮文件夾內的全部文件和目錄(不支持有中文目錄或文件名)。
  *
  * @param zipDirectory
  *            須要壓縮的文件夾名
  * @param zipFileName
  *            壓縮後的zip文件名,若是後綴不是".zip, .jar, .war", 自動添加後綴".zip"。
  * @param includeSelfDir
  *            是否包含自身文件夾
  * @return 成功返回null,不然返回失敗信息
  */
 public static String zip(String zipDirectory, String zipFileName,
   boolean includeSelfDir) {
  File zipDir = new File(zipDirectory);
  File[] willZipFileArr;
  if (includeSelfDir || zipDir.isFile()) {
   willZipFileArr = new File[] { zipDir };
  } else {
   willZipFileArr = zipDir.listFiles();
  }
  return zip(willZipFileArr, zipFileName);
 } spa

 /**
  * 壓縮多個文件或目錄。能夠指定多個單獨的文件或目錄。
  *
  * @param files
  *            要壓縮的文件或目錄組成的<code>File</code>數組。
  * @param zipFileName
  *            壓縮後的zip文件名,若是後綴不是".zip, .jar, .war",自動添加後綴".zip"。
  * @return 成功返回null,不然返回失敗信息
  */
 public static String zip(File[] files, String zipFileName) {
  if (files == null) {
   return "待壓縮的文件不存在。";
  } .net

  // 未指定壓縮文件名,默認爲"ZipFile"
  if (zipFileName == null || zipFileName.equals(""))
   zipFileName = "ZipFile"; code

  // 添加".zip"後綴
  if (!zipFileName.toLowerCase().endsWith(".zip")
    && !zipFileName.toLowerCase().endsWith(".jar")
    && !zipFileName.toLowerCase().endsWith(".war"))
   zipFileName += ".zip"; 對象

  JarOutputStream jarOutput = null;
  try {
   jarOutput = new JarOutputStream(new FileOutputStream(zipFileName),
     new Manifest());

   for (File file : files) {
    zipFiles(file, jarOutput, "");
   }

  } catch (Exception e) {

  } finally {
   if (jarOutput != null) {
    try {
     jarOutput.close();
    } catch (IOException e) {

    }
   }
  }
  return null;
 }

 /**
  * @param file
  *            壓縮文件
  * @param jos
  *            JarOutputStream
  * @param pathName
  *            相對路徑
  * @throws Exception
  *             異常
  */
 private static void zipFiles(File file, JarOutputStream jos, String pathName)
   throws Exception {
  String fileName = pathName + file.getName();
  if (file.isDirectory()) {
   fileName = fileName + "/";
   jos.putNextEntry(new JarEntry(fileName));
   String fileNames[] = file.list();
   if (fileNames != null) {
    for (int i = 0; i < fileNames.length; i++) {
     zipFiles(new File(file, fileNames[i]), jos, fileName);
    }
    jos.closeEntry();
   }
  } else {
   JarEntry jarEntry = new JarEntry(fileName);
   BufferedInputStream in = new BufferedInputStream(
     new FileInputStream(file));
   jos.putNextEntry(jarEntry);

   byte[] buf = new byte[bufSize];
   int len;
   while ((len = in.read(buf)) >= 0) {
    jos.write(buf, 0, len);
   }
   in.close();
   jos.closeEntry();
  }
 }

 /**
  * 壓縮文件夾內的全部文件和目錄。
  *
  * @param zipDirectory
  *            須要壓縮的文件夾名
  * @return 成功返回null,不然返回失敗信息
  */
 public static String antzip(String zipDirectory) {
  File zipDir = new File(zipDirectory);
  return antzip(zipDirectory, zipDir.getPath(), false);
 }

 /**
  * 壓縮文件夾內的全部文件和目錄。
  *
  * @param zipDirectory
  *            須要壓縮的文件夾名
  * @param zipFileName
  *            壓縮後的zip文件名,若是後綴不是".zip, .jar, .war", 自動添加後綴".zip"。
  * @param includeSelfDir
  *            是否包含自身文件夾
  * @return 成功返回null,不然返回失敗信息
  */
 public static String antzip(String zipDirectory, String zipFileName,
   boolean includeSelfDir) {
  File zipDir = new File(zipDirectory);
  File[] willZipFileArr;
  if (includeSelfDir || zipDir.isFile()) {
   willZipFileArr = new File[] { zipDir };
  } else {
   willZipFileArr = zipDir.listFiles();
  }
  return antzip(willZipFileArr, zipFileName);
 }

 /**
  * 壓縮多個文件或目錄。能夠指定多個單獨的文件或目錄。
  *
  * @param files
  *            要壓縮的文件或目錄組成的<code>File</code>數組。
  * @param zipFileName
  *            壓縮後的zip文件名,若是後綴不是".zip, .jar, .war",自動添加後綴".zip"。
  * @return 成功返回null,不然返回失敗信息
  */
 public static String antzip(File[] files, String zipFileName) {
  // 未指定壓縮文件名,默認爲"ZipFile"
  if (zipFileName == null || zipFileName.equals(""))
   zipFileName = "ZipFile";

  // 添加".zip"後綴
  if (!zipFileName.toLowerCase().endsWith(".zip")
    && !zipFileName.toLowerCase().endsWith(".jar")
    && !zipFileName.toLowerCase().endsWith(".war"))
   zipFileName += ".zip";

  ZipOutputStream zipOutput = null;
  try {
   zipOutput = new ZipOutputStream(new BufferedOutputStream(
     new FileOutputStream(zipFileName)));
   zipOutput.setEncoding("GBK");

   for (File file : files) {
    antzipFiles(file, zipOutput, "");
   }

  } catch (Exception e) {

   return e.getMessage();
  } finally {
   try {
    assert zipOutput != null;
    zipOutput.close();
   } catch (Exception e) {

   }
  }
  return null;
 }

 /**
  * @param file
  *            壓縮文件
  * @param zipOutput
  *            ZipOutputStream
  * @param pathName
  *            相對路徑
  * @throws Exception
  *             異常
  */
 private static void antzipFiles(File file, ZipOutputStream zipOutput,
   String pathName) throws Exception {
  String fileName = pathName + file.getName();
  if (file.isDirectory()) {
   fileName = fileName + "/";
   zipOutput.putNextEntry(new ZipEntry(fileName));
   String fileNames[] = file.list();
   if (fileNames != null) {
    for (int i = 0; i < fileNames.length; i++) {
     antzipFiles(new File(file, fileNames[i]), zipOutput,
       fileName);
    }
    zipOutput.closeEntry();
   }
  } else {
   ZipEntry jarEntry = new ZipEntry(fileName);
   BufferedInputStream in = new BufferedInputStream(
     new FileInputStream(file));
   zipOutput.putNextEntry(jarEntry);

   byte[] buf = new byte[bufSize];
   int len;
   while ((len = in.read(buf)) >= 0) {
    zipOutput.write(buf, 0, len);
   }
   in.close();
   zipOutput.closeEntry();
  }
 }

 /**
  * 解壓指定zip文件。
  *
  * @param unZipFile
  *            須要解壓的zip文件對象
  * @return 成功返回null,不然返回失敗信息
  */
 public static String unZip(File unZipFile) {
  return unZip(unZipFile.getPath(), null);
 }

 /**
  * 解壓指定zip文件到指定的目錄。
  *
  * @param unZipFile
  *            須要解壓的zip文件對象
  * @param destFileName
  *            解壓目的目錄
  * @return 成功返回null,不然返回失敗信息
  */
 public static String unZip(File unZipFile, String destFileName) {
  return unZip(unZipFile.getPath(), destFileName);
 }

 /**
  * 解壓指定zip文件。
  *
  * @param unZipFileName
  *            須要解壓的zip文件名
  * @return 成功返回null,不然返回失敗信息
  */
 public static String unZip(String unZipFileName) {
  return unZip(unZipFileName, null);
 }

 /**
  * 解壓指定zip文件到指定的目錄。
  *
  * @param unZipFileName
  *            須要解壓的zip文件名
  * @param destFileName
  *            解壓目的目錄,若是爲null則爲當前zip文件全部目錄
  * @return 成功返回null,不然返回失敗信息
  */
 public static String unZip(String unZipFileName, String destFileName) {
  File unzipFile = new File(unZipFileName);

  if (destFileName == null || destFileName.trim().length() == 0) {
   destFileName = unzipFile.getParent();
  }

  File destFile;
  ZipFile zipFile = null;
  try {
   zipFile = new ZipFile(unzipFile, "GBK");
   for (Enumeration entries = zipFile.getEntries(); entries
     .hasMoreElements();) {
    ZipEntry entry = (ZipEntry) entries.nextElement();
    destFile = new File(destFileName, entry.getName());

    unZipFile(destFile, zipFile, entry); // 執行解壓
   }
  } catch (Exception e) {

   return e.getMessage();
  } finally {
   try {
    assert zipFile != null;
    zipFile.close();
   } catch (Exception e) {

   }
  }
  return null;
 }

 /* 執行解壓 */
 private static void unZipFile(File destFile, ZipFile zipFile, ZipEntry entry)
   throws IOException {
  InputStream inputStream;
  FileOutputStream fileOut;
  if (entry.isDirectory()) // 是目錄,則建立之
  {
   destFile.mkdirs();
  } else // 是文件
  {
   // 若是指定文件的父目錄不存在,則建立之.
   File parent = destFile.getParentFile();
   if (parent != null && !parent.exists()) {
    parent.mkdirs();
   }

   inputStream = zipFile.getInputStream(entry);

   fileOut = new FileOutputStream(destFile);
   byte[] buf = new byte[bufSize];
   int readedBytes;
   while ((readedBytes = inputStream.read(buf)) > 0) {
    fileOut.write(buf, 0, readedBytes);
   }
   fileOut.close();

   inputStream.close();
  }
 }

 /**
  * 設置壓縮或解壓時緩衝區大小,單位字節。
  *
  * @param bufSize
  *            緩衝區大小
  */
 public void setBufSize(int bufSize) {
  UnzipUtil.bufSize = bufSize;
 }

}

相關文章
相關標籤/搜索