一切皆可抽象

          大而無(wú)形 庖丁解牛 厚積薄發(fā) 滌慮玄覽
             ::  ::  ::  ::  :: 管理

          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  解壓后的目標(biāo)目錄
             */
            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();
              }
            }

          }


          評(píng)論

          # re: 【原創(chuàng)】java寫(xiě)的壓縮、解壓的代碼  回復(fù)  更多評(píng)論   

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

          # re: 【原創(chuàng)】java寫(xiě)的壓縮、解壓的代碼  回復(fù)  更多評(píng)論   

          2006-05-16 15:04 by whw
          能用嗎?
          主站蜘蛛池模板: 天气| 金山区| 环江| 余江县| 江华| 曲松县| 濮阳市| 西乌| 金坛市| 元阳县| 白水县| 库伦旗| 信宜市| 仁怀市| 紫阳县| 关岭| 外汇| 定襄县| 广西| 会宁县| 西畴县| 略阳县| 水富县| 万州区| 泌阳县| 文成县| 永春县| 西青区| 资阳市| 岳阳市| 陵水| 新乡县| 呼玛县| 玉溪市| 固阳县| 西林县| 温泉县| 镇平县| 来宾市| 色达县| 青铜峡市|