中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java 壓縮解壓文件

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
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 {
   // 關(guān)閉流
   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()) {
    // 得到當(dāng)前目錄里面的文件列表
    File list[] = file.listFiles();
    basePath = basePath + (basePath.length() == 0 ? "" : "/")
      + file.getName();
    // 循環(huán)遞歸壓縮每個文件
    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 {
   // 關(guān)閉流
   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 {
   // 創(chuàng)建文件對象
   File file = new File(zipFilePath);
   // 創(chuàng)建zip文件對象
   ZipFile zipFile = new ZipFile(file);
   // 創(chuàng)建本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;
   // 循環(huán)讀取條目
   while (zipEnum.hasMoreElements()) {
    // 得到當(dāng)前條目
    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("/")) // 為目錄,則創(chuàng)建文件夾
       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 {
   // 關(guān)閉流
   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..............");
 }

}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:js高級截取字符串功能

下一篇:對javascript中String類型的拓展