无法压缩解压缩

时间:2016-01-29 11:39:31

标签: c# windows-phone-8.1 compression http-compression deflatestream

我试图通过从WP8中的文件读取内容来压缩和解压缩数据。我无法压缩或解压缩。压缩结果字符串始终为空。解压缩时收到错误为“SYSTEM.IO.COMPRESSION.NI.DLL中发生类型'System.IO.InvalidDataException'的异常但未在用户代码中处理

附加信息:块长度与其补码不匹配。“

代码低于

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

请帮我弄清楚出了什么问题。

1 个答案:

答案 0 :(得分:0)

很难确定一切都可能是错的,没有包括好Minimal, Complete, and Verifiable example的问题。但是,当然,你应该读取压缩流,而不是写入它。例如。像这样的东西:

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

注意:您的代码也使用了datafileStream对象,当然这只是原始的未压缩数据文件,并且已经被读取到最后。因此,即使忽略你滥用DeflateStream,也不会有用。我也在上面的例子中解决了这个问题,即通过写入明显的预期输出流,并使用decompressfileStream对象作为DeflateStream的输入。

注意:从您的问题中不清楚您是否也遇到了压缩方面的问题。但如果是这样,我建议您在复制数据后查看显式刷新/关闭DeflateStream对象。