繁体   English   中英

Powershell-从.zip存档读取文件失败

[英]Powershell - Reading file from .zip archive fails

我目前正在尝试读取.zip归档文件中特定.xml文件的内容,而不提取该文件。
代码很简单,但是以某种方式将几个字节滑入缓冲区,使得无法使用文件的内容。

这是各自的代码:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
    $arch = [System.IO.Compression.ZipFile]::OpenRead("C:\file.zip")

    $entr = $arch.Entries | ?{$_.Name -like "test.xml"}
    if(!$entr)
    {throw [System.Exception] "Could not find the .xml file"}

    $buf = New-Object System.Byte[]($entr.Length)
    $entr.Open().Read($buf, 0, $entr.Length) | Out-Null

    $xml = [xml] ([System.Text.Encoding]::Unicode.GetString($buf))

我说的代码非常简单,但是可悲的是$buf的前两个字节似乎总是等于255254 ,这导致Powershell的xml解析器抛出异常。
作为一种临时的解决方法,我尝试省略了前两个字节,但这仅导致后两个字节发生相同的问题。

这就引出我的问题,缓冲区怎么可能弄乱了?
我的方法做错了吗? 我错过了什么?

任何帮助,我们将不胜感激!

更新:

嗯,好像Windows使用UTF-16作为内部编码一样,这意味着前两个字节是Byte Order Mark (BOM) 我希望GetString()方法能够识别BOM ,有人可以对此进行澄清吗?

您将需要将Stream包装到StreamReader ,然后使用ReadToEnd()方法,我希望这样做能够尊重BOM:

$reader = new-object System.IO.StreamReader($entr.Open())
$contents = $reader.ReadToEnd()
$reader.Close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM