本文介绍了System.IO.Compression.ZipFile多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我为 $ destination 设置了一个数组,则可以使它工作,但这会为所选的每个目录创建一个单独的.zip。

I can get this to work if I set up an array for my $destination but this creates a seperate .zip for each directory selected.

如何选择每个目录并将其压缩在单个目标而不是多个目标文件下?这是我的代码:

How can I select every directory and zip them under a single destination instead of multiple destination files? Here is my code:

$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 选项是一个单一目录还是多个目录,我可以选择它们并将它们存储为数组。

I want to keep my Out-GridView option the way it is so if it's a single directory or multiple I can select them and store them as an array.

推荐答案

-update 参数将确保使用从中选择的新条目来更新现有归档文件。 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

这篇关于System.IO.Compression.ZipFile多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 23:24