0

我需要在 zip 文件中包含空目录和文件。我可以使用 7-Zip 手动完成这项工作,但我想自动化它,因为我经常这样做。我最近开始学习 powershell,所以我决定试一试。

我的问题是Compress-Archive自动丢弃空目录。我的解决方法是($Files是脚本的参数):

$items = Get-ChildItem -Path . | Where-Object { $_.Name -in $Files }
$placeholders = @()
foreach ($item in $items) {
    if (($item | Get-ChildItem | Measure-Object).Count -eq 0 ) {
        $placeholders += (New-Item -Path "$item\.placeholder")
    }
}

在脚本的最后

foreach ($item in $placeholders) {
    $item.Delete() 
}

这可行,但它并不漂亮,因为它导致占位符文件位于最终的 zip 中。

有没有在powershell中压缩空目录的好方法?

使用底部的版本信息编辑整个脚本:

[CmdletBinding()]
param (
    # Files and folders to compress, comma separated
    [Parameter(Mandatory)]
    [string[]]
    $Files,

    # zip file to create
    [Parameter(Mandatory)]
    [string]
    $ZipName
)

if (-not $ZipName.Contains(".zip")) {
    $ZipName += ".zip"
}

$items = Get-ChildItem -Path . | Where-Object { $_.Name -in $Files }
$placeholders = @()
foreach ($item in $items) {
    if (($item | Get-ChildItem | Measure-Object).Count -eq 0 ) {
        $placeholders += (New-Item -Path "$item\.placeholder")
    }
}

if ((Get-ChildItem -Path . | Where-Object { $_.Name -eq $ZipName } | Measure-Object).Count -ne 0) {
    Remove-Item -Path "$ZipName"
}

$items | Compress-Archive -DestinationPath $ZipName

foreach ($item in $placeholders) {
    $item.Delete() 
}

# output of Get-Host
# Name             : ConsoleHost
# Version          : 5.1.19041.610
# InstanceId       : c799930e-ea5e-4ec9-9e5d-41d949bf4ee4
# UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
# CurrentCulture   : en-GB
# CurrentUICulture : en-GB
# PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
# DebuggerEnabled  : True
# IsRunspacePushed : False
# Runspace         : System.Management.Automation.Runspaces.LocalRunspace

EDIT2很奇怪的东西。再次测试它,我发誓它对我不起作用。如果我给它空目录的名称,它甚至不会创建一个 zip 文件。我的新ssd一到,我就会重新安装Windows,也许可以解决它。

顺便说一句,我需要这个用于 Wordpress 插件开发,因为您必须将插件上传到 zip 文件中。我上传了我用这个脚本创建的档案,它产生了一个非常奇怪的结果。而不是像 Wordpress 那样每次都正确解压缩 zip,然后它所做的是这样的:

some\file\which\should\be\in\a\directory.php
weird\file\again.php
normal.php

不,那些不是路径,它们是文件名。在 Windows 上,我可以很好地解压缩它。我感到很困惑。

4

1 回答 1

0

正如 Doug Maurer 所说,在 PS 中压缩空文件夹时没有问题。我也对你的功能进行了测试,没有发现任何问题。我可以毫无问题地压缩空文件夹:

PS /home/zen/Documents/test> gci -Recurse ./emptyfolderIpromise/


    Directory: /home/zen/Documents/test/emptyfolderIpromise

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           3/18/2021  6:53 PM                emptyfolderinsideemptyfolder

    Directory: /home/zen/Documents/test/emptyfolderIpromise/emptyfolderinsideemptyfolder

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           3/18/2021  6:53 PM                insideemptyfolder

    Directory: /home/zen/Documents/test/emptyfolderIpromise/emptyfolderinsideemptyfolder/insideemptyfolder

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           3/18/2021  6:53 PM                insideeeee

PS /home/zen/Documents/test> superCompresser -Files emptyfolderIpromise -ZipName myemptycompressedfolder

PS /home/zen/Documents/test> gi ./myemptycompressedfolder.zip                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Directory: /home/zen/Documents/test                                                                                                                                                                                                                                                                                                                                                                   
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----           3/18/2021  6:54 PM            638 myemptycompressedfolder.zip

PS /home/zen/Documents/test> remove-item ./emptyfolderIpromise/ -Recurse -Force

PS /home/zen/Documents/test> gi ./emptyfolderIpromise/                         

Get-Item: Cannot find path '/home/zen/Documents/test/emptyfolderIpromise/' because it does not exist.

PS /home/zen/Documents/test> Expand-Archive ./myemptycompressedfolder.zip

Recurse   ReadOnly  PS /home/zen/Documents/test> gci ./myemptycompressedfolder -Recurse                                                                                                                                  


    Directory: /home/zen/Documents/test/myemptycompressedfolder

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           3/18/2021  6:56 PM                emptyfolderIpromise

    Directory: /home/zen/Documents/test/myemptycompressedfolder/emptyfolderIpromise

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           3/18/2021  6:56 PM                emptyfolderinsideemptyfolder

    Directory: /home/zen/Documents/test/myemptycompressedfolder/emptyfolderIpromise/emptyfolderinsideemptyfolder

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           3/18/2021  6:56 PM                insideemptyfolder

    Directory: /home/zen/Documents/test/myemptycompressedfolder/emptyfolderIpromise/emptyfolderinsideemptyfolder/insideemptyfolder

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           3/18/2021  6:56 PM                insideeeee

PS /home/zen/Documents/test> 
于 2021-03-18T21:59:59.317 回答