I am trying to compress and decompress data by reading contents from a file in WP8 .I am not able to compress or decompress.While compressing the resultant string is always empty. While decompress getting error as "An exception of type 'System.IO.InvalidDataException' occurred in SYSTEM.IO.COMPRESSION.NI.DLL but was not handled in user code
Additional information: Block length does not match with its complement."
Code is below
public static async void CompressDecompress()
{
//Location of File to be Compressed
string toCompressFileName = "ms-appx:///Assets/data.txt";
StorageFile toCompressFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(toCompressFileName));
Stream toCompressStream = await toCompressFile.OpenStreamForReadAsync();
toCompressStream.Position = 0;
string compressedString = Compress(toCompressStream);
//Location of File to be Decompressed
string toDecompressFileName = "ms-appx:///Assets/zipped_data.txt";
StorageFile toDecompressFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(toDecompressFileName));
Stream toDecompressStream = await toDecompressFile.OpenStreamForReadAsync();
toDecompressStream.Position = 0;
string decompressedString = DeCompress(toDecompressStream);
}
public static string Compress(Stream toCompressStream)
{
using (MemoryStream resultCompressedStream = new MemoryStream())
{
using (DeflateStream compressionStream = new DeflateStream(resultCompressedStream, CompressionMode.Compress))
{
toCompressStream.CopyTo(compressionStream);
toCompressStream.Flush();
return Encoding.UTF8.GetString(resultCompressedStream.ToArray(), 0, Convert.ToInt32(resultCompressedStream.Length));
}
}
}
public static string DeCompress(Stream toDecompressStream)
{
using (MemoryStream resultDeCompressedStream = new MemoryStream())
{
using (DeflateStream decompressionStream = new DeflateStream(toDecompressStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(resultDeCompressedStream);
decompressionStream.Flush();
return Encoding.UTF8.GetString(resultDeCompressedStream.ToArray(), 0, Convert.ToInt32(resultDeCompressedStream.Length));
}
}
}
Please help me to figure what is going wrong.