一、代码来源:http://www.oracle.com/technetwork/articles/java/compress-1565076.html
二、方法:利用jdk 压缩文件夹
三、不足:压缩后的文件过大,于源文件夹大小无太大差异,期待更好的解决办法
四、代码:
import java.io.*;
import java.util.zip.*;
public class Zip {
?? static final int BUFFER = 2048;
?? static final String path="C:\\Users\\sino\\Downloads\\mb";?????? //要压缩的文件夹目录
?? public static void main (String argv[]) {
????? try {
???????? BufferedInputStream origin = null;
???????? FileOutputStream dest = new? FileOutputStream("C:\\Users\\sino\\Downloads\\mb233.zip");??? //压缩后的文件夹目录
???????? GZIPOutputStream out = new GZIPOutputStream(new? BufferedOutputStream(dest));
???????? // out.setMethod(ZipOutputStream.DEFLATED);
???????? // out.setLevel(9);
???????? byte data[] = new byte[BUFFER];
???????? // get a list of files from current directory
???????? File f = new File(path);
???????? String files[] = f.list();
???????? for (int i=0; i<files.length; i++) {
??????????? System.out.println("Adding: "+files[i]);
??????????? FileInputStream fi = new? FileInputStream(path+File.separator+files[i]);
??????????? origin = new?? BufferedInputStream(fi, BUFFER);
?????????? // ZipEntry entry = new ZipEntry(files[i]);???? //存放遍历过的文件名称
?????????? // entry.setMethod(ZipOutputStream.DEFLATED);
?????????? // out.putNextEntry(entry);
??????????? int count;
??????????? while((count = origin.read(data, 0,
????????????? BUFFER)) != -1) {
?????????????? out.write(data, 0, count);
??????????? }
??????????? origin.close();
???????? }
???????? out.close();
????? } catch(Exception e) {
???????? e.printStackTrace();
????? }
?? }
}