Jud*_*Way 2 xml powershell connection-string
我正在尝试更新从Excel文档创建的zip文件的内容。我想替换\ zipfile \ xl \ connections.xml的某些内容。
此部分脚本将列出zip文件的内容:
$shell_app = new-object -com shell.application
$zip = “$destination\exceltemplates\Templates\Template1.xlsx.zip”
$zip_file=$shell_app.NameSpace($zip)
$zip_file.Items() | Select Path
Run Code Online (Sandbox Code Playgroud)
但是我尝试过的每种更新方法都产生了一个错误。访问和更新zip文件中的文件的下一步需要做什么?
小智 5
在搜索时,这是我发现的一个问题,所以我想我会冒险回答。在这个通用脚本中,没有使用 VB 的调用,而是使用 System.IO.Compression.FileSystem 的 dotnet 导入。希望这可以帮助某人。谁知道呢,未来甚至可能是我!哈哈
# The zip file to be updated
$file = Get-ChildItem ~\file.zip
# Load ZipFile (Compression.FileSystem) if necessary
try { $null = [IO.Compression.ZipFile] }
catch { [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') }
# Open zip file with update mode (Update, Read, Create -- are the options)
try { $fileZip = [System.IO.Compression.ZipFile]::Open( $file, 'Update' ) }
catch { throw "Another process has locked the '$file' file." }
# Finding the specific file within the zip file
<#
NOTE: These entries have the directories separated with a forward slash (/) instead of MS convention
of a backward slash (\). Even though this is regex it seems the forward slash does not need to be escaped (\/).
NOTE2: Because this is regex '.*' must be used instead of a simple '*'
#>
$fileZip.Entries | Where-Object { $_.FullName -match '/subdir/.*/desiredfile.xml' }
# If needed, read the contents of specific file to $text and release the file so to use streamwriter later
$desiredFile = [System.IO.StreamReader]($fileZip.Entries | Where-Object { $_.FullName -match '/subdir/.*/desiredfile.xml' }).Open()
$text = $desiredFile.ReadToEnd()
$desiredFile.Close()
$desiredFile.Dispose()
# If needed, manipulate $text however for the update
$text = $text -replace '\n', [char]30
# Re-open the file this time with streamwriter
$desiredFile = [System.IO.StreamWriter]($fileZip.Entries | Where-Object { $_.FullName -match '/subdir/.*/desiredfile.xml' }).Open()
# If needed, zero out the file -- in case the new file is shorter than the old one
$desiredFile.BaseStream.SetLength(0)
# Insert the $text to the file and close
$desiredFile.Write($text -join "`r`n")
$desiredFile.Flush()
$desiredFile.Close()
# Write the changes and close the zip file
$fileZip.Dispose()
Run Code Online (Sandbox Code Playgroud)
这不是很复杂。使用PowerShell v3.0或更高版本(以及标准的.NET System.IO库)更加容易。
# Parameters
$zipfileName = "E:\temp\WebsitePackage.zip"
$fileToEdit = "robots.txt"
$contents = "User-agent: *
Disallow: /"
# Open zip and find the particular file (assumes only one inside the Zip file)
Add-Type -assembly System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::Open($zipfileName,"Update")
$robotsFile = $zip.Entries.Where({$_.name -eq $fileToEdit})
# Update the contents of the file
$desiredFile = [System.IO.StreamWriter]($robotsFile).Open()
$desiredFile.BaseStream.SetLength(0)
$desiredFile.Write($contents)
$desiredFile.Flush()
$desiredFile.Close()
# Write the changes and close the zip file
$zip.Dispose()
Write-Host "zip file updated"
Run Code Online (Sandbox Code Playgroud)
下一个问题是如何快速检查更改是否成功?对脚本进行简单的修改即可使您读取Zip文件中的文件内容:
# Parameters
$zipfileName = "E:\temp\WebsitePackage.zip"
$fileToRead = "robots.txt"
# Open zip and find the particular file (assumes only one inside the Zip file)
Add-Type -assembly System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::Open($zipfileName,"Update")
$robotsFile = $zip.Entries.Where({$_.name -eq $fileToRead})
# Read the contents of the file
$desiredFile = [System.IO.StreamReader]($robotsFile).Open()
$text = $desiredFile.ReadToEnd()
# Output the contents
$text
$desiredFile.Close()
$desiredFile.Dispose()
# Close the zip file
$zip.Dispose()
Run Code Online (Sandbox Code Playgroud)
本文提供了有用的背景材料:https : //mcpmag.com/articles/2014/09/29/file-frontier-part-6.aspx
我不得不在压缩的 nuget 包中替换版本 xml 文件 (NuGetApp1.nuspec),如下所示:
zip 中的原始文件内容:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>NuGetApp1</id>
<version>1.1.3-dev22222</version>
<title>NuGetApp1</title>
.....
Run Code Online (Sandbox Code Playgroud)
必需的 :
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>NuGetApp1</id>
<version>1.0.0.0</version>
<title>NuGetApp1</title>
.....
Run Code Online (Sandbox Code Playgroud)
Powershell 脚本:
$replaceWithVersion="1.0.0.0"
$path = "c:\temp\testnuget"
$files = Get-ChildItem *.nupkg -Path $path
foreach($fileNuget in $files)
{
$target = $fileNuget.FullName -replace "[0-9]+(\.([0-9]+|\*)){1,4}", $replaceWithVersion
Copy-Item $fileNuget.FullName -Destination $target
$zipfileName = $target
$fileToEdit = "*.nuspec"
# Open zip and find the particular file (assumes only one inside the Zip file)
$zip = [System.IO.Compression.ZipFile]::Open($zipfileName,"Update")
$nuspecFile = $zip.Entries.Where({$_.name -like $fileToEdit})
# Read the contents of the file
$desiredFile = [System.IO.StreamReader]($nuspecFile).Open()
$text = $desiredFile.ReadToEnd()
$desiredFile.Close()
$desiredFile.Dispose()
$text = $text -replace '<version>[\s\S]*?<\/version>',"<version>$replaceWithVersion</version>"
#update file with new content
$desiredFile = [System.IO.StreamWriter]($nuspecFile).Open()
$desiredFile.BaseStream.SetLength(0)
# Insert the $text to the file and close
$desiredFile.Write($text)
$desiredFile.Flush()
$desiredFile.Close()
# Write the changes and close the zip file
$zip.Dispose()
Write-Host "zip file updated"
}
Run Code Online (Sandbox Code Playgroud)
狩猎快乐:-)!
| 归档时间: |
|
| 查看次数: |
4588 次 |
| 最近记录: |