c# - zlib from C++ to C#(How to convert byte[] to stream and stream to byte[])

标签 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 代码更改为不保存文件?

最佳答案

只需使用 MemoryStreams 而不是 FileStreams:

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

然后你可以使用 output.ToArray() 得到一个字节数组。

请注意,通常最好使用 using 语句而不是单个 try/finally block - 否则如果第一次调用 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();
}

关于c# - zlib from C++ to C#(How to convert byte[] to stream and stream to byte[]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/737258/

相关文章:

c# - 如何使用 SharpSSH 以编程方式从 SFTP 服务器删除文件?

c# - 如何将 "concatenate"或 "combine"或 "join"序列化为一系列 'binarily' 字节数组?

python - PyQT5 未找到 zlib 错误

Git 安装 RHEL Linux 失败,出现 "warning: zlib.h: No such file or directory"

c - C语言从文件中读取数字

c++ - 使用 zlib/minizip 确定压缩 block 大小

c# - 如何强制派生类填充基类中定义的列表?

c# - 将 .NET Core 类库引用添加到 UWP 应用程序时,它显示 "Unable to add a reference to project..."

c# - NHibernate 中的 QueryOver

c++ - 如何在 char 数组中分配特定数量的字节 - 内存管理