next up previous
Next: 3.6 Java アーカイブ Up: 3 ファイルの取り扱い Previous: 3.4 ファイルの読み込み書き出し

3.5 圧縮例

Javaには,圧縮用のユーティリティがたくさん用意されていて, そのためのストリームが定義されている.
//: GZIPcompress.java
// Copyright (c) Bruce Eckel, 1998
// Uses Java 1.1 GZIP compression to compress
// a file whose name is passed on the command
// line.
import java.io.*;
import java.util.zip.*;

public class GZIPcompress {
  public static void main(String[] args) {
    try {
      BufferedReader in =
        new BufferedReader(
          new FileReader(args[0]));
      BufferedOutputStream out =
        new BufferedOutputStream(
          new GZIPOutputStream(
            new FileOutputStream("test.gz")));
      System.out.println("Writing file");
      int c;
      while((c = in.read()) != -1)
        out.write(c);
      in.close();
      out.close();
      System.out.println("Reading file");
      BufferedReader in2 =
        new BufferedReader(
          new InputStreamReader(
            new GZIPInputStream(
              new FileInputStream("test.gz"))));
      String s;
      while((s = in2.readLine()) != null)
        System.out.println(s);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}


generated through LaTeX2HTML. M.Inaba 平成18年5月7日