Hi, everyone...
I have a Memorystream and put in a GZipStream, save
in SQL 2005, after that I load this stream from sql 2005 and try to
deserialize from BinaryFormatter. This will be the GOLD, for now I whant a
simple serializable-Compress-Decompress-Deserializable objects is very very fine... I anyone could help-me I will be very gratefull... I past the code I´m using, for traing to make it works... In the last line I´m reciveing an error:
"Binary stream '0' does not contain a valid BinaryHeader. Possible
causes are invalid stream or object version change between
serialization and deserialization." Anyone have any tip ou anything maybe helps me with that??? THANKS A LOT IN ADVANCE!!!
My Code:
public static void Main()
{
List p3;
List p1 = new List();
p1.Add("Brazil");
p1.Add("Error");
p1.Add("Serializable");
Stream Serial = new MemoryStream();
FileStream fileStream = new FileStream("Teste.bin", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
//BinaryFormatter teste = new BinaryFormatter();
Processo _Retorno = new Processo();
BinaryFormatter teste = new BinaryFormatter();
teste.Serialize(Serial, p1);
byte[] buffer = new byte[Serial.Length];
Serial.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Close();
//teste.Serialize(fileStream, _Retorno );
byte[] Compremido = Compressor.Compress(buffer);
//FileStream destino = new FileStream("Teste.ZIP", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
//destino.Write(Compremido,0,Compremido.Length );
//destino.Close();
byte[] Descompremido = Compressor.Decompress(Compremido);
MemoryStream msUnZip = new MemoryStream();
msUnZip.Write(Descompremido, 0, Descompremido.Length);
msUnZip.Seek(0, SeekOrigin.Begin);
BinaryFormatter Serializador = new BinaryFormatter();
p3 = (List)Serializador.Deserialize(msUnZip);
}
public static class Compressor
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input,
CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
}
Font:
http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx[
^]