java 编程思想 GZip(Zip) 格式压缩解压缩
GZIPOutputStream 只能支持 OutputStream对象,不能接受Writer 对象,会中文乱码
GZIPInputStream 支持 Reader字符流
1 package io; 2 3 import java.io.BufferedOutputStream; 4 import java.io.BufferedReader; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.FileReader; 9 import java.io.IOException; 10 import java.io.InputStreamReader; 11 import java.util.zip.GZIPInputStream; 12 import java.util.zip.GZIPOutputStream; 13 14 public class GZipcompress { 15 16 17 public static void main(String[] args) throws IOException { 18 String fileName = "src\\io\\GZipcompress.java"; 19 BufferedReader in = new BufferedReader(new FileReader(fileName) ); 20 21 BufferedOutputStream out = new BufferedOutputStream( 22 new GZIPOutputStream( 23 new FileOutputStream("test.gz")) 24 ); 25 26 System.out.println("write file"); 27 int c ; 28 29 while ((c = in.read()) != -1) { 30 out.write(c); 31 }//GZIPOutputStream只支持字节对象,一个一个写(中文乱码) 32 33 in.close(); 34 out.close(); 35 36 //-------------------read-------------- 37 System.out.println("读取文件 file "); 38 39 BufferedReader in2 = new BufferedReader( 40 new InputStreamReader(new GZIPInputStream( 41 new FileInputStream("test.gz")))); 42 // 支持字符对象 43 String s ; 44 while ( (s = in2.readLine())!= null ) { 45 System.out.println(s); 46 } 47 48 49 50 } 51 52 53 54 55 56 }
结果 及 文件结构
posted on 2018-06-08 20:53 wait_for_you 阅读(453) 评论(0) 编辑 收藏 举报