我正在解决我的另一个问题,该问题涉及读取PNG文件中“ zTXt”块中的数据。我已经定位到文件中的块,并读取了zTXt的关键字。但是,我无法读取zTXt的压缩部分。我以前从未使用过DeflateStream对象,并且遇到了一些麻烦。在读取时,它似乎期望长度参数为“未压缩”字节。但在我的情况下,我只知道“压缩”字节中数据的长度。为了希望克服这个问题,我将所有需要解压缩的数据放入MemoryStream中,然后使用DeflateStream进行“读取到结束”。但是,这会抛出一个InvalidDataException异常,其中包含消息“块长度与其补码不匹配。”我现在不知道这意味着什么。可能出了什么问题?
块的格式是ID(“zTXt”)的4个字节,数据长度的大端32位整数,数据,最后是CRC32校验和,我现在忽略它。
块的格式是ID(“zTXt”)的4个字节,数据长度的大端32位整数,数据,最后是CRC32校验和,我现在忽略它。
zTXt块的格式首先是以空字符结尾的字符串作为关键字,然后是一个字节用于压缩方法(始终为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 != '\0');
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, 0, 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是如何工作的。
非常感谢!!