java ZipOutputStream

实现代码如下:(在当前包文件夹下新建一个文本文件:123.txt,将其压缩到test.zip)

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zipstream {
 public static void main(String[] args) throws Exception{
  FileOutputStream f=new FileOutputStream("text.zip");
  CheckedOutputStream csum=new CheckedOutputStream(f,new Adler32());
  ZipOutputStream zos=new ZipOutputStream(csum);
  BufferedOutputStream out = new BufferedOutputStream(zos);
  zos.setComment("A test of Java Ziping!");
  
  BufferedReader in =new BufferedReader(new FileReader("123.txt"));
  zos.putNextEntry(new ZipEntry("123.txt"));
  int c;
  while((c = in.read()) != -1){
   out.write(c); 
  }
  in.close();
  out.flush();
  out.close();
 }
}

你可能感兴趣的:(java ZipOutputStream)