gpt4 book ai didi

java - 用PHP压缩的字符串与用Java压缩的字符串不匹配

转载 作者:行者123 更新时间:2023-11-30 04:58:32 25 4
gpt4 key购买 nike

压缩php中的单词“ test”返回
使用该行的“ eNorSS0uAQAEXQHB”


  base64_encode(gzcompress(“ test”,9))


我也想要在Android应用中使用相同的方法。所以,我用下面的代码:

try {
byte[] input = "test".getBytes("UTF-8");
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
compresser.deflate(output);
compresser.end();
return Base64.encodeToString(output, Base64.DEFAULT);
} catch (Exception e) {
return e.getMessage();
}


作为输出


  eJzLz0sFAAKRAUMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ==




尝试了另一种方法,如下所示:

try {
ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();

byte[] compressedBytes = out.toByteArray();
return Base64.encodeToString(compressedBytes, Base64.DEFAULT); // I would return compressedBytes instead String
} catch (Exception e) {
return e.getMessage();
}


作为输出


  H4sIAAAAAAAAAMvPSwUA8YZsegMAAAA =


应该使用哪种方法从Java中获取相同的压缩文件(与php中一样)=> eNorSS0uAQAEXQHB

最佳答案

您的方法有两个问题。


该代码未使用相同的压缩级别(PHP-> 9,Java-> 6(默认))
压缩数据的长度(由compresser.deflate(output)返回)被忽略


这是一个冗长的工作示例,用于在Java中生成与PHP代码段相同的Base64输出。

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.zip.Deflater;

public class CompressDemo {
public static void main(String[] args) throws Exception {
byte[] input = "test".getBytes(StandardCharsets.UTF_8);
byte[] buffer = new byte[100];
// create a deflater for the compression to best compression level (9)
Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
compresser.setInput(input);
compresser.finish();
// keep the length of the compressed data
int compressedDataLength = compresser.deflate(buffer);
compresser.end();
// create an array only containing the compressed bytes
byte[] compressed = Arrays.copyOfRange(buffer, 0, compressedDataLength);
System.out.println(new String(Base64.getEncoder().encode(compressed)));
}
}


编辑一个示例,其中缓冲区很小,可以一次存储整个压缩数据。

public static void main(String[] args) throws Exception {
ByteArrayOutputStream resultBuffer = new ByteArrayOutputStream();
byte[] input = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
// a buffer to small to hold the compressed data
byte[] buffer = new byte[10];
Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
compresser.setInput(input);
compresser.finish();
do {
// compress the input data and fill the buffer
int compressedDataLength = compresser.deflate(buffer);
System.out.println("compressedDataLength = " + compressedDataLength);
// append the compressed data to the result buffer
resultBuffer.write(buffer, 0, compressedDataLength);
// continue until all input data were processed
} while(!compresser.finished());
compresser.end();
byte[] compressed = resultBuffer.toByteArray();
System.out.println(new String(Base64.getEncoder().encode(compressed)));
}


输出

compressedDataLength = 10
compressedDataLength = 10
compressedDataLength = 10
compressedDataLength = 4
eNpzdHJ2cXVz9/D08vbx9fMPCAwKDgkNC4+IjAIAZKYH4A==

关于java - 用PHP压缩的字符串与用Java压缩的字符串不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58705757/

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