zoukankan      html  css  js  c++  java
  • 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 }

     结果 及 文件结构

  • 相关阅读:
    币值转换
    打印沙漏
    秋季学期总结
    在人生道路上对我影响最大一位老师
    自我介绍
    python笔记十五(面向对象及其特性)
    python笔记十四(高阶函数——map/reduce、filter、sorted)
    python笔记十三(高阶函数、装饰器)
    python笔记十二(匿名函数)
    Python笔记十一(迭代器)
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/9157544.html
Copyright © 2011-2022 走看看