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(); } }