【问题标题】:Read a filename inside a zip file without extracting it in powershell读取 zip 文件中的文件名而不在 powershell 中提取它
【发布时间】:2014-04-09 17:16:20
【问题描述】:

我有一个.zip 文件file.zip。这个 zip 文件中有很多文件,只有一个扩展名为 .txt 的文本文件。我想读取这个文本文件的名称。有什么方法可以读取名称而不在 Powershell 或 C# 中提取 zip 文件?

【问题讨论】:

  • 你有没有为此写过代码?
  • 你可以在这个类似的问题上找到答案:stackoverflow.com/questions/307774/…
  • 是的,我可以通过提取 zip 来读取文件名。但我想在不提取的情况下读取文件名
  • HABJAN 我想在不使用任何第三方库的情况下阅读它。有什么办法吗?

标签: c# .net powershell


【解决方案1】:

您可以使用 Shell.Application 对象从 zip 文件中枚举文件名:

$zip = 'C:\path\to\file.zip'

$app = New-Object -COM 'Shell.Application'
$app.NameSpace($zip).Items() | ? { $_.Name -like '*.txt' } | select -Expand Name

【讨论】:

    【解决方案2】:

    如果你有 .Net 4.5,你可以运行这样的东西

    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" );
    $zipContens = [System.IO.Compression.ZipFile]::OpenRead("C:\path\file.zip");  
    $zipContens.Entries | % {
       if ($_.Name -match "TheFileYouAreLookingFor.txt"){
           # found, your code block
       }
    }
    

    这个问题会有所帮助: Powershell to search for files based on words / phrase but also Zip files and within zip files

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多