Java/File Input Output/DeflaterOutputStream — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:05, 1 июня 2010

Create a compressed data file by using a DeflaterOutputStream and then read that data through an InflaterInputStream.

 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    double data[] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
    DataOutputStream fout = new DataOutputStream(new DeflaterOutputStream(new FileOutputStream(
        "data.dat")));
    fout.writeInt(data.length);
    for (double d : data)
      fout.writeDouble(d);
    DataInputStream  fin = new DataInputStream(new InflaterInputStream(new FileInputStream(
        "data.dat")));
    int num = fin.readInt();
    double avg = 0.0;
    double d;
    for (int i = 0; i < num; i++) {
      d = fin.readDouble();
      avg += d;
      System.out.print(d + " ");
    }
    fin.close();
    fout.close();
  }
}





Use DeflaterOutputStream to zip

 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.DeflaterOutputStream;
public class FileDeflater {
  public static void main(String[] args) throws Exception {
    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");
    DeflaterOutputStream dos = new DeflaterOutputStream(fout);
    for (int c = fin.read(); c != -1; c = fin.read()) {
      dos.write(c);
    }
    dos.close();
    fin.close();
  }
}