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)  編輯  收藏

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


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 祁阳县| 彭山县| 滨海县| 临邑县| 临沧市| 延安市| 中西区| 浦东新区| 运城市| 天祝| 苏尼特右旗| 巴东县| 泰安市| 菏泽市| 红桥区| 象州县| 木兰县| 洛南县| 开化县| 马龙县| 玉田县| 建湖县| 五家渠市| 东山县| 永平县| 秀山| 乌拉特后旗| 保靖县| 噶尔县| 启东市| 黑河市| 三门县| 海宁市| 砀山县| 修水县| 宣化县| 霍州市| 砚山县| 信阳市| 灌云县| 明星|