我需要實現一個特殊的ZLib實現,它應該在.Net和Mono下運行。 數據/字符串消息通過套接字接收,因此校驗和丟失。 這是關於原始字符串數據,而不是關於文件。Zlib for Mono/.Net沒有一個已知的校驗和
unsigned char zlib_header[]={
// custom additional Zlib Id
'Z', // Our own ID
// The normal GZIP header
0x1f,
0x8b, // GZIP ID
0x08, // Deflated
0x00, // Flags
0, 0, 0, 0, // Timestamp,
0x00, // Extra flags
0x00, // OS identifier
// afterwards compressed data without a checksum
};
我曾嘗試與解壓縮和GZipStream的DeflateStream數據,但我認爲GZStream失敗,因爲缺少校驗和。我也嘗試了各種補償,但沒有運氣。 不使用校驗和,因爲無論如何都是通過套接字接收數據的 - 因此ZLib校驗和會是額外的開銷。 我錯過了什麼,或者你能解釋我如何添加校驗和並調用正確的庫,或者我應該看看支持Mono和.Net的第三方庫嗎? 編輯:性能非常關鍵,因爲這至少每秒完成一次。你會推薦我最後通過Interop使用C-Lib嗎? 我現在總是收到無效數據異常,並且我認爲它與錯誤的校驗和有關。 這是我嘗試沒有成功使用的實際代碼:
const int HeaderSize = 1;
System.IO.MemoryStream ms = new System.IO.MemoryStream(compressedBuffer, HeaderSize, compressedBuffer.Length-HeaderSize);//remove the additional Z from the header
GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress,true);
byte[] deCompressedBytes = new byte[actualBufferLength* 10];
int resultSize=zipStream.Read(deCompressedBytes, 0, actualBufferLength);//get rid of the header
UTF8Encoding enc = new UTF8Encoding();
string result = enc.GetString(deCompressedBytes, 0, resultSize);
我正在與遠程位置的第三方進行整合,他們的軟件中顯然存在一個錯誤 - 他們沒有正確刷新網絡流。還是要謝謝你的幫助。 – weismat 2009-05-10 05:51:22