在 Java 中解压缩字节数组



我的问题是关于C#和Java的。

我想解压缩一个字节数组,就像从这个链接解压缩 C# 的方法一样:https://github.com/AresChat/cb0t/blob/master/cb0t/Misc/Zip.cs

我用Java翻译了该方法:

public static byte[] Decompress(byte[] data)
{
try {
Byte[] r = null;
try (InputStream ms = new ByteArrayInputStream(data);
InflaterInputStream s = new InflaterInputStream(ms)
) {
List<Byte> list = new ArrayList<Byte>();
int count = 0;
byte[] b = new byte[8192];
while ((count = s.read(b, 0, 8192)) > 0) {
for (byte by : Arrays.copyOfRange(b,0,count+1)) {
list.add(by);
}
}
r = list.toArray(r);
list.clear();
list = null;
}
byte[] bytes = new byte[r.length];
int j=0;
// Unboxing Byte values. (Byte[] to byte[])
for(Byte b: r)
bytes[j++] = b.byteValue();
return bytes;
}
catch (Exception e){
System.out.println(e.toString());
}
return new byte[] {};
}

在 C# 中

public static void Main() 
{
string str = "F5fPxdTq8eJeuqSVejGmq7aTh6BJZ8J0jgt92MDDjxTIWf+mWa8Ld+01L2bVIV6FXhCO";
byte[] val2 = Convert.FromBase64String(str);
val2 = d67(val2, 28435);
val2 = Zip.Decompress(val2);
Console.WriteLine("Converted byte value: {0}", BitConverter.ToString(val2));
}
private static byte[] d67(byte[] data, int b)
{
byte[] buffer = new byte[data.Length];
Array.Copy(data, buffer, data.Length);
for (int i = 0; i < data.Length; i++)
{
buffer[i] = (byte)(data[i] ^ b >> 8 & 255);
b = (b + data[i]) * 23219 + 36126 & 65535;
}
return buffer;
}

我得到输出:

00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-43-48-41-54-43-48-41-4E-4E-45-4C-00-36-26-D2-37-31-D4-00-00-00-00-4D-41-59-4F-52-45-53-20-44-45-20-33-30-2C-34-30-2C-35-30-00-00

而在爪哇

public static void main(String[] args)
{
String encodedString = "arlnk://F5fPxdTq8eJeuqSVejGmq7aTh6BJZ8J0jgt92MDDjxTIWf+mWa8Ld+01L2bVIV6FXhCO";
encodedString = encodedString.substring(8);
Base64.Decoder decoder = Base64.getDecoder();
byte[] decodedByteArray = decoder.decode(encodedString);
decodedByteArray = d67(decodedByteArray, 28435);
decodedByteArray = Zip.Decompress(decodedByteArray);
System.out.print(hexlify(decodedByteArray));
}
private static byte[] d67(byte[] data, int b) {
byte[] buffer = new byte[data.length];
System.arraycopy(data, 0, buffer, 0, data.length);
for (int i = 0; i < data.length; i++) {
int unsignedData = unsignedToBytes(data[i]);
buffer[i] = (byte) (unsignedData ^ b >> 8 & 255);
b = (b + unsignedData) * 23219 + 36126 & 65535;
}
return buffer;
}
public static int unsignedToBytes(byte b) {
return b & 0xFF;
}
public static String hexlify(byte[] data) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < data.length; i++) {
b.append(String.format("%02X", data[i]));
if (i < data.length - 1) {
b.append("-");
}
}
return b.toString();
}

我收到异常

java.lang.NullPointerException:尝试获取空数组的长度

但没有像 C# 那样的输出。

我翻译错了什么?我也不明白为什么我会得到这个例外:(...

您的解压缩方法一团糟,根本不起作用。它尝试做的只是返回 inflow 流的完整输出。Java有一个内置的方法可以做到这一点,它可以替换整个东西。当你这样做时,你会得到完全相同的输出。这是完整的程序,准备编译和运行:

import java.util.Base64;
import java.util.zip.InflaterInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
class Example {
public static void main(String[] args) throws Exception {
String encodedString = "arlnk://F5fPxdTq8eJeuqSVejGmq7aTh6BJZ8J0jgt92MDDjxTIWf+mWa8Ld+01L2bVIV6FXhCO";
encodedString = encodedString.substring(8);
Base64.Decoder decoder = Base64.getDecoder();
byte[] decodedByteArray = decoder.decode(encodedString);
decodedByteArray = d67(decodedByteArray, 28435);
decodedByteArray = decompress(decodedByteArray);
System.out.print(hexlify(decodedByteArray));
}
private static byte[] d67(byte[] data, int b) {
byte[] buffer = new byte[data.length];
System.arraycopy(data, 0, buffer, 0, data.length);
for (int i = 0; i < data.length; i++) {
int unsignedData = unsignedToBytes(data[i]);
buffer[i] = (byte) (unsignedData ^ b >> 8 & 255);
b = (b + unsignedData) * 23219 + 36126 & 65535;
}
return buffer;
}
public static int unsignedToBytes(byte b) {
return b & 0xFF;
}
public static String hexlify(byte[] data) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < data.length; i++) {
b.append(String.format("%02X", data[i]));
if (i < data.length - 1) {
b.append("-");
}
}
return b.toString();
}
public static byte[] decompress(byte[] data) throws IOException {
try (InputStream ms = new ByteArrayInputStream(data);
InflaterInputStream s = new InflaterInputStream(ms)) {
return s.readAllBytes();
}
}
}

除了解压缩方法之外,我没有真正改变任何东西,它现在几乎不包含任何代码。

最新更新