compress.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 54 行

JAVA
54
字号
package cn.js.fan.util.file;import java.io.*;import java.util.zip.*;import cn.js.fan.util.*;public class Compress {  public static void gzipFile(String from, String to) throws IOException {    FileInputStream in = new FileInputStream(from);    ZipEntry entry = new ZipEntry(from);    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(to));    out.putNextEntry(entry);    byte[] buffer = new byte[4096];    int bytes_read;    while ( (bytes_read = in.read(buffer)) != -1)      out.write(buffer, 0, bytes_read);    in.close();    out.close();  }  public static void zipDirectory(String dir, String zipfile) throws      IOException, IllegalArgumentException {    ZipOutputStream out =        new ZipOutputStream(new FileOutputStream(zipfile));    zipDir(out, dir);    out.close();  }  public static void zipDir(ZipOutputStream out, String dir) throws IOException,      IllegalArgumentException {    File d = new File(dir);    if (!d.isDirectory()) {      byte[] buffer = new byte[4096];      int bytes_read;      FileInputStream in = new FileInputStream(d);      ZipEntry entry = new ZipEntry(StrUtil.GBToUnicode(dir));      out.putNextEntry(entry);      while ( (bytes_read = in.read(buffer)) != -1)        out.write(buffer, 0, bytes_read);            in.close();      return;    }        String[] entries = d.list();    String path = d.getPath();    for (int i = 0; i < entries.length; i++) {      zipDir(out, path + "/" + entries[i]);    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?