压缩指定目录及相应子目录的文件!

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class Zip {
 public static final String XG = "/";

 public static final char FXG = '//';

 /**
  * @param args
  */
 public static void main(String[] args) {
  doZip("E://upload", "E://test.zip");
 }

 /**
  * 收集要压缩目录中的所有文件的信息
  *
  * @param parentFile
  * @param nameList
  * @param fileList
  */
 static void listFile(File parentFile, List fileNameList, List fileList,
   String sourceFilePath) {
  if (parentFile.isDirectory()) {
   File[] files = parentFile.listFiles();
   for (int loop = 0; loop < files.length; loop++) {
    // 递归调用,列出文件
    listFile(files[loop], fileNameList, fileList, sourceFilePath);
   }
  } else {
   fileList.add(parentFile);
   // 下句很重要,按目录层次打包文件
   fileNameList.add(parentFile.getPath().substring(
     sourceFilePath.length()));
   // 将各级目录下的文件打包到同一级目录
   // fileNameList.add(parentFile.getName());
  }
 }

 /**
  * 将指定目录下的文件打包成zip.
  *
  * @param sourceFilePath
  *            生成zip文件的目录和文件名 如: "d:/dd/dd/dd";
  * @param destFilePath
  *            要打包的文件所在的目录,不包含具体文件名称 如: "E:/ZipOutOfPath2.zip"
  * @see 使用org.apache.tools.zip.ZipOutputStream,org.apache.tools.zip.ZipEntry解决中文问题
  *
  */
 public static void doZip(String sourceFilePath, String destFilePath) {
  sourceFilePath = sourceFilePath.replace(FXG, XG.charAt(0));
  if (!sourceFilePath.endsWith(XG)) {
   sourceFilePath += XG;
  }
  destFilePath = destFilePath.replace(FXG, XG.charAt(0));
  ArrayList fileNameList = new ArrayList(); // 存放文件名,并非含有路径的名字
  ArrayList fileList = new ArrayList(); // 存放文件对象
  try {
   createFile(destFilePath);
   FileOutputStream fileOut = new FileOutputStream(destFilePath);
   // 使用org.apache.tools.zip.ZipOutputStream
   ZipOutputStream outputStream = new ZipOutputStream(fileOut);
   // 根目录
   File rootFile = new File(sourceFilePath);
   listFile(rootFile, fileNameList, fileList, sourceFilePath);

   for (int loop = 0; loop < fileList.size(); loop++) {
    // 读入一个要压缩的文件
    File file = (File) fileList.get(loop);
    FileInputStream fileIn = new FileInputStream(file);
    // 放入压缩包
    // 使用org.apache.tools.zip.ZipEntry
    outputStream.putNextEntry(new ZipEntry((String) fileNameList
      .get(loop)));
    byte[] buffer = new byte[1024];
    int readTotal = 0;
    int perReadSize = 0;
    long fileSize = file.length();

    while (readTotal < fileSize) {
     perReadSize = fileIn.read(buffer, 0, 1024);
     readTotal += perReadSize;
     outputStream.write(buffer, 0, perReadSize);
     outputStream.flush();
    }
    outputStream.closeEntry();
    fileIn.close();
   }
   outputStream.close();
  } catch (IOException ioe) {
   ioe.printStackTrace();
  }
 }

 public static void createFile(String filePath) {
  if (filePath == null) {
   throw new IllegalArgumentException("filePath is null.");
  }
  try {
   String[] _path = filePath.split(XG);
   String oriPath = _path[0] + XG;
   int len = _path.length;
   if (filePath.lastIndexOf(".") > 0) {
    len = _path.length - 1;
   }
   for (int i = 1; i < len; i++) {
    File file = new File(oriPath + _path[i]);
    if (!file.exists() || !file.isDirectory()) {
     file.mkdir();
    }
    oriPath = oriPath + _path[i] + XG;
   }
   if (filePath.lastIndexOf(".") > 0) {
    File file = new File(filePath);
    file.createNewFile();
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

}
 

你可能感兴趣的:(exception,String,File,buffer,Path,import)