Public object decompress (string filename)
{
Object OBJ = NULL;
Try
{
Using (stream source = file. openread (filename ))
{
Using (Stream Destination = new memorystream ())
{
Using (gzipstream input = new gzipstream (source, compressionmode. decompress, true ))
{
Byte [] bytes = new byte [1, 4096];
Int N;
While (n = input. Read (bytes, 0, bytes. Length ))! = 0)
{
Destination. Write (bytes, 0, N );
}
}
Destination. Flush ();
Destination. Position = 0;
Binaryformatter B = new binaryformatter ();
OBJ = (object) B. deserialize (destination );
}
}
}
Catch
{}
Return OBJ;
}
Private void createcompressfile (Object OBJ, string filename)
{
Try
{
Binaryformatter formatter = new binaryformatter ();
Using (stream source = new memorystream ())
{
Formatter. serialize (source, OBJ );
Source. Position = 0;
Using (Stream Destination = new filestream (filename, filemode. append, fileaccess. Write ))
{
Using (gzipstream output = new gzipstream (destination, compressionmode. Compress ))
{
Byte [] bytes = new byte [1, 4096];
Int N;
While (n = source. Read (bytes, 0, bytes. Length ))! = 0)
{
Output. Write (bytes, 0, N );
}
}
}
}
}
Catch
{}
}
Test:
Public void test ()
{
String filename = appdomain. currentdomain. basedirectory. Trim () + "test. dat ";
Dictionary <string, int> DIC = new dictionary <string, int> ();
Dic. Add ("A", 1 );
Dic. Add ("B", 2 );
Dic. Add ("C", 3 );
Dic. Add ("D", 4 );
Dic. Add ("e", 5 );
Dic. Add ("F", 6 );
Createcompressfile (DIC, filename );
Dic = (Dictionary <string, int>) decompress (filename );
Console. writeline (DIC. Count );
}