0

我正在尝试通过从 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));
            }
        }           
    }

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

4

1 回答 1

0

如果没有一个很好的Minimal、Complete 和 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但如果是这样,我建议您在复制数据后查看显式刷新/关闭对象。

于 2016-01-30T07:32:55.067 回答