我正在嘗試使用帶有ZLIB .NET庫的字典,但ZStream
成員inflateSetDictionary
始終返回Z_STREAM_ERROR
。我已經跟蹤到這個子調用Inflate.inflateSetDictionary
哪個測試是否(z.istate.mode == DICT0
)如何在ZLIB.NET中使用字典?
有誰知道如何使用這個庫的字典或知道任何好例子。我的代碼的簡化版本如下...
public class Form1 : System.Windows.Forms.Form
{
static private string sDictionary = "VALUE1,VALUE2,VALUE3";
class GZOutputStream : ZOutputStream
{
public GZOutputStream(Stream in_Renamed)
: base(in_Renamed)
{
byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(sDictionary);
z.inflateSetDictionary(dictionary, dictionary.Length);
}
public GZOutputStream(Stream in_Renamed, int level)
: base(in_Renamed, level)
{
byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(sDictionary);
z.deflateSetDictionary(dictionary, dictionary.Length);
}
}
class GZInputStream : ZInputStream
{
public GZInputStream(Stream in_Renamed)
: base(in_Renamed)
{
byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(sDictionary);
z.inflateSetDictionary(dictionary, dictionary.Length);
}
public GZInputStream(Stream in_Renamed, int level)
: base(in_Renamed, level)
{
byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(sDictionary);
z.deflateSetDictionary(dictionary, dictionary.Length);
}
}
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();
}
private void compressFile(string inFile, string outFile)
{
FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
GZOutputStream outZStream = new GZOutputStream(outFileStream, 1); // zlibConst.Z_DEFAULT_COMPRESSION);
System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
try
{
CopyStream(inFileStream, outZStream);
}
finally
{
outZStream.Close();
outFileStream.Close();
inFileStream.Close();
}
}
private void decompressFile(string inFile, string outFile)
{
FileStream outFileStream = new FileStream(outFile, FileMode.Create);
GZOutputStream outZStream = new GZOutputStream(outFileStream);
FileStream inFileStream = new FileStream(inFile, FileMode.Open);
try
{
CopyStream(inFileStream, outZStream);
}
finally
{
outZStream.Close();
outFileStream.Close();
inFileStream.Close();
}
}
}
沒有答案買一個可能的黑客...嘗試將字典轉換爲列表...並嘗試序列化...還嘗試序列化字典中的項目...如果您的字典是字典<字符串,{ customeType}>也許錯誤信息是錯誤的,實際上是因爲它不能序列化字典的內容 – 2011-12-19 14:24:31