[英]Equivalent Inflate for Deflate function in Java
I have a function for decompression in Java. 我有一个Java解压功能。 I want same equivalent function for compression. 我想要相同的压缩功能。 Right now my deflate function gives altogether different output. 现在,我的deflate函数提供了完全不同的输出。 Functions are as below: 功能如下:
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);
}
Using decompress function for "LcrJCYAwEEDRVlKBzmJmnNwELx4EW8haxRRvEOFfPrzzuSSamOtKthKgBaQEMAvH7QjqDLaoOE7C8JNElEg/EZ2iS61QtkbKuJeR5woz59FBWhduLw==" Gives "DPI65969|7/29/2019 12:00:00 AM|107|309.76|1|7/30/2019 1:22:27 AM|15|25|6cc0b4d27318bfa6cc6333afe06de63d" 为 “LcrJCYAwEEDRVlKBzmJmnNwELx4EW8haxRRvEOFfPrzzuSSamOtKthKgBaQEMAvH7QjqDLaoOE7C8JNElEg / EZ2iS61QtkbKuJeR5woz59FBWhduLw ==” 使用解压缩功能赋予“DPI65969 | 2019年7月29日12:00:00 AM | 107 | 309.76 | 1 | 2019年7月30日上午01时22分27秒| 15 | 25 | 6cc0b4d27318bfa6cc6333afe06de63d ”
But using "DPI65969|7/29/2019 12:00:00 AM|107|309.76|1|7/30/2019 1:22:27 AM|15|25|6cc0b4d27318bfa6cc6333afe06de63d" in compress function gives "eJwtyskJgDAQQNFWUoHOYmac3AQvHgRbyFrFFG8Q4V8+vPO5JJqY60q2EqAFpAQwC8ftCOoMtqg4TsLwk0SUSD8RnaJLrVC2Rsq4l5HnCjPn0UFaF24vAkwaGg==" But I am expecting same result on compression. 但使用 “DPI65969 | 2019年7月29日12:00:00 AM | 107 | 309.76 | 1 |二零一九年七月三十零日上午1点22分27秒| 15 | 25 | 6cc0b4d27318bfa6cc6333afe06de63d” 在压缩功能赋予“eJwtyskJgDAQQNFWUoHOYmac3AQvHgRbyFrFFG8Q4V8 + vPO5JJqY60q2EqAFpAQwC8ftCOoMtqg4TsLwk0SUSD8RnaJLrVC2Rsq4l5HnCjPn0UFaF24vAkwaGg = =“但我期望压缩时得到相同的结果。
I don't want to change my decompression function as it's widely used somewhere else. 我不想更改我的解压缩功能,因为它已在其他地方广泛使用。
I am using these functions as below: 我正在使用这些功能,如下所示:
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());
}
}
@Kartikeya You need to replace Deflater deflater = new Deflater(); @Kartikeya您需要替换Deflater deflater = new Deflater(); line with Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION,true); 带有Deflater deflater的行= new Deflater(Deflater.BEST_COMPRESSION,true); in your compress function. 在您的压缩功能。
public static String compress(byte[] data) throws IOException {
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION,true);
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));
}
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.