java 中压缩与解压

来源:互联网 发布:烟花算法 matlab 编辑:程序博客网 时间:2024/04/28 08:15

package com.common.util;

import java.io.*;
import java.util.ArrayList;
import java.util.zip.*;

public class UnZip {

 public static int iCompressLevel; //压缩比  取值范围为0~9

 public static boolean bOverWrite; //是否覆盖同名文件 取值范围为True和False

 private static ArrayList allFiles = new ArrayList();

 public static String sErrorMessage;

 public static ArrayList unZip(String zipPathFile, String DestPath) {
  ArrayList allFileName = new ArrayList();
  try {
   //先指定压缩档的位置和档名,建立FileInputStream对象
   FileInputStream in = new FileInputStream(zipPathFile);
   //将fins传入ZipInputStream中
   ZipInputStream zin = new ZipInputStream(in);
   ZipEntry ent = null;
   byte ch[] = new byte[256];
   while ((ent = zin.getNextEntry()) != null) {
    File zfile = new File(DestPath + ent.getName());
    File fpath = new File(zfile.getParentFile().getPath());
    if (ent.isDirectory()) {
     if (!zfile.exists())
      zfile.mkdirs();
     zin.closeEntry();
    } else {
     if (!fpath.exists())
      fpath.mkdirs();
     FileOutputStream fouts = new FileOutputStream(zfile);
     int i;
     allFileName.add(zfile.getAbsolutePath());
     while ((i = zin.read(ch)) != -1)
      fouts.write(ch, 0, i);
     zin.closeEntry();
     fouts.close();
    }
    //System.out.println("解压文件: " + ent.getName() + zipPathFile);
   }
   in.close();
   zin.close();
   sErrorMessage = "OK";
  } catch (Exception e) {
   System.err.println("Extract error:" + e.getMessage());
   sErrorMessage = e.getMessage();
  }
  allFiles.clear();
  System.out.println("完成");
  return allFileName;
 }
 
 public static void main(String[] args) {
  
  UnZip.unZip("c://order_20.rar", "c://un//");

 }
}