【问题标题】:Compression and Encoding giving Wrong results in Strings压缩和编码在字符串中给出错误的结果
【发布时间】:2016-04-14 09:29:01
【问题描述】:

我正在尝试压缩一个字符串。我正在使用 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. 编码。

请帮忙。谢谢。

【问题讨论】:

  • new ByteArrayOutputStream(data.length) => 如果要解压缩数据,可能需要比输入更大的缓冲区。
  • @AndyTurner 增加了 10 倍,没有任何区别。对空白有什么想法吗?
  • 我对您在compressString 中对Base64.decode 的初始调用非常怀疑-您没有传入Base64 编码的字符串。如果我不得不猜测,我会说这就是空间被剥离的地方。

标签: java base64 deflate inflate


【解决方案1】:

compressString 中,替换:

Base64.decode(uncompressedString)

uncompressString.getBytes(StandardCharsets.UTF_8)

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

类似decompressString,替换:

Base64.encode(bytes)

new String(bytes, StandardCharsets.UTF_8)

【讨论】:

  • @谢谢,它成功了。我不必要地使用 base64 使它过于复杂。
猜你喜欢
  • 2015-12-19
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多