`
flycun2
  • 浏览: 26830 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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

    博客分类:
  • java
阅读更多
一个样例代码:
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
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics