java实例2 压缩文件

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


public class ZipCompress {
  public static void mian(String[] args){
	  try{
		 FileOutputStream fileOut=new FileOutputStream("Example.zip");//建立输出文件流
		 //建立冗余验证流
		 CheckedOutputStream checkedOut=new CheckedOutputStream(fileOut, new CRC32());
		 ZipOutputStream zipOut=new ZipOutputStream(checkedOut);//建立zip流
		    new ZipOutputStream(
		    		new BufferedOutputStream(checkedOut));
		    zipOut.setComment("This is a java zip test file");
		    String fileName="D:/ZipCompress .java/"+"ZipCompress.java";
		    //读取压缩文件
		   BufferedReader in=new BufferedReader(new FileReader(fileName));
		   zipOut.putNextEntry(new ZipEntry(fileName));//建立压缩实体
		   int ch;
		   //当压缩文件未结束的时候继续写
		  while((ch=in.read())!=-1){
			  zipOut.write(ch);
			  
			  
		  }
		  in.close();
		  zipOut.close();
		   
		 
	  }catch (Exception e) {
		System.out.println(e.toString());
	}
	  
  }
}
 

你可能感兴趣的:(java)