压缩和编码在字符串中给出错误的结果

Compression and Encoding giving Wrong results in Strings

我正在尝试压缩一个字符串。我正在使用 Base64 编码和解码将字符串转换为字节,反之亦然。

import org.apache.axis.encoding.Base64;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class UtilTesting {
    public static void main(String[] args) {


        try {
            String original = "I am the god";
            System.out.println("Starting Zlib");
            System.out.println("==================================");
            String zcompressed = compressString(original);
            String zdecompressed = decompressString(zcompressed);
            System.out.println("Original String: "+original);
            System.out.println("Compressed String: "+zcompressed);
            System.out.println("Decompressed String: "+zdecompressed);
        } catch (IOException e) {
            e.printStackTrace();
        }


    public static String compressString(String uncompressedString){
        String compressedString = null;
        byte[] bytes = Base64.decode(uncompressedString);
        try {
            bytes = compressBytes(bytes);
            compressedString = Base64.encode(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return compressedString;
    }

    public static String decompressString(String compressedString){
        String decompressedString = null;
        byte[] bytes = Base64.decode(compressedString);
        try {
            bytes = decompressBytes(bytes);
            decompressedString = Base64.encode(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DataFormatException e) {
            e.printStackTrace();
        }
        return decompressedString;
    }

    public static byte[] compressBytes(byte[] data) throws IOException {
        Deflater deflater = new Deflater();
        deflater.setInput(data);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer); // returns the generated code... index
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();
        return output;
    }

    public static byte[] decompressBytes(byte[] data) throws IOException, DataFormatException {
        Inflater inflater = new Inflater();
        inflater.setInput(data);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();
        return output;
    }
}

这是给出的结果:

    Starting Zlib
==================================
Original String: I am the god
Compressed String: eJxTXLm29YUGAApUAw0=
Decompressed String: Iamthego

如您所见,它缺少空格,甚至丢失了给定字符串中的最后一个字母。

有人可以建议这段代码有什么问题吗? 我正在执行以下步骤:

  1. 解码
  2. 压缩
  3. 编码
  4. 保存
  5. 检索
  6. 解码
  7. 解压缩
  8. 编码。

请帮忙。谢谢。

compressString中替换:

Base64.decode(uncompressedString)

uncompressString.getBytes(StandardCharsets.UTF_8)

您没有传递 base64 编码的字符串;您只需要输入字符串的字节。请注意,空格永远不会出现在 base64 编码中,因此它们可能被视为多余并被丢弃。

decompressString类似,替换为:

Base64.encode(bytes)

new String(bytes, StandardCharsets.UTF_8)