java压缩文件夹或文件

package com.proxy;

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

public class ZipUtil {
 public enum zipEnum {
  byteLength(1024 * 2), srcPath("d:/快盘-学习"), objPath("d:/"), zipName(
    "ac.zip");
  zipEnum(int intData) {
   this.intData = intData;
  }

  zipEnum(String strData) {
   this.strData = strData;
  }

  public int getIntData() {
   return intData;
  }

  public String getStrData() {
   return strData;
  }

  private int intData = -1;
  private String strData = null;
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  zipDir(zipEnum.srcPath.getStrData(), zipEnum.objPath.getStrData(),
    zipEnum.zipName.getStrData());
 }
 /**
  * @判断输入的地址是文件名还是文件夹名
  * @param srcPath:被压缩的文件或文件夹(源文件)
  * @param objPath:压缩至某个路径
  * @param zipName:压缩文件名
  */
 public static void zipDir(String srcPath, String objPath, String zipName) {
  if (srcPath == null) {
   return;
  }
  File fileIn = new File(srcPath);
  File fileOut = new File(objPath, zipName);
  ZipOutputStream zipOut = null;
  try {
   zipOut = new ZipOutputStream(new FileOutputStream(fileOut));
   //判断源文件是否是文件夹
   if (!fileIn.isAbsolute()) {
    //如果是文件
    if (fileIn.isFile()) {
     zipFile("", fileIn, zipOut);
    } else {
     System.out.println("输入的源文件地址错误");
    }
   } else {
    //如果是文件夹
    zipDirFile("", fileIn, zipOut);
   }
  } catch (FileNotFoundException e) {
   System.out.println("目标文件错误");
   e.printStackTrace();
  } finally {
   if (zipOut != null) {
    try {
     zipOut.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }

 }
 /**
  * @压缩文件
  * @param relName:压缩包内的路径位置
  * @param fileIn:被压缩的文件
  * @param zipOut:压缩输出
  */
 private static void zipFile(String relName, File fileIn,
   ZipOutputStream zipOut) {
  //relName + fileIn.getName()类似于java文件夹下面java.txt文件这样的路径
  ZipEntry zipEntry = new ZipEntry(relName + fileIn.getName());
  FileInputStream fileSteam = null;
  byte[] buf = new byte[zipEnum.byteLength.getIntData()];
  int num = 0;
  try {
   fileSteam = new FileInputStream(fileIn);
   zipOut.putNextEntry(zipEntry);
   while ((num = fileSteam.read(buf)) != -1) {
    zipOut.write(buf, 0, num);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (fileSteam != null) {
    try {
     fileSteam.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   if (zipEntry != null) {
    zipEntry.clone();
   }
  }
 }
 /**
  * @遍历文件夹中的文件或文件夹
  * @param relName:压缩包内的路径位置
  * @param fileIn:被压缩的文件
  * @param zipOut:压缩输出
  */
 private static void zipDirFile(String relName, File fileIn,
   ZipOutputStream zipOut) {
  //如果是文件
  if (fileIn.isFile()) {
   zipFile(relName, fileIn, zipOut);
  } else {
   String relNames = relName + fileIn.getName() + File.separator;
   File[] files = fileIn.listFiles();
   for (File fi : files) {
    zipDirFile(relNames, fi, zipOut);
   }
   files.clone();
  }
 }
}

你可能感兴趣的:(java,压缩,java压缩,java文件压缩)