java实现压缩解压缩

压缩
package org.imos.manager.util.zip;

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

public class DataCompressZip {

 /**
  * @Description: 压缩打包文件
  * @param zipFileName
  *            压缩文件路径
  * @param compressBasedir
  *            压缩文件根路径
  * @return
  */
 public boolean zip(String zipFileName, String compressBasedir) {
  try {

   File file = new File(compressBasedir);
   FileOutputStream fileout = new FileOutputStream(zipFileName);
   ZipOutputStream out = new ZipOutputStream(fileout);
   zip(out, file, "");
   out.close();
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }

 /**
  * 定义压缩文件及目录为zip文件的方法
  * 
  * @param out
  * @param f
  * @param base
  * @throws Exception
  */
 public void zip(ZipOutputStream out, File f, String base) {

  try {
   if (f.isDirectory()) {
    // 获取f目录下所有文件及目录,作为一个File数组返回
    File[] fl = f.listFiles();
    out.putNextEntry(new ZipEntry(base + "/"));
    base = base.length() == 0 ? "" : base + "/";
    // base=new String(base.getBytes(),"UTF-8");
    for (int i = 0; i < fl.length; i++) {
     String name = new String(
       (base + fl[i].getName()).getBytes(), "UTF-8");
     zip(out, fl[i], name);
    }
   } else {

    ZipEntry ze = new ZipEntry(base);
    out.putNextEntry(ze);

    FileInputStream in = new FileInputStream(f);
    byte[] b = new byte[1024];
    int length;
    while ((length = in.read(b)) != -1) {
     out.write(b, 0, length);
    }
    in.close();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 

 public static void main(String[] args) {
  DataCompressZip zip = new DataCompressZip();
  zip.zip("D:\\temp\\ZCRW0001.zip", "D:\\temp\\ZCRW0001");
 }
}


解压缩

public class DataUnZip {

 static final int BUFFER = 2048;

 public static void main(String argv[]) {
  try {
   DataUnZip uz = new DataUnZip();
   String zippath = "d:\\temp";// /解压到的目标文件路径
   String zipDir = "D:\\temp\\ZCRW0001.zip";// 要解压的压缩文件的存放路径
   uz.ReadZip(zippath, zipDir);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * @Description 解压zip文件
  * @param zippath解压目标目录
  * @param unzipPath
  *            待解压的文件地址
  * @throws Exception
  */
 public void ReadZip(String destZipPath, String unzipPath) throws Exception {
  try {
   BufferedOutputStream dest = null;
   FileInputStream fis = new FileInputStream(unzipPath);
   ZipInputStream zis = new ZipInputStream(
     new BufferedInputStream(fis));
   ZipEntry entry;
   int begin = unzipPath.lastIndexOf("\\") + 1;
   int end = unzipPath.lastIndexOf(".");
   String zipRealName = unzipPath.substring(begin, end);
   while ((entry = zis.getNextEntry()) != null) {
    int count;
    byte data[] = new byte[BUFFER];
    // 如果为目录条目,则返回 true,执行下列语句
    if (entry.isDirectory()) {
     continue;
    }

    dest = new BufferedOutputStream(new FileOutputStream(
      getRealFileName(destZipPath + "\\" + zipRealName,
        entry.getName())));
    // dest = new BufferedOutputStream(
    // new
    // FileOutputStream(getRealFileName(zippath,entry.getName())));
    while ((count = zis.read(data, 0, BUFFER)) != -1) {
     dest.write(data, 0, count);
    }
    dest.flush();
    dest.close();
   }
   zis.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 给定根目录,返回一个相对路径所对应的实际文件名.
  * 
  * @param zippath
  *            指定根目录
  * @param absFileName
  *            相对路径名,来自于ZipEntry中的name
  * @return java.io.File 实际的文件
  */
 private File getRealFileName(String zippath, String absFileName) {
  String[] dirs = absFileName.split("/", absFileName.length());
  File ret = new File(zippath);// 创建文件对象
  if (dirs.length > 1) {
   for (int i = 0; i < dirs.length - 1; i++) {
    ret = new File(ret, dirs[i]);
   }
  }
  if (!ret.exists()) {// 检测文件是否存在
   ret.mkdirs();// 创建此抽象路径名指定的目录
  }
  ret = new File(ret, dirs[dirs.length - 1]);// 根据 ret 抽象路径名和 child
  // 路径名字符串创建一个新 File 实例
  return ret;
 }
}


你可能感兴趣的:(java实现压缩解压缩)