java ZIP 檔案壓縮

來源:互聯網
上載者:User

package client;

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

/**
 * 壓縮實作類別 <br>
 * 主要實現: <br>
 * <p>
 * 壓縮單個檔案、
 * <p>
 * 壓縮檔夾下的所有檔案及子檔案夾
 *
 * <p>
 *
 * @author libo
 *
 */

public class ZipUtil {

 /**
  * 壓縮單個檔案
  *
  * @param filePath
  *            檔案路徑
  * @param fileName
  *            檔案名稱字
  * @param objDir
  *            壓縮檔目標檔案夾
  * @param ojbZipName
  *            壓縮檔名字
  * @return
  * @throws IOException
  */
 public boolean zip(String filePath, String fileName, String objDir,
   String ojbZipName) throws IOException {
  boolean tag = false;
  ZipOutputStream zos = null;
  FileInputStream fis = null;
  try {
   zos = new ZipOutputStream(new FileOutputStream(objDir + ojbZipName));

   fis = new FileInputStream(filePath + fileName);
   byte[] b = new byte[4096];
   int i = 0;

   zos.putNextEntry(new ZipEntry(fileName));
   while ((i = (fis.read(b))) > 0) {
    zos.write(b, 0, i);
   }
  } finally {
   try {
    fis.close();
   } catch (IOException ioe) {
    ioe.printStackTrace();
    throw new IOException();
   }
   try {
    zos.close();
   } catch (IOException ioe) {
    ioe.printStackTrace();
    throw new IOException();
   }
  }
  return tag;
 }

 /**
  * 壓縮一個檔案夾下的所有檔案 注意中間路徑串連用"/" 如:c:/tt/ttt
  *
  * @param srcPath
  *            要壓縮的檔案夾路徑
  * @param objZipPath
  *            目標檔案夾路徑
  * @param ojbZipName
  *            目標壓縮檔名字
  * @return
  */
 public boolean zipDir(String srcPath, String objZipPath, String ojbZipName)
   throws Exception {

  ZipOutputStream zos = null;
  try {
   File objFile = new File(objZipPath, ojbZipName);
   zos = new ZipOutputStream(new FileOutputStream(objFile));

   if (srcPath == null) {
    System.out.println("傳入的源檔案夾路徑字串不可為空!");
    throw new java.lang.NullPointerException();
   }
   String dirName = "";
   File file = new File(srcPath);
   if (!file.isDirectory()) {
    throw new Exception("傳入了不正確的源檔案夾路徑!");
   } else {
    dirName = srcPath.substring(srcPath.lastIndexOf("/") + 1);
    if (dirName == null || "".equals(dirName)) {
     String subStr = srcPath.substring(0, srcPath.length() - 2);
     dirName = subStr.substring(subStr.lastIndexOf("/") + 1);
    }
    ZipEntry ze = new ZipEntry(dirName + "/");
    zos.putNextEntry(ze);
    if (dirName == null || "".equals(dirName)) {
     throw new Exception("傳入了不正確的源檔案夾路徑!");
    }
   }

   File[] files = file.listFiles();

   for (int i = 0; i < files.length; i++) {
    zipFile(dirName + "/", files[i], zos);
   }
   return true;
  } finally {
   if (zos != null) {
    try {
     zos.close();
    } catch (IOException e1) {
     e1.printStackTrace();
    }
   }
  }

 }

 /**
  * 用來壓縮檔夾下的所有子檔案 此方法為一個遞迴調法方法
  *
  * @param zipPath
  *            要壓縮的檔案在壓縮包中的相對路徑
  * @param file
  *             要壓縮的檔案引用
  * @param zos
  *               壓縮檔輸出資料流
  * @throws IOException
  */
 private void zipFile(String zipPath, File file, ZipOutputStream zos)
   throws IOException {
  // 是檔案夾的操作
  if (file.isDirectory()) {
   ZipEntry ze = new ZipEntry(zipPath + file.getName() + "/");
   zos.putNextEntry(ze);
   // 遞迴調用
   for (int i = 0; i < file.listFiles().length; i++) {
    zipFile(zipPath + file.getName() + "/", file.listFiles()[i],
      zos);
   }
   // 是檔案時的操作
  } else {
   FileInputStream fis = null;
   try {
    fis = new FileInputStream(file);
    ZipEntry ze = new ZipEntry(zipPath + file.getName());
    zos.putNextEntry(ze);
    byte[] b = new byte[4096];
    int i = 0;
    while ((i = (fis.read(b))) > 0) {
     zos.write(b, 0, i);
    }
   } finally {
    if (fis != null) {
     fis.close();
    }
   }
  }
 }

 public static void main(String[] args) {
  ZipUtil zt = new ZipUtil();
  try {
   zt.zipDir("c:/gif", "c:/", "test.zip");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.