GZIPOutputStream GZIPInputStream

GZIP is appropriate for single data stream.

Example: Compress one file

 

public class Demo8 {
 
 public static void main(String[] args) throws Exception {
  //read a file
  FileReader fr = new FileReader("C:/Users/caich5/Desktop/aaaa.txt");
  
        BufferedReader in = new BufferedReader(fr);
       
        //starting to compress
        FileOutputStream fos = new FileOutputStream("C:/Users/caich5/Desktop/aaaa.gz");
        GZIPOutputStream gzipout = new GZIPOutputStream(fos);
        BufferedOutputStream bos = new BufferedOutputStream(gzipout);
       

        int c;
        while((c = in.read())!= -1){
         bos.write(c);
        }
        in.close();
        bos.close();
        
        //reading  gzip file 
        FileInputStream fis = new FileInputStream("C:/Users/caich5/Desktop/aaaa.gz");
        GZIPInputStream gzipin = new GZIPInputStream(fis);
        InputStreamReader isr = new InputStreamReader(gzipin);
        BufferedReader in2 = new BufferedReader(isr);
       
        String s;
        while((s = in2.readLine())!= null){
         System.out.println(s);
         
        }
 }
}

posted @ 2018-03-06 17:05  Chris,Cai  阅读(2223)  评论(0编辑  收藏  举报