0
votes

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.

1

1 Answers

0
votes

Hard to say for sure what all might be wrong, without the question including a good Minimal, Complete, and Verifiable example. But for sure, you should be reading from the compressed stream, not writing to it. E.g. something like this:

   using (MemoryStream resultDeCompressedStream = new MemoryStream())
   {
       using (DeflateStream decompressionStream = new DeflateStream(decompressfileStream, CompressionMode.Decompress))
       {
           decompressionStream.CopyTo(resultDeCompressedStream);

           string decompressedString = Encoding.UTF8.GetString(resultDeCompressedStream.ToArray(), 0, Convert.ToInt32(resultDeCompressedStream.Length));
       }
   }

Note: your code was also using the datafileStream object, which of course was just the original uncompressed data file, and which had already been read to the end. So even ignoring that you were misusing DeflateStream, that wouldn't have worked either. I've addressed that issue in the above example as well, i.e. by writing to your apparent intended output stream, and by using the decompressfileStream object as the input for the DeflateStream.

Note: it's not clear from your question whether you are also having trouble with the compression side of things. But if so, I recommend you look at explicitly flushing/closing the DeflateStream object after copying the data.