【问题标题】:How to decompress zip with password in windows 8 metro program?如何在 Windows 8 Metro 程序中使用密码解压 zip?
【发布时间】:2012-10-18 09:17:26
【问题描述】:

我有一个 zip 密码文件并且知道这个密码。

我需要在 Windows 8 Metro 应用程序代码中打开这个 zip 文件。

但不支持“System.IO.Compression.ZipArchive”在 Windows 8 Metro 应用程序代码中使用密码解压缩 zip。

我正在尝试 SharpZipLib 和 DotNetZip。但它们不支持 net 4.5。所以我没有在我的 Metro 程序代码中使用它们。

我正在尝试 Ionic.Zip。在程序代码中没问题。我想构建包以上传到 Windows 商店。但不能通过 microsoft code review。

还有其他方法吗? 非常感谢

【问题讨论】:

    标签: c# windows-8 .net-4.5


    【解决方案1】:

    System.IO.Compression.FileSystem 程序集不适用于 Windows 应用商店应用,因此您不能使用 ZipFileExtensions classExtractToDirectory 扩展方法。

    使用 StorageFile 代替 DirectoryInfo、FileInfo 等。有关如何在 Metro 风格应用程序中读取和写入文件的更多信息,请参阅 Accessing data and filesFile access sample。然后,您需要将文件中的数据读取到流中,然后将其传递给以下类之一的方法(您的选择):

    【讨论】:

      【解决方案2】:

      您可以使用https://sharpcompress.codeplex.com/。 它支持打开文件 zip 有密码

      代码如下

      //如果文件zip有一个文件pdf,一个文件xml

      async void Read(StorageFile 文件) {

              MemoryStream memoryFilePDf = new MemoryStream();
      
              MemoryStream memoryFileXml = new MemoryStream();
      
              FilePdf = null;
      
              FileXml = null;
      
      
              using (var zipStream = await file.OpenStreamForReadAsync())
              {
                  using (MemoryStream zipMemoryStream = new MemoryStream((int)zipStream.Length))
                  {
                      await zipStream.CopyToAsync(zipMemoryStream);
                      try
                      {
                          using (var archive = ZipArchive.Open(zipMemoryStream, PassWord))
                          {
                              bool isFilePdf = false;
                              foreach (var entry in archive.Entries)
                              {
                                  if (!entry.Key.ToLower().EndsWith(".pdf") && !entry.Key.ToLower().EndsWith(".xml"))
                                  {
                                      continue;
                                  }
      
                                  if (entry.Key.ToLower().EndsWith(".pdf"))
                                  {
                                      isFilePdf = true;
                                      entry.WriteTo(memoryFilePDf);
                                  }
                                  else
                                  {
                                      isFilePdf = false;
                                      entry.WriteTo(memoryFileXml);
                                  }
      
                                  var fileName = entry.Key.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
                                  var createFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
      
                                  using (IRandomAccessStream stream = await createFile.OpenAsync(FileAccessMode.ReadWrite))
                                  {
                                      // Write compressed data from memory to file
                                      using (Stream outstream = stream.AsStreamForWrite())
                                      {
                                          byte[] buffer = isFilePdf ? memoryFilePDf.ToArray() : memoryFileXml.ToArray();
                                          outstream.Write(buffer, 0, buffer.Length);
                                          outstream.Flush();
                                      }
                                  }
      
                                  if (isFilePdf)
                                  {
                                      FilePdf = createFile;
                                  }
                                  else
                                  {
                                      FileXml = createFile;
                                  }
      
                              }
                          }
                      }
                      catch (Exception ex)
                      {
                          throw new Exception(ex.Message);
                      }
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-04
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多