System.IO.Compression.ZipFile 多个目录
System.IO.Compression.ZipFile Multiple Directories
如果我为我的 $destination
设置一个数组,我可以让它工作,但这会为每个目录创建一个单独的 .zip selected.
如何 select 每个目录并将它们压缩到一个目标而不是多个目标文件下?这是我的代码:
$type = "*.txt"
$destination = "LogsBackup.zip"
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
$_sources = dir c:\Logs$type -Recurse | Select Directory -Unique |
Out-GridView -OutputMode Multiple |
Select @{Name="Path";Expression={$_.Directory -As [string]}}
for ($i = 0; $i -lt $_sources.Length; $i++)
{
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory( $_sources[$i].Path , $destination)
}
我想保持我的 Out-GridView
选项不变,所以如果它是单个目录或多个目录,我可以 select 它们并将它们存储为数组。
-update
参数将确保您现有的存档更新为您 select 来自 out-gridview
的新条目。
$destination = "C:\logs\LogsBackup.zip"
#Create a single archive
Get-ChildItem -Path C:\logs -Filter *.txt -Recurse |
Select @{Name="Path";Expression={$_.DirectoryName}} -Unique |
Out-GridView -PassThru | Compress-Archive -CompressionLevel Optimal -DestinationPath $destination -Update
如果我为我的 $destination
设置一个数组,我可以让它工作,但这会为每个目录创建一个单独的 .zip selected.
如何 select 每个目录并将它们压缩到一个目标而不是多个目标文件下?这是我的代码:
$type = "*.txt"
$destination = "LogsBackup.zip"
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
$_sources = dir c:\Logs$type -Recurse | Select Directory -Unique |
Out-GridView -OutputMode Multiple |
Select @{Name="Path";Expression={$_.Directory -As [string]}}
for ($i = 0; $i -lt $_sources.Length; $i++)
{
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory( $_sources[$i].Path , $destination)
}
我想保持我的 Out-GridView
选项不变,所以如果它是单个目录或多个目录,我可以 select 它们并将它们存储为数组。
-update
参数将确保您现有的存档更新为您 select 来自 out-gridview
的新条目。
$destination = "C:\logs\LogsBackup.zip"
#Create a single archive
Get-ChildItem -Path C:\logs -Filter *.txt -Recurse |
Select @{Name="Path";Expression={$_.DirectoryName}} -Unique |
Out-GridView -PassThru | Compress-Archive -CompressionLevel Optimal -DestinationPath $destination -Update