Nén dự liệu trong Java

package test.lession3;

import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class DeflaterExample {

	public static void main(String[] args) throws DataFormatException, IOException {
		String input = "私はチャンヴぁんチュンと申します。ベトナムからまいりました。私はチャンヴぁんチュンと申します。ベトナムからまいりました。私はチャンヴぁんチュンと申します。ベトナムからまいりました。私はチャンヴぁんチュンと申します。ベトナムからまいりました。私はチャンヴぁんチュンと申します。ベトナムからまいりました。";
		byte[] inputObj = input.getBytes("UTF-8");
		System.out.println("Length: " + inputObj.length);
		// Compress the bytes
		byte[] output = new byte[100];
		Deflater deflater = new Deflater();
		deflater.setInput(inputObj);
		deflater.finish();
		int compressDataLength = deflater.deflate(output);
		System.out.println(compressDataLength);

		// Giai nén file
		Inflater inflaterOjb = new Inflater();
		inflaterOjb.setInput(output, 0, compressDataLength);
		byte[] resultObj = new byte[1024];
		int resultLength = inflaterOjb.inflate(resultObj);
		inflaterOjb.end();

		String output2 = new String(resultObj, 0, resultLength);
		System.out.println(output2);
	}

}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started