posts - 241,  comments - 116,  trackbacks - 0
          示例用的是org.apache.tools.zip包下面的一些類,jdk自帶的zip工具類當文件名是中文的情況下會出現問題,本事例可以遞歸壓縮文件和解壓文件,功能上和現在常用的一些壓縮軟件功能類似,暫時沒有做性能上的對比。
          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 墻頭草 閱讀(1471) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 潼关县| 崇州市| 葫芦岛市| 孝义市| 遂平县| 聂拉木县| 镇巴县| 宁城县| 平江县| 丰顺县| 鹿邑县| 景德镇市| 河南省| 龙岩市| 宁夏| 苗栗县| 皮山县| 安陆市| 阳城县| 吴旗县| 穆棱市| 崇信县| 江西省| 会理县| 绥宁县| 纳雍县| 五台县| 德州市| 富源县| 尉犁县| 彩票| 大庆市| 北安市| 高青县| 淄博市| 桦川县| 阿克陶县| 琼结县| 大足县| 治县。| 平和县|