2014-08-27 131 views
2

我試圖更新從Excel文檔創建的zip文件的內容。我想替換\ zipfile \ xl \ connections.xml的一些內容。使用Powershell編輯子文件夾中的zip文件內容

這部分的腳本將列出的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 

但我想盡更新方法產生了一個錯誤。下一步需要訪問和更新zip文件中的文件?

+0

我從來沒有到ZIP操縱與PS,但是你能分享你收到的錯誤嗎?這會讓問題更加完整 – Koliat 2014-08-28 08:56:22

+0

當我添加如下內容時: – 2014-08-28 13:48:28

+0

當我添加如下內容:foreach($ item in $ zip.items()) { \t(Get-Content $ connstring)| Foreach-Object {$ _ -replace「\ {DatabaseServer \}」,$ sqlDatabase} | Set-Content $ connstring }我得到「你不能調用一個空值表達式的方法」錯誤。我想編輯壓縮文件中子文件夾中的文件。 – 2014-08-28 13:54:46

回答

3

當搜索,這是我發現的問題,所以我想我會冒昧回答。在這個通用腳本中,不使用VB的調用,而是使用System.IO.Compression.FileSystem的dotnet導入。希望這可以幫助某人。誰知道,未來甚至可能是我! LOL

# 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() 
4

這不是很複雜。使用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" 

接下來的問題是如何快速檢查您的更改是否成功?腳本的一個簡單的適應可以讀取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() 

有有用的背景材料,在這篇文章:https://mcpmag.com/articles/2014/09/29/file-frontier-part-6.aspx

+0

除PowerShell 3.0+外,System.IO.Compression.ZipFile還需要.Net 4.5+。 – Chris 2016-12-16 17:22:35

相關問題