java 打包工具类

  |   0 评论   |   1,065 浏览

    java 打包工具类, 需要ant.jar这个jar包具体的看程序实现

    package com.cthq.crm.util;

    import java.io.ByteArrayOutputStream;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.util.Iterator;

    import java.util.List;

    import java.util.Map;

    import org.apache.tools.zip.ZipEntry;

    import org.apache.tools.zip.ZipOutputStream;

    /**

    • Company: 广州正道(集团)有限公司

    • Title: 中国电信集团级CRM系统

    • description:ZIP文件打包

    • @version 1.0

    • @since: Apr 21, 2010 5:43:53 PM

    • @author liwl

    */

    public class ZipUtil {

    private ZipOutputStream zipOut = null;

    private String outZipName ="";

    /**

    • 获取压缩的文件名称

    • @return

    */

    public String getOutZipFileName() {

    return outZipName;

    }

    /**

    • 得到WEB-INFO绝对路径
      • @author liwl Apr 22, 2010 3:30:48 PM

      • getClassPath :

      • @return

      */

      public static String getClassPath(){

    String path = new ZipUtil().getClass().getResource("").getPath().split(“classes”)[0];

    String aba_path = path.replaceAll(“%20”," ");

      return aba_path;
    
    
    } 
    
    
    
    
    
    /**   
    
    
     * 取文件在项目中的绝对路径
    
    
     * @author liwl Apr 22, 2010 3:30:20 PM
    
    
     * getClassPath : 
    
    
     * @param path
    
    
     * @return
    
    
     */
    
    
    public static String getClassPath(String path){
    
    
     path =  Thread.currentThread().getContextClassLoader().getResource(path).getPath();
    

    String aba_path = path.replaceAll(“%20”," ");

     return aba_path;
    

    }

    /**   
    
    
     * 把文件夹打包成RAR,ZIP压缩包,ZIP文件保存到系统缓存目录,返回文件目录
    
    
     * @author liwl Apr 21, 2010 5:41:02 PM
    
    
     * zipForDir : 
    
    
     * @param inputFileName 打包的文件夹路径
    
    
     * @return  ZIP文件保存到系统缓存目录,返回文件目录
    
    
     * @throws Exception
    
    
     */
    
    
    public static String zipForDir(String inputFileName) throws Exception {
    
    
     String zipFileName = getClassPath()+IDGenerator.getUUID()+"excleZip.zip";
    
    
     ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    
    
     out.setEncoding("gbk");
    
    
     zipForDir(out, new File(inputFileName), "");
    
    
     out.flush();
    
    
     out.close();
    
    
     return zipFileName;
    
    
    }
    
    
    
    
    
    
    
    
    
    
    /**   
    
    
     * 把文件夹打包成RAR,ZIP压缩包
    
    
     * @author liwl Apr 21, 2010 5:37:58 PM
    
    
     * zipForDir : 
    
    
     * @param inputFileName  要打包的文件夹路径
    
    
     * @param outFileName  打包好的文件路径
    
    
     * @throws Exception
    
    
     */
    
    
    public static void zipForDir(String inputFileName,String outFileName) throws Exception {
    
    
     String zipFileName = outFileName; //打包后文件名字要以.ZIP结束
    
    
     ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    
    
     out.setEncoding("gbk");
    
    
     zipForDir(out, new File(inputFileName), "");
    
    
     out.flush();
    
    
     out.close();
    

    }

    /**  
    
    
     * 循环写入ZIP文件 
    
    
     * @author liwl Apr 21, 2010 5:43:21 PM
    
    
     * zipForDir : 
    
    
     * @param out
    
    
     * @param f
    
    
     * @param base
    
    
     * @throws Exception
    
    
     */
    
    
    private static void zipForDir(ZipOutputStream out, File f, String base) throws Exception {
    
    
        
    
    
     //验证本文件是否是一个目录
    
    
     if (f.isDirectory()) {
    
    
     //目录中文件路径名的数组
    
    
            File[] fl = f.listFiles();
    
    
            out.putNextEntry(new ZipEntry(base + "/"));
    
    
            base = base.length() == 0 ? "" : base + "/";
    
    
            for (int i = 0; i < fl.length; i++) {
    

    zipForDir(out, fl[i], base + fl[i].getName());

            }
    
    
         }else {
    
    
            out.putNextEntry(new ZipEntry(base));
    
    
            FileInputStream in = new FileInputStream(f);
    
    
            int b;
    
    
            //输出文件
    
    
            while ( (b = in.read()) != -1 ) {
    

    out.write(b);

            }
    
    
            in.close();
    
    
         }
    
    
     }
    

    /start:创建ZIP包文件,循环放文件把JAR包*/

    /**   
    
    
     * 创建并返回一个ZIP输出对象
    
    
     * @author liwl Apr 26, 2010 10:37:24 AM
    
    
     * creatZipObj : 
    
    
     */
    
    
    public void creatZipObj(){
    

    outZipName = getClassPath()+IDGenerator.getUUID()+“excelZip.rar”;

     try {
    

    zipOut = new ZipOutputStream(new FileOutputStream(outZipName));

     zipOut.setEncoding("GBK");
    
    
     }catch (IOException e) {
    
    
     e.printStackTrace();
    
    
     }
    
    
    }
    
    
    
    
    
    /**   
    
    
     * 把文件写到ZIP里面
    
    
     * @author liwl Apr 26, 2010 10:55:34 AM
    
    
     * writeZip : 
    
    
     * @param bytes
    
    
     * @param excelName
    
    
     */
    
    
    public void writeZip(byte[] bytes,String excelName){
    
    
     try {
    

    zipOut.putNextEntry(new ZipEntry(excelName));

    zipOut.write(bytes);

    } catch (IOException e) {

    e.printStackTrace();

    }

    }
    
    
    
    
    
    /**  
    
    
     * 读出这个ZIP包
    
    
     * @author liwl Apr 26, 2010 10:55:29 AM
    
    
     * getZipBytle : 
    
    
     * @return
    
    
     */
    
    
    public byte[] getZipBytle(){
    
    
     //关闭文件
    
    
     closeZip();
    
    
     if(!outZipName.equals("")){
    

    byte[] bytes = getOutZipByteCon(outZipName);

    //删除文件零时文件

    (new File(outZipName)).delete();

    return bytes;

     }
    
    
     return null;
    
    
    }
    
    
    
    
    
    /**   
    
    
     * 关闭
    
    
     * @author liwl Apr 26, 2010 10:56:14 AM
    
    
     * close : 
    
    
     */
    
    
    public void closeZip(){
    
    
     try {
    
    
     if(null != zipOut){
    

    zipOut.flush();

    zipOut.close();

    zipOut = null;

     }
    

    } catch (IOException e) {

    e.printStackTrace();

    }

    }
    
    
    
    
    
    
    
    
    
    
    /end/
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**  
    
    
     * 
    
    
     * @author liwl Apr 22, 2010 10:11:44 AM
    
    
     * zipForListByte : 
    
    
     * @param list (Map)KEY文件名,VALUE文件BYTE[]
    
    
     * @param outFileName 生成ZIP包的名字
    
    
     * @return
    
    
     */
    
    
    /**   
    
    
     * 对多个EXCEL文件打包
    
    
     * @author liwl Apr 27, 2010 1:15:48 PM
    
    
     * zipForListByte : 
    
    
     * @param list  (Map)KEY文件名,VALUE文件(BYTE[]  OR  路径)
    
    
     * @return ZIP文件包,byte[]
    
    
     */
    
    
    public static byte[] zipForListByte(List list){
    

    String outFileName = getClassPath()+IDGenerator.getUUID()+“my.zip”;

     ZipOutputStream out = null;
    
    
     int i = 1;
    

    try {

    out = new ZipOutputStream(new FileOutputStream(outFileName));

    out.setEncoding(“GBK”);

        Iterator iter = list.iterator();
    
    
        while(iter.hasNext()){
    

    Map map = (Map)iter.next();

    byte[] bytes = null;

         if(null!=map){
    

    Object obj = map.get(“excelByte”);

    if(obj instanceof String){

    bytes = getOutZipByteCon(map.get(“excelByte”).toString());

         //删除文件
    

    delFile(map.get(“excelByte”).toString());

         }
    

    if(obj instanceof byte[]){

    bytes = (byte[]) map.get(“excelByte”);

         }
    

    String excelName = (String)map.get(“excelName”);

         //设置文件名称
    

    out.putNextEntry(new ZipEntry(i+excelName));

    out.write(bytes);

    i++;

         }
    
    
         
    
    
        }
    

    } catch (IOException e) {

    e.printStackTrace();

    }finally{

    //关闭流

    try {

    out.flush();

    out.close();

    out = null;

    } catch (IOException e1) {

    e1.printStackTrace();

    }

    }

    //输出文件

    byte[] bytes = getOutZipByteCon(outFileName);

    //删除文件

    delFile(outFileName);

    return bytes;

    }
    
    
    
    
    
    
    
    
    /**   
    
    
     * 删除文件
    
    
     * @author liwl Apr 27, 2010 1:14:44 PM
    
    
     * delFile : 
    
    
     * @param fileName
    
    
     */
    
    
    public static void delFile(String fileName){
    
    
     //删除文件
    

    File file = new File(fileName);

    if (file.exists() && file.isFile()) {

    file.delete();

    }

    }
    

    /**

    • 获取excel数据的二进制数组

    • @author liwl Apr 22, 2010 3:30:01 PM

    • getOutZipByteCon :

    • @param excelFile

    • @return

    */

    public static byte[] getOutZipByteCon(String excelFile){

    byte[] attachBytes = null;

    File file = new File(excelFile);

    try {

    FileInputStream inputStream = new FileInputStream(file);

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    int cb = 0;

    while ((cb = inputStream.read()) != -1) {

    outStream.write(cb);

    }

    inputStream.close();

    attachBytes = outStream.toByteArray();

    outStream.close();

    return attachBytes;

    } catch (Exception e) {

    }

    return null;

    }

     public static void main(String [] temp){
    

    //
    ZipUtil dd = new ZipUtil();

    //
    List list = new ArrayList();

    //
    Map map = new HashMap();

    // map.put(“excelName”, “在在.xls”);

    //
    map.put(“excelByte”, dd.getOutZipByteCon(“D:\33.xls”));

    // list.add(map);

    // try {

    //
    dd.zipForListByte(list, “d:\test111.rar”);//你要压缩的文件夹

    //
    dd.zipForDir(“D:\logs”);

    // }catch (Exception ex) {

    // ex.printStackTrace();

    // }

    //System.out.println(ZipUtil.getClassPath());

    // System.out.println(Thread.currentThread().getContextClassLoader().getResource(ZipUtil.class.getName()).getPath());

    System.out.println(getClassPath()+DateUtils.getDateByNowYYMMDDTT()+“excleZip.zip”);

     }
    

    }

    评论

    发表评论

    validate