Javazip-deflater-setdictionary1
提供:Dev Guides
java.util.zip.Deflater.setDictionary()メソッドの例
説明
- java.util.zip.Deflater.setDictionary(byte [] b、int off、int len)*メソッドは、圧縮用のプリセット辞書を設定します。 履歴バッファを事前に決定できる場合、プリセット辞書が使用されます。 データが後でInflater.inflate()で圧縮解除されると、Inflater.getAdler()を呼び出して、圧縮解除に必要な辞書のAdler-32値を取得できます。
宣言
以下は、* java.util.zip.Deflater.setDictionary(byte [] b、int off、int len)*メソッドの宣言です。
public void setDictionary(byte[] b, int off, int len)
パラメーター
- b -辞書データバイト。
- off -データの開始オフセット。
- len -データの長さ。
例
次の例は、java.util.zip.Deflater.setDictionary(byte [] b、int off、int len)メソッドの使用方法を示しています。
package com.finddevguides;
import java.io.UnsupportedEncodingException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class DeflaterDemo {
public static void main(String[] args)
throws DataFormatException, UnsupportedEncodingException {
String message = "Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;"
+"Welcome to finddevguides.com;";
String dictionary = "Welcome";
System.out.println("Original Message length : " + message.length());
byte[] input = message.getBytes("UTF-8");
//Compress the bytes
byte[] output = new byte[1024];
Deflater deflater = new Deflater();
deflater.setDictionary(dictionary.getBytes("UTF-8"),0, dictionary.length());
deflater.setInput(input);
deflater.finish();
int compressedDataLength = deflater.deflate(output,0 , output.length, Deflater.NO_FLUSH);
System.out.println("Total uncompressed bytes output :" + deflater.getTotalOut());
System.out.println("Compressed Message Checksum :" + deflater.getAdler());
deflater.finished();
System.out.println("Compressed Message length : " + compressedDataLength);
//Decompress the bytes
Inflater inflater = new Inflater();
inflater.setInput(output, 0, compressedDataLength);
byte[] result = new byte[1024];
int resultLength = inflater.inflate(result);
if(inflater.needsDictionary()){
inflater.setDictionary(dictionary.getBytes("UTF-8"),0, dictionary.length());
resultLength = inflater.inflate(result);
}
inflater.finished();
//Decode the bytes into a String
message = new String(result, 0, resultLength, "UTF-8");
System.out.println("UnCompressed Message Checksum :" + inflater.getAdler());
System.out.println("UnCompressed Message length : " + message.length());
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Original Message length : 300
Total uncompressed bytes output :40
Compressed Message Checksum :368538129
Compressed Message length : 40
UnCompressed Message Checksum :368538129
UnCompressed Message length : 300
link:/cgi-bin/printpage.cgi [__印刷]