Java文字列を圧縮して送信する方法は何ですか
Javaでは文字列の圧縮転送に圧縮アルゴリズムを使用でき、一般的な圧縮方法を次に示します。
- GZIP圧縮: JavaのGZIPOutputStreamクラスで圧縮し、GZIPInputStreamクラスで展開できます。圧縮と展開は、以下のコードで行えます。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GZIPCompression {
public static byte[] compress(String data) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data.getBytes("UTF-8"));
gzip.close();
byte[] compressedData = bos.toByteArray();
bos.close();
return compressedData;
}
public static String decompress(byte[] compressedData) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(compressedData);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buffer = new byte[1024];
StringBuilder sb = new StringBuilder();
int len;
while ((len = gzip.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len, "UTF-8"));
}
gzip.close();
bis.close();
return sb.toString();
}
public static void main(String[] args) throws IOException {
String originalString = "This is a test string";
byte[] compressedData = compress(originalString);
String decompressedString = decompress(compressedData);
System.out.println("Original string: " + originalString);
System.out.println("Compressed data: " + new String(compressedData));
System.out.println("Decompressed string: " + decompressedString);
}
}
- デフレータ圧縮では、Java の Deflater クラスを使って圧縮し、Inflater クラスを使って伸張します。圧縮と伸張は、以下のコードを使って行えます。
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class DeflaterCompression {
public static byte[] compress(String data) throws IOException {
byte[] input = data.getBytes("UTF-8");
Deflater deflater = new Deflater();
deflater.setInput(input);
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
deflater.finish();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer);
bos.write(buffer, 0, count);
}
bos.close();
byte[] compressedData = bos.toByteArray();
return compressedData;
}
public static String decompress(byte[] compressedData) throws IOException {
Inflater inflater = new Inflater();
inflater.setInput(compressedData);
ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
bos.write(buffer, 0, count);
}
bos.close();
byte[] decompressedData = bos.toByteArray();
return new String(decompressedData, "UTF-8");
}
public static void main(String[] args) throws IOException {
String originalString = "This is a test string";
byte[] compressedData = compress(originalString);
String decompressedString = decompress(compressedData);
System.out.println("Original string: " + originalString);
System.out.println("Compressed data: " + new String(compressedData));
System.out.println("Decompressed string: " + decompressedString);
}
}
これらの手法で文字列を圧縮し送信し、受信側で展開することにより元の文字列に戻せる。