GZIPOutputStream GZIPInputStream

2018/03/06 17:05
阅读数 14

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

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部