java 壓縮解壓文件

b4c2 9年前發布 | 2K 次閱讀 Java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

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

/**

  • ZIP文件壓縮和解壓(要使用apache ant.jar以處理中文亂碼)
  • @author sunjun
  • @version 2.0 */ public class ZipUtil {

    /**

    • 壓縮文件file成zip文件zipFile
    • @param file
    • 要壓縮的文件
    • @param zipFile
    • 壓縮文件存放地方
    • @throws Exception */ public static void zip(File file, File zipFile) throws Exception { ZipOutputStream output = null; try { output = new ZipOutputStream(new FileOutputStream(zipFile)); // 頂層目錄開始 zipFile(output, file, ""); } catch (Exception ex) { ex.printStackTrace(); } finally { // 關閉流 if (output != null) { output.flush(); output.close(); } } }

    /**

    • 壓縮文件為zip格式
    • @param output
    • ZipOutputStream對象
    • @param file
    • 要壓縮的文件或文件夾
    • @param basePath
    • 條目根目錄
    • @throws IOException */ private static void zipFile(ZipOutputStream output, File file, String basePath) throws IOException { FileInputStream input = null; try { // 文件為目錄 if (file.isDirectory()) { // 得到當前目錄里面的文件列表 File list[] = file.listFiles(); basePath = basePath + (basePath.length() == 0 ? "" : "/")
      • file.getName(); // 循環遞歸壓縮每個文件 for (File f : list) zipFile(output, f, basePath); } else { // 壓縮文件 basePath = (basePath.length() == 0 ? "" : basePath + "/")
      • file.getName(); // System.out.println(basePath); output.putNextEntry(new ZipEntry(basePath)); input = new FileInputStream(file); int readLen = 0; byte[] buffer = new byte[1024 8]; while ((readLen = input.read(buffer, 0, 1024 8)) != -1) output.write(buffer, 0, readLen); } } catch (Exception ex) { ex.printStackTrace(); } finally { // 關閉流 if (input != null) input.close(); } }

    /**

    • 解壓zip文件
    • @param zipFilePath
    • zip文件絕對路徑
    • @param unzipDirectory
    • 解壓到的目錄
    • @throws Exception */ public static void unzip(String zipFilePath, String unzipDirectory) throws Exception { // 定義輸入輸出流對象 InputStream input = null; OutputStream output = null; try { // 創建文件對象 File file = new File(zipFilePath); // 創建zip文件對象 ZipFile zipFile = new ZipFile(file); // 創建本zip文件解壓目錄 String name = file.getName().substring(0, file.getName().lastIndexOf(".")); File unzipFile = new File(unzipDirectory + "/" + name); if (unzipFile.exists()) unzipFile.delete(); unzipFile.mkdir(); // 得到zip文件條目枚舉對象 Enumeration zipEnum = zipFile.getEntries(); // 定義對象 ZipEntry entry = null; String entryName = null, path = null; String names[] = null; int length; // 循環讀取條目 while (zipEnum.hasMoreElements()) { // 得到當前條目 entry = (ZipEntry) zipEnum.nextElement(); entryName = new String(entry.getName()); // 用/分隔條目名稱 names = entryName.split("\\/"); length = names.length; path = unzipFile.getAbsolutePath(); for (int v = 0; v < length; v++) { if (v < length - 1) // 最后一個目錄之前的目錄 FileUtil.createDirectory(path += "/" + names[v] + "/"); else { // 最后一個 if (entryName.endsWith("/")) // 為目錄,則創建文件夾 FileUtil.createDirectory(unzipFile
      .getAbsolutePath()
      + "/" + entryName);
      
      else { // 為文件,則輸出到文件 input = zipFile.getInputStream(entry); output = new FileOutputStream(new File(unzipFile
      .getAbsolutePath()
      + "/" + entryName));
      
      byte[] buffer = new byte[1024 8]; int readLen = 0; while ((readLen = input.read(buffer, 0, 1024 8)) != -1) output.write(buffer, 0, readLen); } } } } } catch (Exception ex) { ex.printStackTrace(); } finally { // 關閉流 if (input != null) input.close(); if (output != null) { output.flush(); output.close(); } } }

    /**

    • 測試
    • @param args
    • @throws Exception */ public static void main(String[] args) throws Exception { unzip("d:/桌面.zip", "f:/"); System.out.println("over...................."); zip(new File("C:/a"), new File("d:/桌面.zip")); System.out.println("over.............."); }

}</pre>

 本文由用戶 b4c2 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!