FileCompression[zip].txt
上传用户:sbftbdw
上传日期:2022-01-12
资源大小:1k
文件大小:1k
源码类别:

压缩解压

开发平台:

Java

  1. /**
  2.   * 把一个文件压缩成zip格式 入口格式: File src = new File("d:/temp/1.txt"); String des
  3.   * ="d:/temp/www.zip";
  4.   * 
  5.   * @param src
  6.   * @param des
  7.   */
  8.  public void compress(File src, String des) {
  9.   DataInputStream in = null;
  10.   ZipOutputStream out = null;
  11.   try {
  12.    in = new DataInputStream(new FileInputStream(src));
  13.    out = new ZipOutputStream(new FileOutputStream(des));
  14.    out.putNextEntry(new ZipEntry(src.getName()));
  15.    int n = 0;
  16.    byte[] buff = new byte[1024 * 1024];
  17.    while ((n = in.read(buff)) != -1) {
  18.     out.write(buff, 0, n);
  19.    }
  20.   } catch (Exception e) {
  21.    // TODO Auto-generated catch block
  22.    e.printStackTrace();
  23.   } finally {
  24.    try {
  25.     out.close();
  26.     in.close();
  27.    } catch (IOException e) {
  28.     // TODO Auto-generated catch block
  29.     e.printStackTrace();
  30.    }
  31.   }
  32.  }