gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:03:42 25 4
gpt4 key购买 nike

我正在尝试压缩字符串。我使用 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)

关于java - 压缩和编码在字符串中给出错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36618955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com