ghlat520 发表于 2013-2-7 06:08:23

java常用组件之解压包工具 ZipUtils

/**
*
*/
package cn.ccb.jstsccf.taskmanager.util;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* 文件压缩和解压缩帮助类
*
* @author liuning
*
*/
public class ZipUtils {
public static final int BUF = 2048;
/**
* 把某一个目录下的文件压缩成zip文件
*
* @param fileDir
* @param zipName
* @throws IOException
*/
public static void zip(String fileDir, String zipName) throws IOException {
ZipOutputStream zos = null;
try {
   zos = new ZipOutputStream(new FileOutputStream(zipName));
   File folder = new File(fileDir);
   zip(folder, zos, "");
} catch (FileNotFoundException e) {
   throw e;
} catch (IOException e) {
   throw e;
} finally {
   if (zos != null) {
    zos.close();
   }
}
}
/**
* 把某一目录下某些文件压缩成zip文件
*
* @param fileDir
* @param fileNameList
* @param zipName
* @throws IOException
*/
public static void zip(String fileDir, List fileNameList, String zipName)
   throws IOException {
ZipOutputStream zos = null;
try {
   zos = new ZipOutputStream(new FileOutputStream(zipName));
   for (Iterator iter = fileNameList.iterator(); iter.hasNext();) {
    String fileName = (String) iter.next();
    File file = new File(fileDir + "/" + fileName);
    zip(file, zos, "");
   }
} catch (FileNotFoundException e) {
   throw e;
} catch (IOException e) {
   throw e;
} finally {
   if (zos != null) {
    zos.close();
   }
}
}

/**
* 把某些文件压缩成zip文件
*
* @param filePathList
* @param zipName
* @throws IOException
*/
public static void zip(List filePathList, String zipName)
   throws IOException {
ZipOutputStream zos = null;
try {
   zos = new ZipOutputStream(new FileOutputStream(zipName));
   for (Iterator iter = filePathList.iterator(); iter.hasNext();) {
    String filePath = (String) iter.next();
    File file = new File(filePath);
    zip(file, zos, "");
   }
} catch (FileNotFoundException e) {
   throw e;
} catch (IOException e) {
   throw e;
} finally {
   if (zos != null) {
    zos.close();
   }
}
}

/**
* 把某一文件添加到压缩zip包中
*
* @param file
* @param zos
* @param dir
* @throws IOException
*/
private static void zip(File file, ZipOutputStream zos, String dir)
   throws IOException {
dir = dir + (dir.length() == 0 ? "" : "/") + file.getName();
// 如果是目录
if (file.isDirectory()) {
   zos.putNextEntry(new ZipEntry(dir + "/"));
   File[] listFiles = file.listFiles();
   for (int i = 0; i < listFiles.length; i++) {
    zip(listFiles, zos, dir);
   }
} else {
   FileInputStream fis = null;
   try {
    zos.putNextEntry(new ZipEntry(dir));
    fis = new FileInputStream(file);
    byte[] buf = new byte;
    int len = -1;
    while ((len = fis.read(buf)) > 0) {
   zos.write(buf, 0, len);
    }
   } catch (FileNotFoundException e) {
    throw e;
   } catch (IOException e) {
    throw e;
   } finally {
    if (fis != null) {
   fis.close();
    }
   }
}
}
/**
* 从一个zip压缩文件压缩到另外一个压缩文件
*
* @param file
* @param descZip
* @throws IOException
*/
public static void zipToZip(InputStream is, String descZip)
   throws IOException {
ZipInputStream zis = null;
ZipOutputStream zos = null;
try {
   zis = new ZipInputStream(is);
   zos = new ZipOutputStream(new FileOutputStream(descZip));
   ZipEntry entry = zis.getNextEntry();
   while (entry != null) {
    zos.putNextEntry(entry);
    byte[] buf = new byte;
    int len = -1;
    while ((len = zis.read(buf)) > 0) {
   zos.write(buf, 0, len);
    }
    entry = zis.getNextEntry();
   }
} catch (FileNotFoundException e) {
   throw e;
} catch (IOException e) {
   throw e;
} finally {
   if (zis != null) {
    try {
   zis.close();
    } catch (IOException e) {
   throw e;
    }
   }
   if (zos != null) {
    try {
   zos.close();
    } catch (IOException e) {
   throw e;
    }
   }
}
}
/**
* zip文件解压
*
* @param uploadFile
* @param unZipdir
* @throws IOException
*/
public static List unZip(InputStream is, String unZipdir)
   throws IOException {
ZipInputStream zis = null;
List fileNames = new ArrayList();

try {
   zis = new ZipInputStream(is);
   ZipEntry entry = zis.getNextEntry();
   File unZipDir = new File(unZipdir);
   if (!unZipDir.exists()) {
    unZipDir.mkdir();
   }
   while (entry != null) {
    // modify by caoyanbao 2010-05-31 给上传的文件加上时间前缀
    String fileName = fileDateFormat() + "_" + entry.getName();
    fileNames.add(fileName);
    String childFile = unZipdir + "/" + fileName;
    File file = new File(childFile);
    if (entry.isDirectory()) {
   file.mkdir();
    } else {
   FileOutputStream fos = null;
   try {
      fos = new FileOutputStream(file);
      byte[] buf = new byte;
      int len = -1;
      while ((len = zis.read(buf)) > 0) {
       fos.write(buf, 0, len);
      }
   } catch (FileNotFoundException e) {
      fileNames = null;
      throw e;
   } catch (IOException e) {
      fileNames = null;
      throw e;
   } finally {
      if (fos != null) {
       try {
      fos.close();
       } catch (IOException e) {
      fileNames = null;
      throw e;
       }
      }
   }
    }
    entry = zis.getNextEntry();
   }
   return fileNames;
} catch (FileNotFoundException e) {
   fileNames = null;
   throw e;
} catch (IOException e) {
   fileNames = null;
   throw e;
} finally {
   if (zis != null) {
    try {
   zis.close();
    } catch (IOException e) {
   fileNames = null;
   throw e;
    }
   }
}
}
/**
* 把zip文件写入输出流
*
* @param errorZipName
* @param response
* @throws IOException
* @throws FileNotFoundException
*/
public static void readToStream(String errorZipName, OutputStream ops)
   throws IOException {
DataOutputStream dos = null;
DataInputStream dis = null;
try {
   dos = new DataOutputStream(ops);
   dis = new DataInputStream(new FileInputStream(errorZipName));
   int count = -1;
   byte[] data = new byte;
   while ((count = dis.read(data)) != -1) {
    dos.write(data, 0, count);
   }
} catch (FileNotFoundException e) {
   throw e;
} catch (IOException e) {
   throw e;
} finally {
   if (dis != null) {
    dis.close();
   }
}
}
/**
* 写入上传的文件
*
* @param file
* @param filePath
* @throws IOException
*/
public static void writeFile(InputStream is, String filePath)
   throws IOException {
DataOutputStream writer = null;
try {
   DataInputStream reader = new DataInputStream(is);
   // 将文件写入目录
   writer = new DataOutputStream(new FileOutputStream(filePath));
   byte[] data = new byte;
   int len = -1;
   while ((len = reader.read(data)) != -1) {
    writer.write(data, 0, len);
   }
} catch (FileNotFoundException e) {
   throw e;
} catch (IOException e) {
   throw e;
} finally {
   if (writer != null) {
    writer.flush();
    writer.close();
   }
}
}
/**
* 判断文件是否为ZIP文件
*
* @param file
* @return
*/
public static boolean isZipFile(String fileName) {
String name = fileName.substring(fileName.lastIndexOf(".") + 1);
if (name.equalsIgnoreCase("zip")) {
   return true;
}
return false;
}
/**
* 判断文件是否为excel文件
*
* @param file
* @return
*/
public static boolean isExcelFile(String fileName) {
String name = fileName.substring(fileName.lastIndexOf(".") + 1);
if (name.equalsIgnoreCase("xls")) {
   return true;
}
return false;
}
/**
* 判断文件是否为Rar文件
*
* @param file
* @return
*/
public static boolean isRarFile(String fileName) {
String name = fileName.substring(fileName.lastIndexOf(".") + 1);
if (name.equalsIgnoreCase("rar")) {
   return true;
}
return false;
}
/**
* 格式化的时分秒时间
*
* @return
*/
public static String fileDateFormat() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
return sdf.format(date);
}
public static void main(String[] args) throws IOException {
zip("", new ArrayList(), "c:/sss.zip");
}
}
页: [1]
查看完整版本: java常用组件之解压包工具 ZipUtils