posts - 241,  comments - 116,  trackbacks - 0
          示例用的是org.apache.tools.zip包下面的一些類(lèi),jdk自帶的zip工具類(lèi)當(dāng)文件名是中文的情況下會(huì)出現(xiàn)問(wèn)題,本事例可以遞歸壓縮文件和解壓文件,功能上和現(xiàn)在常用的一些壓縮軟件功能類(lèi)似,暫時(shí)沒(méi)有做性能上的對(duì)比。
          package zip;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.util.Enumeration;

          import org.apache.tools.zip.ZipEntry;
          import org.apache.tools.zip.ZipFile;
          import org.apache.tools.zip.ZipOutputStream; javascript table 表格排序



          @SuppressWarnings("rawtypes")
          public class TestAntZip {
              public static void main(String[] args) {
                  //Unzip();
                  //ZipFile();
              }

              /**
               * 壓縮文件或者文件夾
               */
              private static void ZipFile() {
                  try {
                      String inputStr = "D:/testzip";
                      File input = new File(inputStr);
                                  String singleFile = null;
                      if (input.isFile()) {
                        int temp = input.getName().lastIndexOf(".");
                            singleFile = input.getName().substring(0, temp);
                      }
                      String basepath = input.getName();
                                  String zipfilename = singlefile != null ? singlefile : input.getName();
                      ZipOutputStream zos = new ZipOutputStream(new File(input.getParent(), zipfilename + ".zip"));
                      zos.setEncoding("UTF-8");
                      PutEntry(zos, input, basepath);
                      zos.close();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }

              private static void PutEntry(ZipOutputStream zos, File file, String basepath) throws IOException, FileNotFoundException {
                  FileInputStream is = null;
                  byte[] b = new byte[1024 * 10];
                  int tmp = 0;
                  basepath += File.separator;
                  if (file.isFile()) {
                      zos.putNextEntry(new ZipEntry(basepath));
                      is = new FileInputStream(file);
                      while ((tmp = is.read(b, 0, b.length)) != -1) {
                          zos.write(b, 0, tmp);
                      }
                      is.close();
                  } else {
                      for (File f : file.listFiles()) {
                          PutEntry(zos, f, basepath + f.getName());
                      }
                  }
                  zos.flush();
              }

              /**
               * 解壓縮
               */
              private static void Unzip() {
                  String file = "d:/testzip.zip";
                  File zipFile = new File(file);
                  int i = file.lastIndexOf(".");
                  File filePath = new File(file.substring(0, i));
                  if (!filePath.exists()) {
                      filePath.mkdirs();
                  }
                  File temp = null;
                  try {
                      ZipFile zip = new ZipFile(zipFile, "UTF-8");
                      ZipEntry entry = null;
                      byte[] b = new byte[1024 * 10];
                      int tmp = 0;
                      
                      FileOutputStream os = null;
                      InputStream is = null;
                      for (Enumeration enu = zip.getEntries(); enu.hasMoreElements();) {
                          entry = (ZipEntry) enu.nextElement();
                          is = zip.getInputStream(entry);
                          temp = new File(filePath.getParent() + File.separator + entry.getName());
                          if (!temp.getParentFile().exists()) {
                              temp.getParentFile().mkdirs();
                          }
                          os = new FileOutputStream(temp);
                          while ((tmp = is.read(b, 0, b.length)) != -1) {
                              os.write(b, 0, tmp);
                          }
                          os.flush();
                          os.close();
                          is.close();
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }
          posted on 2011-08-04 09:29 墻頭草 閱讀(1472) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          人人游戲網(wǎng) 軟件開(kāi)發(fā)網(wǎng) 貨運(yùn)專(zhuān)家
          主站蜘蛛池模板: 德令哈市| 台中市| 南漳县| 乌鲁木齐市| 公主岭市| 刚察县| 商城县| 且末县| 巴林左旗| 南投县| 尉氏县| 新丰县| 蓬莱市| 阳新县| 娄底市| 万安县| 寿阳县| 嘉峪关市| 东方市| 龙游县| 治多县| 莱西市| 高清| 安多县| 凤山县| 保康县| 崇文区| 乳山市| 神木县| 永修县| 平塘县| 马公市| 塘沽区| 额济纳旗| 威海市| 邻水| 大渡口区| 依兰县| 孟连| 喀什市| 行唐县|