首页 > WORK > zip文件的解压代码

zip文件的解压代码

版权声明:本文可以任意转载,转载时请务必以超链接形式标明文章原始出处作者署名本版权声明

用于zip文件的解压,原来不能解深层目录,现在可以了,用绝对路径的方法,但感觉还不是很好。
代码中使用的MyZipInputStream和MyZipEntry类改写了原来java.util.zip包中的ZipInputStream和ZipEntry类,主要用于中文文件名的处理,具体代码不列出了。

//…省略
public static void main(String[] args) {
    File infile = new File("myZip.zip");
    try {
      MyZipInputStream in = new MyZipInputStream(new FileInputStream(infile));
      MyZipEntry file = in.getNextEntry();

      int i = infile.getAbsolutePath().lastIndexOf(’.');
      String dirname = new String();

      if (i != -1) {
        dirname = infile.getAbsolutePath().substring(0, i);
      }
      else {
        dirname = infile.getAbsolutePath();
      }

      File newdir = new File(dirname);
      newdir.mkdir();

      byte[] c = new byte[1024];
      int len;
      int slen;

      while (file != null) {
        //对于目录
        i = file.getName().replace(’/', ‘\\’).lastIndexOf(’\\’);
        if (i != -1) {
          File dirs = new File(dirname + File.separator +
                              file.getName().replace(’/',’\\’).
                              substring(0, i));
          dirs.mkdir();
          dirs = null;
        }
        System.out.print("正在解压:" + file.getName());

        //对于文件
        if (!file.isDirectory()) {
          FileOutputStream out = new FileOutputStream(dirname + File.separator +
                    file.getName().replace(’/', ‘\\’));
          while ( (slen = in.read(c, 0, c.length)) != -1) {
            out.write(c, 0, slen);
        }
        out.close();
        }
        System.out.println("完成。");
        file = in.getNextEntry();
      }
      in.close();
    }
    catch (Exception ex) {
      System.out.println(ex);
    }
}
分类: WORK 标签:
  1. 匿名用户
    2005年5月19日21:15 | #1

    good。

  2. 匿名用户
    2005年5月24日12:00 | #2

    有没有MyZipInputStream的代码呢