ITeye博客

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

flycun2 · 2013-07-10 17:02 · java · 编程语言 · Markdown

一个样例代码:


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();
		}
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。