【问题标题】:Displaying the contents of a Zip archive in WinRT在 WinRT 中显示 Zip 存档的内容
【发布时间】:2013-11-06 18:26:05
【问题描述】:

我想遍历压缩存档的内容,并在内容可读的地方显示它们。我可以对基于文本的文件执行此操作,但似乎无法弄清楚如何从图像等内容中提取二进制数据。这是我所拥有的:

var zipArchive = new System.IO.Compression.ZipArchive(stream);

foreach (var entry in zipArchive.Entries)
{
    using (var entryStream = entry.Open())
    {
        if (IsFileBinary(entry.Name))
        {
            using (BinaryReader br = new BinaryReader(entryStream))
            {
                //var fileSize = await reader.LoadAsync((uint)entryStream.Length);
                var fileSize = br.BaseStream.Length;
                byte[] read = br.ReadBytes((int)fileSize);

                binaryContent = read;

我可以看到 zip 文件的内部,但调用 Length 会导致出现 OperationNotSupported 错误。另外,考虑到我得到一个 long 然后不得不转换为一个整数,感觉就像我错过了关于它应该如何工作的一些非常基本的东西。

【问题讨论】:

    标签: c# windows-8 windows-runtime windows-store-apps


    【解决方案1】:

    我认为流会在读取数据时对其进行解压缩,这意味着流无法知道解压缩后的长度。调用 entry.Length 应该返回您可以使用的正确大小值。也可以调用 entry.CompressedLength 来获取压缩后的大小。

    【讨论】:

      【解决方案2】:

      只需将流复制到文件或另一个流中:

      using (var fs = await file.OpenStreamForWriteAsync())
      {
          using (var src = entry.Open())
          {
              var buffLen = 1024;
              var buff = new byte[buffLen];
              int read;
              while ((read = await src.ReadAsync(buff, 0, buffLen)) > 0)
              {
                  await fs.WriteAsync(buff, 0, read);
                  await fs.FlushAsync();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-05-03
        • 1970-01-01
        • 2011-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 2013-06-18
        • 1970-01-01
        相关资源
        最近更新 更多