中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

java字符串壓縮傳輸的方法是什么

小億
95
2023-11-27 17:41:32
欄目: 編程語言

Java中可以使用壓縮算法對字符串進行壓縮傳輸,常用的壓縮方法有以下幾種:

  1. 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);
    }
}
  1. Deflater壓縮:可以使用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);
    }
}

這些方法可以將字符串進行壓縮,然后傳輸。在接收端,再進行解壓縮操作,恢復原始字符串。

0
通道| 临桂县| 湘潭县| 云霄县| 金乡县| 墨江| 云和县| 深泽县| 任丘市| 清原| 志丹县| 武宣县| 乐平市| 略阳县| 哈尔滨市| 马尔康县| 洪泽县| 繁峙县| 南城县| 深水埗区| 南和县| 常熟市| 葵青区| 西盟| 玉田县| 尚义县| 呼伦贝尔市| 四子王旗| 泉州市| 奉化市| 恩施市| 阿拉善盟| 长治县| 科技| 峨眉山市| 富蕴县| 富民县| 嘉祥县| 乌鲁木齐县| 阿勒泰市| 缙云县|