最初は、単純にDeflaterのみを利用するパターン
import java.io.ByteArrayOutputStream; import java.util.zip.DataFormatException; import java.util.zip.Deflater; import java.util.zip.Inflater; public class ZLIBTest { /** * @param args * @throws DataFormatException */ public static void main(String[] args) throws DataFormatException { // TODO Auto-generated method stub Deflater compresser = new Deflater(); compresser.setLevel(Deflater.BEST_COMPRESSION); StringBuffer val= new StringBuffer(); for (int i=0;i<10000;i++){ val.append("1000"); val.append(','); } byte[] value = val.toString().getBytes(); compresser.setInput(value); compresser.finish(); ByteArrayOutputStream compos = new ByteArrayOutputStream(value.length); byte[] buf = new byte[1024]; int count; while (!compresser.finished()) { count = compresser.deflate(buf); compos.write(buf, 0, count); } System.out.println("Size:"+value.length+"->"+compos.toByteArray().length); // ここから解凍 Inflater decompresser = new Inflater(); decompresser.setInput(compos.toByteArray()); ByteArrayOutputStream decompos = new ByteArrayOutputStream(); while (!decompresser.finished()) { count = decompresser.inflate(buf); decompos.write(buf, 0, count); } System.out.println("Size:"+decompos.toByteArray().length); System.out.println(decompos.toString()); } }
次に上記のプログラムをDeflaterOutputStream を利用するように変更してみる。
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.DataFormatException; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; import java.util.zip.Inflater; public class ZLIBStreamTest { /** * @param args * @throws DataFormatException * @throws IOException */ public static void main(String[] args) throws DataFormatException, IOException { Deflater compresser = new Deflater(); compresser.setLevel(Deflater.BEST_COMPRESSION); ByteArrayOutputStream compos = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream(compos,compresser); for (int i=0;i<10000;i++){ dos.write("1000".getBytes()); dos.write(','); } dos.finish(); System.out.println("Size:"+compos.toByteArray().length); // ここから解凍 Inflater decompresser = new Inflater(); decompresser.setInput(compos.toByteArray()); ByteArrayOutputStream decompos = new ByteArrayOutputStream(); while (!decompresser.finished()) { byte[] buf = new byte[1024]; int count = decompresser.inflate(buf); decompos.write(buf, 0, count); } System.out.println("Size:"+decompos.toByteArray().length); System.out.println(decompos.toString()); } }
0 件のコメント:
コメントを投稿