小石頭
          Excellence in any department can be attained only by the labor of a lifetime; it is not to be purchased at a lesser price.
          posts - 91,comments - 22,trackbacks - 0
          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          當我們大喊一聲“不!我要這樣走!”時,我們就成熟了。

          常用鏈接

          留言簿(10)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          Blogger's

          Java站點

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          Java壓縮文件/目錄成ZIP包最新技巧

          日期:2005-05-23 ??來源: Java研究組織 ??作者: littletree
            使用java自帶的類 java.util.zip進行文件/目錄的壓縮的話,有一點不足,不支持中文的名件/目錄命名,如果有中文名,那么打包就會失敗。本人經過一段時間的摸索和實踐,發現在一般的Ant.jar類庫中也有zip類,import org.apache.tools.zip.*,能夠解決不支持中文文件目錄的問題,同時,Ant的獲得途徑也比較多,一般的應用服務器中有這個包,實在不行去下載個tomcat5.X,里面也有ant.jar,本人經過測試,可以使用。

            例子如下:

          import java.io.File;
          import org.apache.tools.zip.ZipOutputStream;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import dianda.cwmanage.*;
          import dianda.com.util.Format;

          public class CompressBook {
           public CompressBook() {
          }

          public void zip(String inputFileName) throws Exception {
           String zipFileName="c:\\test.zip";//打包后文件名字
           System.out.println(zipFileName);
           zip(zipFileName, new File(inputFileName));

          }

          private void zip(String zipFileName, File inputFile) throws Exception {
           ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
           zip(out, inputFile, "");
           System.out.println("zip done");
           out.close();
          }

          private void zip(ZipOutputStream out, File f, String base) throws Exception {
           if (f.isDirectory()) {
            File[] fl = f.listFiles();
            out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
            base = base.length() == 0 ? "" : base + "/";
            for (int i = 0; i < fl.length; i++) {
             zip(out, fl[i], base + fl[i].getName());
            }
           }
           else {
            out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            int b;
            System.out.println(base);
            while ( (b = in.read()) != -1) {
             out.write(b);
            }
            in.close();
           }
          }
          }

            唯一遺憾,美中不足的是,無論,java.util.zip或者org.apache.tools.zip都不能解壓rar工具打成的rar包,搜了半天也找不到采用java解壓rar工具打成的包,盼望哪位高人能夠提供解壓rar工具打成的包,和大家分享一下!
          posted on 2007-03-08 08:40 小石頭 閱讀(483) 評論(0)  編輯  收藏 所屬分類: Subversion
          主站蜘蛛池模板: 阳原县| 休宁县| 宁明县| 鹤庆县| 襄垣县| 格尔木市| 洞头县| 丹巴县| 巴林右旗| 荣成市| 新津县| 塔城市| 达日县| 长治市| 乌兰县| 威远县| 中方县| 连江县| 平阳县| 根河市| 科技| 庆城县| 仙桃市| 繁峙县| 邛崃市| 明溪县| 玛沁县| 台前县| 通辽市| 兴安县| 顺义区| 曲阜市| 手机| 望江县| 盐源县| 共和县| 奇台县| 石家庄市| 浦城县| 铜鼓县| 樟树市|