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

上一篇:Flask从入门到精通之MySQL数据库操作


下一篇:Docker for Windows 使用 VMware WorkStation