gpt4 book ai didi

java - Java 中的 Inflate 与 Deflate 函数等效

转载 作者:行者123 更新时间:2023-12-02 01:23:41 25 4
gpt4 key购买 nike

我有一个Java解压函数。我想要相同的等效压缩函数。现在我的 deflate 函数给出了完全不同的输出。功能如下:

        public static String compress(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 new String(Base64.getEncoder().encode(output));
}

public static String decompress(byte[] data) throws IOException, DataFormatException {
Inflater inflater = new Inflater(true);
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 new String(output);
}

使用“LcrJCYAwEEDRVlKBzmJmnNwELx4EW8haxRRvEOFfPrzzuSSamOtKthKgBaQEMAvH7QjqDLaoOE7C8JNElEg/EZ2iS61QtkbKuJeR5woz59FBWhduLw==”的解压缩函数给出“DPI65969|7/29/201 9 上午 12:00:00|107|309.76|1|2019 年 7 月 30 日上午 1:22:27|15| 25|6cc0b4d27318bfa6cc6333afe06de63d"

但是在压缩函数中使用“DPI65969|7/29/2019 12:00:00 AM|107|309.76|1|7/30/2019 1:22:27 AM|15|25|6cc0b4d27318bfa6cc6333afe06de63d”给出“eJwtyskJgDAQQNFWUoHOYmac 3AQvHgRbyFrFFG8Q4V8 +vPO5JJqY60q2EqAFpAQwC8ftCOoMtqg4TsLwk0SUSD8RnaJLrVC2Rsq4l5HnCjPn0UFaF24vAkwaGg==”但我期望压缩结果相同。

我不想改变我的减压功能,因为它在其他地方广泛使用。

我正在使用这些功能,如下所示:

        public static void main(String[] args) 
{
try{
//Compress Block
String strTocompress="DPI65969|7/29/2019 12:00:00 AM|107|309.76|1|7/30/2019 1:22:27 AM|15|25|6cc0b4d27318bfa6cc6333afe06de63d";
byte[] bytes = strTocompress.getBytes("ASCII");
String encoded = compress(bytes);
System.out.println(encoded);
//End of Compress Block
//***********Decompress Block**************
String strToDecompress="LcrJCYAwEEDRVlKBzmJmnNwELx4EW8haxRRvEOFfPrzzuSSamOtKthKgBaQEMAvH7QjqDLaoOE7C8JNElEg/EZ2iS61QtkbKuJeR5woz59FBWhduLw==";
byte[] bytes1 = Base64.getDecoder().decode(strToDecompress.getBytes());
String decoded = decompress(bytes1);
System.out.println(decoded);
//End of Decompress Block
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

最佳答案

    String s = "hello"; 
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(stream,
compresser);
deflaterOutputStream.write(s.getBytes());
deflaterOutputStream.close();
byte[] output= new Base64().encode(stream.toByteArray());
String re1 = new String(output);

关于java - Java 中的 Inflate 与 Deflate 函数等效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57280120/

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