【问题标题】:Zipping files using Powershell使用 Powershell 压缩文件
【发布时间】:2020-08-23 18:44:58
【问题描述】:

我在下面有一个脚本来压缩 folder.txt 中的内容。但我想要的是一个一个地压缩包括文件夹并放在同一个位置。

$Src = "C:\Users\Asus\Documents\PowerShell\Folder.txt"
Compress-Archive –Path (Get-Content $Src) -DestinationPath C:\Users\Asus\Documents\PowerShell

假设:

folder.txt 中的内容:

新建文件夹

新建文件夹 2

会发生什么我将在同一位置拥有新文件夹.zip 和新文件夹 2.zip (C:\Users\Asus\Documents\PowerShell)

谁能帮我解决这个问题?我有很多需要压缩但需要指定的文件夹。

如果我使用 SecureZip 时也可以输入密码(所有人的密码相同,例如“abcd”)。

谢谢!

【问题讨论】:

  • 我不知道 SecureZip 是什么(直到您的帖子才听说过),但是如果您已经在使用 3rdP Zip 工具,为什么还要使用 cmdlet?该 cmdlet 不提供应用密码的方法,因此您需要使用 3rdP zip 工具,如 7Zip、DotNetZip 等。然后您只需在循环中使用 Get-ChildItem cmdlet 来获取您的目录并将它们传递给您的 zip 工具使用工具命令开关以保护 zip。网络上有很多这样的例子。

标签: powershell zip


【解决方案1】:

继续我的评论。选择资源,根据需要进行审查和调整。

'powershell 7ip folders with password'

点击示例

PowerShell function to create a password protected zip file Download the function script

function Write-ZipUsing7Zip
{
    Param
    (
        [string]$FilesToZip, 
        [string]$ZipOutputFilePath, 
        [string]$Password, 
        [ValidateSet('7z','zip','gzip','bzip2','tar','iso','udf')]
        [string]$CompressionType = 'zip', 
        [switch]$HideWindow
    )
    # Look for the 7zip executable.
    $pathTo32Bit7Zip = "C:\Program Files (x86)\7-Zip\7z.exe"
    $pathTo64Bit7Zip = "C:\Program Files\7-Zip\7z.exe"

    $THIS_SCRIPTS_DIRECTORY = Split-Path $script:MyInvocation.MyCommand.Path
    $pathToStandAloneExe = Join-Path $THIS_SCRIPTS_DIRECTORY "7za.exe"

    if (Test-Path $pathTo64Bit7Zip) { $pathTo7ZipExe = $pathTo64Bit7Zip }
    elseif (Test-Path $pathTo32Bit7Zip) { $pathTo7ZipExe = $pathTo32Bit7Zip }
    elseif (Test-Path $pathToStandAloneExe) { $pathTo7ZipExe = $pathToStandAloneExe }
    else { throw "Could not find the 7-zip executable." }

    # Delete the destination zip file if it already exists (i.e. overwrite it).
    if (Test-Path $ZipOutputFilePath) 
    { Remove-Item $ZipOutputFilePath -Force }

    $windowStyle = "Normal"
    if ($HideWindow) { $windowStyle = "Hidden" }

    # Create the arguments to use to zip up the files.
    # Command-line argument syntax can be found at: http://www.dotnetperls.com/7-zip-examples
    $arguments = "a -t$CompressionType ""$ZipOutputFilePath"" ""$FilesToZip"" -mx9"
    if (!([string]::IsNullOrEmpty($Password))) 
    { $arguments += " -p$Password" }

    # Zip up the files.
    $p = Start-Process $pathTo7ZipExe -ArgumentList $arguments -Wait -PassThru -WindowStyle $windowStyle

    # If the files were not zipped successfully.
    if (!(($p.HasExited -eq $true) -and ($p.ExitCode -eq 0)))
    {
        throw "There was a problem creating the zip file '$ZipFilePath'."
    }
}

顺便说一句,Microsoft powershellgallery.com 中有适用于 7Zip 的 PowerShell 附加模块。

Find-Module -Name '*zip*' | Format-Table -AutoSize

# Results
<#
Version Name            Repository Description                                                                                                                        
------- ----            ---------- -----------                                                                                                                        
1.13.0  7Zip4Powershell PSGallery  Powershell module for creating and extracting 7-Zip archives                                                                       
0.3.1   Zip             PSGallery  PowerShell module for creating ZIP archives.                                                                                       
2.2.0   PS7Zip          PSGallery  Powershell module that allows you to work with compressed archives                                                                 
1.0.8   x7Zip           PSGallery  Powershell DSC Configuration Script for installing 7-Zip versions 15.07, 15.06, 15.05, 9.38, and 9.20. This Configuration Script...
1.3.4   7ZipArchiveDsc  PSGallery  PowerShell DSC Resource to expand an archive file to a specific path.                                                              
0.2.2   ziphelper       PSGallery  Tools to work with zip archives                                                                                                    
1.1.0   7zip-Archive    PSGallery  7zip utility wrapper....                                                                                                           
0.1.1   Get-GzipContent PSGallery  Gets the content of the gzip archive at the specified location.  
#>

看这篇文章就可以了。

https://www.sans.org/blog/powershell-7-zip-module-versus-compress-archive-with-encryption

【讨论】:

  • 谢谢!我会探索这个。试试这是否适用于 SecureZip
  • 不用担心。如果它确实解决了您的用例,请务必将其标记为您接受的答案。如果不回来告诉我们发生了什么。
猜你喜欢
  • 2018-05-20
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 2023-03-22
  • 2018-09-30
  • 2017-11-03
  • 2021-05-29
  • 1970-01-01
相关资源
最近更新 更多