Java Tutorial/File/DeflaterOutputStream

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

Create DeflaterOutputStream from FileOutputStream

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
public class MainClass {
  public final static String DEFLATE_SUFFIX = ".dfl";
  public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
      try {
        FileInputStream fin = new FileInputStream(args[i]);
        FileOutputStream fout = new FileOutputStream(args[i] + DEFLATE_SUFFIX);
        DeflaterOutputStream dos = new DeflaterOutputStream(fout);
        for (int c = fin.read(); c != -1; c = fin.read()) {
          dos.write(c);
        }
        dos.close();
        fin.close();
      } catch (IOException ex) {
        System.err.println(ex);
      }
    }
  }
}





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();
  }
}