一切皆可抽象

          大而無(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寫的壓縮、解壓的代碼  回復(fù)  更多評(píng)論   

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

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

          2006-05-16 15:04 by whw
          能用嗎?
          主站蜘蛛池模板: 灵台县| 石城县| 名山县| 墨玉县| 务川| 灌南县| 大关县| 井陉县| 钟祥市| 桂平市| 策勒县| 肃南| 舞钢市| 徐水县| 曲麻莱县| 海林市| 永丰县| 衢州市| 清远市| 胶南市| 秭归县| 渝中区| 米脂县| 广丰县| 高碑店市| 梁河县| 宣城市| 临朐县| 青河县| 涟源市| 凌海市| 临沧市| 大同市| 桂东县| 马公市| 奈曼旗| 河源市| 金堂县| 孟村| 内丘县| 大厂|