Error[8]: Undefined offset: 1, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我正在研究 my other question的解决方案,它正在读取PNG的’zTXt’块中的数据.我就是在文件中找到块,并阅读zTXt的关键字.我在阅读zTXt的压缩部分时遇到问题.我之前从未使用过DeflateStream对象,并且遇到了一些问题.在读取时,似乎期望长度参数为“未压缩”字节.但就我而言,我只知道“压缩”字节中数据的长度.为了解决这个问题,我将需要解压缩的所有数据放入Memory 我正在研究 my other question的解决方案,它正在读取PNG的’zTXt’块中的数据.我就是在文件中找到块,并阅读zTXt的关键字.我在阅读zTXt的压缩部分时遇到问题.我之前从未使用过DeflateStream对象,并且遇到了一些问题.在读取时,似乎期望长度参数为“未压缩”字节.但就我而言,我只知道“压缩”字节中数据的长度.为了解决这个问题,我将需要解压缩的所有数据放入MemoryStream中,然后使用DeflateStream“读取结束”.现在这只是peachy,除了它抛出一个InvalIDDataException,消息“块长度与其补码不匹配”.现在我不知道这意味着什么.怎么可能出错?

块的格式是ID(“zTXt”)的4个字节,数据长度的大端32位int,数据,最后是CRC32校验和,我现在忽略它.

zTXt块的格式首先是以null结尾(字符串作为关键字),然后是压缩方法的一个字节(总是0,DEFLATE方法),其余数据是压缩文本.

我的方法接受一个新的fileStream,并返回一个包含zTXt关键字和数据的字典.

这是现在的怪物:

public static List<keyvaluePair<string,string>> GetZtxt(fileStream stream){    var ret = new List<keyvaluePair<string,string>>();    try {        stream.position = 0;        var br = new BinaryReader(stream,EnCoding.ASCII);        var head = br.ReadBytes(8); // The header is the same for all PNGs.        if (!head.SequenceEqual(new byte[] { 0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A })) return null; // Not a PNG.        while (stream.position < stream.Length) {            int len; // Length of chunk data.            if (BitConverter.IslittleEndian)                len = BitConverter.ToInt32(br.ReadBytes(4).Reverse().ToArray(),0);            else                len = br.ReadInt32();            char[] cname = br.ReadChars(4); // The chunk type.            if (cname.SequenceEqual(new[] { 'z','T','X','t' })) {                var sb = new StringBuilder(); // Builds the null-terminated keyword associated with the chunk.                char c = br.ReadChar();                do {                    sb.Append(c);                    c = br.ReadChar();                }                while (c != '[+++]');                byte method = br.ReadByte(); // The compression method.  Should always be 0. (DEFLATE method.)                if (method != 0) {                    stream.Seek(len - sb.Length + 3,SeekOrigin.Current); // If not 0,skip the rest of the chunk.                    continue;                }                var data = br.ReadBytes(len - sb.Length - 1); // Rest of the chunk data...                var ms = new MemoryStream(data,data.Length); // ...in a MemoryStream...                var ds = new DeflateStream(ms,CompressionMode.Decompress); // ...read by a DeflateStream...                var sr = new StreamReader(ds); // ... and a StreamReader.  Yeesh.                var str = sr.ReadToEnd(); // !!! InvalIDDataException !!!                ret.Add(new keyvaluePair<string,string>(sb.ToString(),str));                stream.Seek(4,SeekOrigin.Current); // Skip the CRC check.            }            else {                stream.Seek(len + 4,SeekOrigin.Current); // Skip the rest of the chunk.            }        }    }    catch (IOException) { }    catch (InvalIDDataException) { }    catch (ArgumentOutOfRangeException) { }    return ret;}

一旦解决了这个问题,我就需要编写一个将这些zTXt块添加到文件中的函数.所以希望我能理解一旦解决了DeflateStream的工作原理.

非常感谢!!

解决方法 经过这么久,我终于找到了问题所在.数据采用zlib格式,与仅使用DEFLATE相比,它存储的数据更多一些.如果我在获取压缩数据之前只读取了2个额外字节,则可以正确读取文件.

见this feedback page.(我没有提交那个.)

我现在在想.这两个字节的值分别为0x78和0x9C.如果我发现除了那些以外的值,我应该假设DEFLATE会失败吗?

总结

以上是内存溢出为你收集整理的c# – 如何在文件的一部分上使用DeflateStream?全部内容,希望文章能够帮你解决c# – 如何在文件的一部分上使用DeflateStream?所遇到的程序开发问题。

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

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
c# – 如何在文件的一部分上使用DeflateStream?_C_内存溢出

c# – 如何在文件的一部分上使用DeflateStream?

c# – 如何在文件的一部分上使用DeflateStream?,第1张

概述我正在研究 my other question的解决方案,它正在读取PNG的’zTXt’块中的数据.我就是在文件中找到块,并阅读zTXt的关键字.我在阅读zTXt的压缩部分时遇到问题.我之前从未使用过DeflateStream对象,并且遇到了一些问题.在读取时,似乎期望长度参数为“未压缩”字节.但就我而言,我只知道“压缩”字节中数据的长度.为了解决这个问题,我将需要解压缩的所有数据放入Memory 我正在研究 my other question的解决方案,它正在读取PNG的’zTXt’块中的数据.我就是在文件中找到块,并阅读zTXt的关键字.我在阅读zTXt的压缩部分时遇到问题.我之前从未使用过DeflateStream对象,并且遇到了一些问题.在读取时,似乎期望长度参数为“未压缩”字节.但就我而言,我只知道“压缩”字节中数据的长度.为了解决这个问题,我将需要解压缩的所有数据放入MemoryStream中,然后使用DeflateStream“读取结束”.现在这只是peachy,除了它抛出一个InvalIDDataException,消息“块长度与其补码不匹配”.现在我不知道这意味着什么.怎么可能出错?

块的格式是ID(“zTXt”)的4个字节,数据长度的大端32位int,数据,最后是CRC32校验和,我现在忽略它.

zTXt块的格式首先是以null结尾(字符串作为关键字),然后是压缩方法的一个字节(总是0,DEFLATE方法),其余数据是压缩文本.

我的方法接受一个新的fileStream,并返回一个包含zTXt关键字和数据的字典.

这是现在的怪物:

public static List<keyvaluePair<string,string>> GetZtxt(fileStream stream){    var ret = new List<keyvaluePair<string,string>>();    try {        stream.position = 0;        var br = new BinaryReader(stream,EnCoding.ASCII);        var head = br.ReadBytes(8); // The header is the same for all PNGs.        if (!head.SequenceEqual(new byte[] { 0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A })) return null; // Not a PNG.        while (stream.position < stream.Length) {            int len; // Length of chunk data.            if (BitConverter.IslittleEndian)                len = BitConverter.ToInt32(br.ReadBytes(4).Reverse().ToArray(),0);            else                len = br.ReadInt32();            char[] cname = br.ReadChars(4); // The chunk type.            if (cname.SequenceEqual(new[] { 'z','T','X','t' })) {                var sb = new StringBuilder(); // Builds the null-terminated keyword associated with the chunk.                char c = br.ReadChar();                do {                    sb.Append(c);                    c = br.ReadChar();                }                while (c != '');                byte method = br.ReadByte(); // The compression method.  Should always be 0. (DEFLATE method.)                if (method != 0) {                    stream.Seek(len - sb.Length + 3,SeekOrigin.Current); // If not 0,skip the rest of the chunk.                    continue;                }                var data = br.ReadBytes(len - sb.Length - 1); // Rest of the chunk data...                var ms = new MemoryStream(data,data.Length); // ...in a MemoryStream...                var ds = new DeflateStream(ms,CompressionMode.Decompress); // ...read by a DeflateStream...                var sr = new StreamReader(ds); // ... and a StreamReader.  Yeesh.                var str = sr.ReadToEnd(); // !!! InvalIDDataException !!!                ret.Add(new keyvaluePair<string,string>(sb.ToString(),str));                stream.Seek(4,SeekOrigin.Current); // Skip the CRC check.            }            else {                stream.Seek(len + 4,SeekOrigin.Current); // Skip the rest of the chunk.            }        }    }    catch (IOException) { }    catch (InvalIDDataException) { }    catch (ArgumentOutOfRangeException) { }    return ret;}

一旦解决了这个问题,我就需要编写一个将这些zTXt块添加到文件中的函数.所以希望我能理解一旦解决了DeflateStream的工作原理.

非常感谢!!

解决方法 经过这么久,我终于找到了问题所在.数据采用zlib格式,与仅使用DEFLATE相比,它存储的数据更多一些.如果我在获取压缩数据之前只读取了2个额外字节,则可以正确读取文件.

见this feedback page.(我没有提交那个.)

我现在在想.这两个字节的值分别为0x78和0x9C.如果我发现除了那些以外的值,我应该假设DEFLATE会失败吗?

总结

以上是内存溢出为你收集整理的c# – 如何在文件的一部分上使用DeflateStream?全部内容,希望文章能够帮你解决c# – 如何在文件的一部分上使用DeflateStream?所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存