使用powershell下载和解压缩文件,而不会丢失字符编码

时间:2014-09-05 17:30:04

标签: shell powershell character-encoding

我已经编写了ps脚本来从服务器下载zip并在本地解压缩,只要压缩文件不包含任何特殊字符,脚本就可以正常工作。
但是当服务器中的某些文件解压后显示某些字符(如“àòèéù”)时,或者某些文件不是原始数据。 如果我使用winrar或其他工具提取相同的zip,我可以看到orignal字符集,因为它是预期的 请帮助我理解或解决这个问题如何在使用power shell脚本解压缩时保持orignal字符。

$client = New-Object "System.Net.WebClient"
#$client.Headers.add("Authorization",$CookieContainer.GetCookieHeader($token))
$client.Headers.add("Authorization",$token)

#$client.UseDefaultCredentials = $true
#$client.Credentials = Get-Credential
try
{
"Downloading"
 $client.DownloadFile([string]$url,[string]$documents_path)

"Finished Download"

"Unzipping Zip"
"$documents_path"

$zipPackage = (new-object -com shell.application).NameSpace($documents_path)
$destinationFolder = (new-object -com shell.application).NameSpace($target_path)
$destinationFolder.CopyHere($zipPackage.Items(),16)
"$target_path"
"Unzipping Zip Done"

}

更新
它只发生在没有编码信息的xml文件中,我如何在提取时或提取后对此xml文件进行编码。

1 个答案:

答案 0 :(得分:0)

您用来读取文件的软件可能正在尝试以与保存时不同的编码格式读取它。例如,Excel通常会以ANSI格式打开Windows用户的文件,如WIN-1252即使你用UTF-8写的。您可以使用iconv等实用程序(取决于您所使用的平台)将文件从一种格式转换为另一种格式。

相关问题