繁体   English   中英

使用Powershell或命令行在Windows中创建压缩/压缩文件夹

[英]Creating a zipped/compressed folder in Windows using Powershell or the command line

我正在创建一个夜间数据库模式文件,并希望将每晚创建的所有文件(每个数据库一个)放入一个文件夹并压缩该文件夹。 我有一个PowerShell脚本创建架构。只有数据库的创建脚本,然后将所有文件添加到新文件夹。 问题在于该过程的压缩部分。

有没有人知道是否可以使用预先安装的处理文件夹压缩的Windows实用程序来完成此操作?

如果可能的话,最好使用该实用程序,而不是像7zip这样的东西(我不想在每个客户的服务器上安装7zip,如果我问他们可能需要IT年数才能完成)。

采用最新.NET 4.5框架的本机方式,但完全没有功能:

创建:

Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory("c:\your\directory\to\compress", "yourfile.zip") ;

萃取:

Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::ExtractToDirectory("yourfile.zip", "c:\your\destination") ;

如上所述,完全没有功能,所以不要指望覆盖标志。

这里有几个不依赖于扩展的与zip相关的函数: 使用Windows PowerShell压缩文件

您可能感兴趣的主要功能是:

function Add-Zip
{
    param([string]$zipfilename)

    if(-not (test-path($zipfilename)))
    {
        set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
        (dir $zipfilename).IsReadOnly = $false  
    }

    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($zipfilename)

    foreach($file in $input) 
    { 
            $zipPackage.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
    }
}

用法:

dir c:\demo\files\*.* -Recurse | Add-Zip c:\demo\myzip.zip

有一点需要注意:如果路径不是绝对路径,则shell.application对象的NameSpace()函数无法打开zip文件进行写入。 因此,如果您传递了Add-Zip的相对路径,它将以null错误失败,因此zip文件的路径必须是绝对的。

或者你可以在函数的开头添加$zipfilename = resolve-path $zipfilename

从PowersShell 5开始,有一个Compress-Archive cmdlet可以完成开箱即用的任务。

这将压缩。\\在内容上。\\ out.zip与System.IO.Packaging.ZipPackage下面的例子在这里

$zipArchive = $pwd.path + "\out.zip"
[System.Reflection.Assembly]::Load("WindowsBase,Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
$ZipPackage=[System.IO.Packaging.ZipPackage]::Open($zipArchive, [System.IO.FileMode]"OpenOrCreate", [System.IO.FileAccess]"ReadWrite")
$in = gci .\in | select -expand fullName
[array]$files = $in -replace "C:","" -replace "\\","/"
ForEach ($file In $files) {
   $partName=New-Object System.Uri($file, [System.UriKind]"Relative")
   $part=$ZipPackage.CreatePart($partName, "application/zip", [System.IO.Packaging.CompressionOption]"Maximum")
   $bytes=[System.IO.File]::ReadAllBytes($file)
   $stream=$part.GetStream()
   $stream.Write($bytes, 0, $bytes.Length)
   $stream.Close()
                                                    }
$ZipPackage.Close()

在PowerShell中使用了voithos对zip文件的回答,只是在Add-Zip函数中遇到了一个问题,如果文件在那段时间内无法完全压缩,则Start-sleep -milliseconds 500会导致问题 - >下一个启动在它完成之前导致错误和一些文件不被压缩。

所以在玩了一下之后,首先尝试让计数器检查$ zipPackage.Items()的计数,并且只在项目数增加后继续(这不起作用,因为它会在某些情况下返回0不应该)我发现如果包仍在压缩/复制文件,它将返回0(我想,哈哈)。 添加了一个简单的while循环,其中包含start-sleep,等待zipPackage.Items()。count继续之前为非零值,这似乎解决了这个问题。

function Add-Zip
{
param([string]$zipfilename)

if(-not (test-path($zipfilename)))
{
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipfilename).IsReadOnly = $false  
}

$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)

foreach($file in $input) 
{ 
        $zipPackage.CopyHere($file.FullName)
        do
        {
            Start-sleep -milliseconds 250
        }
        while ($zipPackage.Items().count -eq 0)
}
}

使用PowerShell 3.0版:

Copy-ToZip -File ".\blah" -ZipFile ".\blah.zip" -Force

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM