一切皆可抽象

          大而無形 庖丁解牛 厚積薄發 滌慮玄覽
             ::  ::  ::  ::  :: 管理

          【原創】java寫的壓縮、解壓的代碼

          Posted on 2005-10-28 15:04 鋒出磨礪 閱讀(904) 評論(2)  編輯  收藏 所屬分類: java算法

          import java.io.*;
          import java.util.*;
          import java.util.zip.ZipOutputStream;
          import java.util.zip.ZipEntry;
          import java.util.zip.ZipFile;

          public class FileMgr {
            public FileMgr() {
            }
           /**
             * 壓縮文件
             * @param srcfile File[]  需要壓縮的文件列表
             * @param zipfile File    壓縮后的文件
             */
            public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
              byte[] buf = new byte[1024];
              try {
                // Create the ZIP file
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
                // Compress the files
                for (int i = 0; i < srcfile.length; i++) {
                  FileInputStream in = new FileInputStream(srcfile[i]);
                  // Add ZIP entry to output stream.
                  out.putNextEntry(new ZipEntry(srcfile[i].getName()));
                  // Transfer bytes from the file to the ZIP file
                  int len;
                  while ( (len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                  }
                  // Complete the entry
                  out.closeEntry();
                  in.close();
                }
                // Complete the ZIP file
                out.close();
              }
              catch (IOException e) {
                e.printStackTrace();
              }
            }

            /**
             * 解壓縮
             * @param zipfile File 需要解壓縮的文件
             * @param descDir String  解壓后的目標目錄
             */
            public static void UnZipFiles(java.io.File zipfile, String descDir) {
              try {
                // Open the ZIP file
                ZipFile zf = new ZipFile(zipfile);
                for (Enumeration entries = zf.entries(); entries.hasMoreElements(); ) {
                  // Get the entry name
                  ZipEntry entry = ( (ZipEntry) entries.nextElement());
                  String zipEntryName = entry.getName();
                  InputStream in = zf.getInputStream(entry);
                 // System.out.println(zipEntryName);
                  OutputStream out = new FileOutputStream(descDir + zipEntryName);
                  byte[] buf1 = new byte[1024];
                  int len;
                  while ( (len = in.read(buf1)) > 0) {
                    out.write(buf1, 0, len);
                  }
                  // Close the file and stream
                  in.close();
                  out.close();
                }
              }
              catch (IOException e) {
                e.printStackTrace();
              }
            }

          }


          評論

          # re: 【原創】java寫的壓縮、解壓的代碼  回復  更多評論   

          2006-05-16 15:03 by whw
          guanyong ma

          # re: 【原創】java寫的壓縮、解壓的代碼  回復  更多評論   

          2006-05-16 15:04 by whw
          能用嗎?
          主站蜘蛛池模板: 铁岭县| 都匀市| 嘉鱼县| 眉山市| 南川市| 会昌县| 苏尼特右旗| 正宁县| 手游| 阳新县| 德阳市| 沧源| 林甸县| 潮州市| 许昌市| 云阳县| 宁都县| 安达市| 格尔木市| 仪征市| 土默特左旗| 营口市| 宝清县| 临泽县| 随州市| 高雄县| 长沙市| 大埔区| 和平县| 保德县| 宝兴县| 定西市| 孝义市| 荥阳市| 平安县| 滦平县| 鱼台县| 德令哈市| 车险| 沈阳市| 三河市|