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

从C到C#的zlib(如何将byte []转换为流并将流转换为byte []),第1张

概述我的任务是使用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 al 我的任务是使用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,2000)) > 0)        {            output.Write(buffer,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块通常会更好 – 否则如果第一次调用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到C#的zlib(如何将byte []转换为流并将流转换为byte [])全部内容,希望文章能够帮你解决从C到C#的zlib(如何将byte []转换为流并将流转换为byte [])所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1232705.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存