簡體   English   中英

如何在C ++和Java之間壓縮和解壓縮?

[英]How to compress and decompress between C++ and Java?

所有

我遇到了在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文檔是開始的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM