zoukankan      html  css  js  c++  java
  • Java基础--压缩和解压缩gz包

    gz是Linux和OSX中常见的压缩文件格式,下面是用java压缩和解压缩gz包的例子

     1 public class GZIPcompress {
     2 
     3     public static void FileCompress(String file, String outgz) throws IOException {
     4         BufferedReader br = new BufferedReader(new FileReader(file));
     5         BufferedOutputStream bs = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(outgz)));
     6 
     7         int c;
     8         while ((c = br.read()) != -1) {
     9             bs.write(c);
    10         }
    11         br.close();
    12         bs.close();
    13     }
    14 
    15     public static String FileUnCompress(String filegz) throws IOException {
    16         BufferedReader bf = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(filegz))));
    17         String s;
    18         StringBuffer sb = new StringBuffer();
    19         while ((s = bf.readLine()) != null) {
    20             sb.append(s);
    21         }
    22         bf.close();
    23         return sb.toString();
    24     }
    25 
    26     public static void main(String[] args) throws IOException {
    27         String fileOut = "test.gz";
    28         String in = "test.txt";
    29         
    30         FileCompress(in, fileOut);
    31         String out = FileUnCompress(fileOut);
    32         
    33         System.out.println(out);
    34     }
    35 
    36 }
  • 相关阅读:
    HDU 1247
    [转载]亲密接触VC6.0编译器
    [转载]你该学什么程序语言
    ACE学习2009116
    新东方英语学习二
    电脑族吃什么比较好
    爱默生生活的准则
    成大事必备9种能力9种手段9种心态
    [转载]句柄和指针
    关于WM_CREATE消息
  • 原文地址:https://www.cnblogs.com/luangeng/p/5778379.html
Copyright © 2011-2022 走看看