如何在C 和Java之间压缩和解压缩

How to compress and decompress between C++ and Java?

本文关键字:之间 压缩 解压缩 Java      更新时间:2023-10-16

asl

我面临一个问题,可以在Java和C 之间进行压缩和解压缩。

这是Java代码在服务器上运行。

public static byte[] CompressByDeflater(byte[] toCompress) throws IOException
{
    ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();
    DeflaterOutputStream inflater = new DeflaterOutputStream(compressedStream);
    inflater.write(toCompress, 0, toCompress.length);
    inflater.close();
    return compressedStream.toByteArray();
}
public static byte[] DecompressByInflater(byte[] toDecompress) throws IOException
{
    ByteArrayOutputStream uncompressedStream = new ByteArrayOutputStream();
    ByteArrayInputStream compressedStream = new ByteArrayInputStream(toDecompress);
    InflaterInputStream inflater = new InflaterInputStream(compressedStream);
    int c;
    while ((c = inflater.read()) != -1)
    {
        uncompressedStream.write(c);
    }
    return uncompressedStream.toByteArray();
}

我从服务器接收一个二进制文件。

然后我必须使用C 解压缩。

我从哪里开始?

您的压缩程序使用ZLIB(请参阅JDK文档),因此您需要使用C Zlib库来解压缩其输出。

Zlib文档是开始的地方。