运用JDK中 ZipOutputStream类实现文件的压缩功能

一个样例代码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipOutputStreamExample {
	public static void main(String[] args) {
		byte[] buffer = new byte[1024];

		try {
			FileOutputStream fos = new FileOutputStream("log.zip");
			ZipOutputStream zos = new ZipOutputStream(fos);
			ZipEntry ze = new ZipEntry("weblog-20130710.log");
			zos.putNextEntry(ze);
			FileInputStream in = new FileInputStream("weblog.log");//要压缩的文本文件weblog.log
			int len;
			while ((len = in.read(buffer)) > 0) {
				zos.write(buffer, 0, len);
			}
			in.close();
			zos.closeEntry();
			zos.close();
			System.out.println("end");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}

你可能感兴趣的:(java)