从C ++到C#的zlib(如何将byte []转换为流并将流转换为byte [])

时间:2009-04-10 10:30:41

标签: c# byte zlib

我的任务是使用zlib解压缩数据包(已接收),然后使用算法从数据中生成图片

好消息是我有C ++代码,但任务是在C#中完成

C ++

        //Read the first values of the packet received

        DWORD image[200 * 64] = {0}; //used for algoritm(width always = 200 and height always == 64)
        int imgIndex = 0; //used for algoritm
        unsigned char rawbytes_[131072] = {0}; //read below
        unsigned char * rawbytes = rawbytes_; //destrination parameter for decompression(ptr)
        compressed = r.Read<WORD>(); //the length of the compressed bytes(picture)
        uncompressed = r.Read<WORD>(); //the length that should be after decompression
        width = r.Read<WORD>(); //the width of the picture
        height = r.Read<WORD>(); //the height of the picture

        LPBYTE ptr = r.GetCurrentStream(); //the bytes(file that must be decompressed)

        outLen = uncompressed; //copy the len into another variable

        //Decompress

        if(uncompress((Bytef*)rawbytes, &outLen, ptr, compressed) != Z_OK)
        {
            printf("Could not uncompress the image code.\n");
            Disconnect();
            return;
        }

        //Algoritm to make up the picture
        // Loop through the data
        for(int c = 0; c < (int)height; ++c)
        {
            for(int r = 0; r < (int)width; ++r)
            {
                imgIndex = (height - 1 - c) * width + r;
                image[imgIndex] = 0xFF000000;
                if(-((1 << (0xFF & (r & 0x80000007))) & rawbytes[((c * width + r) >> 3)])) 
                    image[imgIndex] = 0xFFFFFFFF;
            }
        }

我正在尝试使用zlib.NET,但所有演示都有解压缩的代码(C#)

    private void decompressFile(string inFile, string outFile)
    {
        System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
        zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream);
        System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);          
        try
        {
            CopyStream(inFileStream, outZStream);
        }
        finally
        {
            outZStream.Close();
            outFileStream.Close();
            inFileStream.Close();
        }
    }

    public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
    {
        byte[] buffer = new byte[2000];
        int len;
        while ((len = input.Read(buffer, 0, 2000)) > 0)
        {
            output.Write(buffer, 0, len);
        }
        output.Flush();
    }

我的问题:我不想在解压缩后保存文件,因为我必须使用C ++代码中显示的算法。

如何将byte []数组转换为类似于C#zlib代码中的数据流来解压缩数据然后如何将流转换回字节数组?

另外,如何将zlib.NET代码更改为不保存文件?

2 个答案:

答案 0 :(得分:10)

只需使用MemoryStreams而不是FileStreams:

// Assuming inputData is a byte[]
MemoryStream input = new MemoryStream(inputData);
MemoryStream output = new MemoryStream();

然后您可以使用output.ToArray()来获取字节数组。

请注意,使用using语句而不是单个try / finally块通常会更好 - 否则,如果第一次调用Close失败,则不会进行其余操作。你可以像这样嵌套它们:

using (MemoryStream output = new MemoryStream())
using (Stream outZStream = new zlib.ZOutputStream(output))
using (Stream input = new MemoryStream(bytes))
{
    CopyStream(inFileStream, outZStream);
    return output.ToArray();
}

答案 1 :(得分:6)

我刚遇到同样的问题。

完整性......(因为这让我困了好几个小时)

在ZLib.Net的情况下,您还必须调用finish(),这通常在Close()期间发生,然后再调用return output.ToArray()

否则,您将从内存流中获取一个空/不完整的字节数组,因为ZStream尚未实际写入所有数据:

public static void CompressData(byte[] inData, out byte[] outData)
{
    using (MemoryStream outMemoryStream = new MemoryStream())
    using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_DEFAULT_COMPRESSION))
    using (Stream inMemoryStream = new MemoryStream(inData))
    {
        CopyStream(inMemoryStream, outZStream);
        outZStream.finish();
        outData = outMemoryStream.ToArray();
    }
}

public static void DecompressData(byte[] inData, out byte[] outData)
{
    using (MemoryStream outMemoryStream = new MemoryStream())
    using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream))
    using (Stream inMemoryStream = new MemoryStream(inData))
    {
        CopyStream(inMemoryStream, outZStream);
        outZStream.finish();
        outData = outMemoryStream.ToArray();
    }
}

在这个例子中,我也使用zlib名称空间:

using zlib;

最初在此主题中找到: ZLib decompression

我还没有足够的积分进行投票,所以......

感谢Tim Greaves关于ToArray之前完成的提示

Jon Skeet关于嵌套流的using语句的提示(我比try / finally更好)