Thking In Java

          關注應用程序的HA、可擴展性 多實踐,遇到問題查doc,google,上論壇咨詢

           

          [導入]利用JAVA打包生成zip文件的類

          主要代碼參考了beansoft的ZipUtil,添加了一個方法,
          可以把不同目錄文件添加到zip中,并做了一些改進,使用了apache的zip類,(原為jdk的zip類),使支持中文文件名。
          apache的zip類可以從ant中抽取。apache好像沒有提供單獨下載。
           package com.order;

          import java.io.*;
          import java.util.List;
          import org.apache.tools.zip.*;

          /**
           * 把多個文件打包到一個文件
           * @author xiaofei.hu
           *
           */
          public class ZipUtil {

              public static void toZip(File[] files, File zipFile){
                  
              }


              /**
               * The buffer.
              */
                  protected static byte buf[] = new byte[1024];
                  
                  
                  /**
                   * 遍歷目錄并添加文件.
                   * @param jos - JAR 輸出流
                   * @param file - 目錄文件名
                   * @param pathName - ZIP中的目錄名
                   * @throws IOException
                   * @throws FileNotFoundException
                   */
                  private static void recurseFiles(ZipOutputStream jos, File file, String pathName)
                      throws IOException, FileNotFoundException
                  {
                      if (file.isDirectory())
                      {
                          pathName = pathName + file.getName() + "/";
                          jos.putNextEntry(new ZipEntry(pathName));
                          String fileNames[] = file.list();
                          if (fileNames != null)
                          {
                              for (int i = 0; i < fileNames.length; i++)
                                  recurseFiles(jos, new File(file, fileNames[i]), pathName);
                  
                          }
                      } else
                      {
                          ZipEntry jarEntry = new ZipEntry(pathName + file.getName());
          //                System.out.println(pathName + "  " + file.getName());
                          FileInputStream fin = new FileInputStream(file);
                          BufferedInputStream in = new BufferedInputStream(fin);
                          jos.putNextEntry(jarEntry);
                          
                          int len;
                          while ((len = in.read(buf)) >= 0)
                              jos.write(buf, 0, len);
                          in.close();
                          jos.closeEntry();
                      }
                  }
                  
                  public static void toZip(List<File> files, File zipFile, String zipFolderName, int level)
                                  throws IOException, FileNotFoundException
                  {
                      level = checkZipLevel(level);
                      
                      if(zipFolderName == null) {
                          zipFolderName = "";
                      }
                      
                      ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(zipFile));
                      jos.setLevel(level);
                      
                      
                  
                      for (int i = 0; i < files.size(); i++){
                          recurseFiles(jos, files.get(i), files.get(i).getPath());
                      }
                  

                      jos.close();
                      
                  }
                  
                  /**
                   * 創建 ZIP/JAR 文件.
                   * @param directory - 要添加的目錄
                   * @param zipFile - 保存的 ZIP 文件名
                   * @param zipFolderName - ZIP 中的路徑名
                   * @param level - 壓縮級別(0~9)
                   * @throws IOException
                   * @throws FileNotFoundException
                   */
                  public static void makeDirectoryToZip(File directory, File zipFile, String zipFolderName, int level)
                      throws IOException, FileNotFoundException
                  {
                      level = checkZipLevel(level);
                      
                      if(zipFolderName == null) {
                          zipFolderName = "";
                      }
                      
                      ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(zipFile));
                      jos.setLevel(level);
                      
                      String fileNames[] = directory.list();
                      if (fileNames != null)
                      {
                          for (int i = 0; i < fileNames.length; i++)
                              recurseFiles(jos, new File(directory, fileNames[i]), zipFolderName);
                  
                      }
                      jos.close();
                  }
                  
                  /**
                   * 檢查并設置有效的壓縮級別.
                   * @param level - 壓縮級別
                   * @return 有效的壓縮級別或者默認壓縮級別
                   */
                  public static int checkZipLevel(int level)
                  {
                      if(level < 0 || level > 9) level = 7;
                      return level;
                  }
                  
          }


          文章來源:http://huxiaofei590.blog.163.com/blog/static/3259612200711457331

          posted on 2007-12-04 17:07 ThinkInJava 閱讀(1571) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          java

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 资溪县| 涞源县| 宜兰县| 凌源市| 碌曲县| 乾安县| 汝南县| 洛浦县| 肇州县| 垦利县| 临泉县| 保康县| 仙居县| 容城县| 余姚市| 石家庄市| 恭城| 广东省| 杭锦后旗| 保定市| 麻江县| 长泰县| 仁布县| 庐江县| 舟曲县| 寻乌县| 玉树县| 北海市| 平陆县| 丰顺县| 外汇| 象山县| 临颍县| 镇雄县| 陵川县| 乌海市| 锡林浩特市| 柳州市| 美姑县| 南澳县| 信丰县|