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 }

     结果 及 文件结构

  • 相关阅读:
    【HDOJ】1058 Humble Numbers
    activity去标题栏操作&保留高版本主题
    谷歌安卓官方开发者网站 https://developer.android.google.cn
    TortoiseGIT
    Git的优势
    eoe开发社区
    安卓巴士 http://www.apkbus.com/
    Git简介
    SVN标准目录结构
    关于人生的
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/9157544.html
Copyright © 2011-2022 走看看