263

PowerShell を使用して zip アーカイブを作成することはできますか?

4

23 に答える 23

258

PowerShell 3 および .NET 4.5 で動作する純粋な PowerShell の代替 (使用できる場合):

function ZipFiles( $zipfilename, $sourcedir )
{
   Add-Type -Assembly System.IO.Compression.FileSystem
   $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
   [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
        $zipfilename, $compressionLevel, $false)
}

作成したい zip アーカイブへのフル パスと、zip したいファイルを含むディレクトリへのフル パスを渡すだけです。

于 2012-11-09T05:26:45.090 に答える
125

CodePlex にアクセスしてPowerShell Community Extensionswrite-zipを取得すると、そのコマンドレットを使用できます。

以来

CodePlex は、シャットダウンの準備のために読み取り専用モードになっています

PowerShell ギャラリーに移動できます。

于 2009-07-20T12:01:23.943 に答える
57

最新の .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") ;

前述のように、完全に機能がないため、上書きフラグは期待しないでください。

更新: 長年にわたってこれを拡張してきた他の開発者については、以下を参照してください...

于 2014-06-11T20:53:53.347 に答える
46

7zip をインストール (または代わりにコマンド ライン バージョンをダウンロード) し、次の PowerShell メソッドを使用します。

function create-7zip([String] $aDirectory, [String] $aZipfile){
    [string]$pathToZipExe = "$($Env:ProgramFiles)\7-Zip\7z.exe";
    [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory", "-r";
    & $pathToZipExe $arguments;
}

次のように呼び出すことができます。

create-7zip "c:\temp\myFolder" "c:\temp\myFolder.zip"
于 2011-04-20T16:18:29.883 に答える
15

編集2 - このコードは、昔からの醜い、醜いクルーゲです。君はそれを欲しがっていない。

これは、ここの例に従って System.IO.Packaging.ZipPackageを使用.\inして to の内容を圧縮します.\out.zip

$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()

編集: 大きなファイル、おそらく >10mb、YMMV では信頼できません。appdomain の証拠と分離ストレージと関係があります使いやすい .NET 4.5アプローチは PS v3 からうまく機能しますが、私の場合はより多くのメモリが必要でした。PS v2 から .NET 4 を使用するには、構成ファイルにサポートされていない 微調整が必​​要です。

于 2012-10-19T16:01:20.523 に答える
9

なぜ誰もドキュメントを見ないのですか?誰もが参照している同じ .NET 4.5 ライブラリを使用すると、空の ZIP を作成したり、そこに個々のファイルを追加したりするなど、好きなことを何でも実行できます。

コード例については、以下を参照してください。

# Load the .NET assembly
Add-Type -Assembly 'System.IO.Compression'
Add-Type -Assembly 'System.IO.Compression.FileSystem'

# Must be used for relative file locations with .NET functions instead of Set-Location:
[System.IO.Directory]::SetCurrentDirectory('.\Desktop')

# Create the zip file and open it:
$z = [System.IO.Compression.ZipFile]::Open('z.zip', [System.IO.Compression.ZipArchiveMode]::Create)

# Add a compressed file to the zip file:
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($z, 't.txt', 't.txt')

# Close the file
$z.Dispose()

zip アーカイブの操作方法の概要を次に示します (後でファイルを閉じることを忘れないでください)。

ドキュメントを参照することをお勧めします。これがすべてを見つけた方法だからです。

于 2018-09-18T21:45:41.383 に答える
8

どうSystem.IO.Packaging.ZipPackageですか?

.NET 3.0 以上が必要です。

#Load some assemblys. (No line break!)
[System.Reflection.Assembly]::Load("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

#Create a zip file named "MyZipFile.zip". (No line break!)
$ZipPackage=[System.IO.Packaging.ZipPackage]::Open("C:\MyZipFile.zip",
   [System.IO.FileMode]"OpenOrCreate", [System.IO.FileAccess]"ReadWrite")

#The files I want to add to my archive:
$files = @("/Penguins.jpg", "/Lighthouse.jpg")

#For each file you want to add, we must extract the bytes
#and add them to a part of the zip file.
ForEach ($file In $files)
{
   $partName=New-Object System.Uri($file, [System.UriKind]"Relative")
   #Create each part. (No line break!)
   $part=$ZipPackage.CreatePart($partName, "",
      [System.IO.Packaging.CompressionOption]"Maximum")
   $bytes=[System.IO.File]::ReadAllBytes($file)
   $stream=$part.GetStream()
   $stream.Write($bytes, 0, $bytes.Length)
   $stream.Close()
}

#Close the package when we're done.
$ZipPackage.Close()

アンダース・ヘッセルボム経由

于 2010-10-20T09:21:55.343 に答える
7

圧縮には、ライブラリを使用します ( Michal が示唆するように 7-Zip が適しています)。

7-Zipをインストールすると、インストールされたディレクトリに7z.exeコンソール アプリケーションが含まれます。
直接呼び出して、必要な圧縮オプションを使用できます。

DLL を使用したい場合は、それも可能です。
7-Zip はフリーウェアでオープンソースです。

于 2009-07-20T12:17:57.417 に答える
4

これは本当にあいまいですが、機能します。7za.exeは 7zip のスタンドアロン バージョンであり、インストール パッケージで利用できます。

# get files to be send
$logFiles = Get-ChildItem C:\Logging\*.* -Include *.log | where {$_.Name -match $yesterday} 

foreach ($logFile in $logFiles)
{
    Write-Host ("Processing " + $logFile.FullName)

    # compress file
    & ./7za.exe a -mmt=off ($logFile.FullName + ".7z") $logFile.FullName

}
于 2009-07-20T12:05:24.527 に答える
4
function Zip-File
    {
    param (
    [string]$ZipName,
    [string]$SourceDirectory 

    )
       Add-Type -Assembly System.IO.Compression.FileSystem
       $Compress = [System.IO.Compression.CompressionLevel]::Optimal
       [System.IO.Compression.ZipFile]::CreateFromDirectory($SourceDirectory,
            $ZipName, $Compress, $false)
    }

注:
ZipName: 作成する Zip ファイルのフル パス。

SourceDirectory: zip するファイルを含むディレクトリへのフル パス。

于 2016-10-24T05:37:51.080 に答える
4

これは、ソースフォルダーからすべてのファイルを圧縮し、宛先フォルダーにzipファイルを作成する作業コードです。

    $DestZip="C:\Destination\"
    $Source = "C:\Source\"

    $folder = Get-Item -Path $Source

    $ZipTimestamp = Get-Date -format yyyyMMdd-HHmmss;
    $ZipFileName  = $DestZip + "Backup_" + $folder.name + "_" + $ZipTimestamp + ".zip" 

    $Source

    set-content $ZipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    # Wait for the zip file to be created.
    while (!(Test-Path -PathType leaf -Path $ZipFileName))
    {    
        Start-Sleep -Milliseconds 20
    } 
    $ZipFile = (new-object -com shell.application).NameSpace($ZipFileName)

    Write-Output (">> Waiting Compression : " + $ZipFileName)       

    #BACKUP - COPY
    $ZipFile.CopyHere($Source) 

    $ZipFileName
    # ARCHIVE

    Read-Host "Please Enter.."
于 2016-03-21T19:38:46.323 に答える
3

WinRAR がインストールされている場合:

function ZipUsingRar([String] $directory, [String] $zipFileName)
{
  Write-Output "Performing operation ""Zip File"" on Target ""Item: $directory Destination:"
  Write-Output ($zipFileName + """")
  $pathToWinRar = "c:\Program Files\WinRAR\WinRar.exe";
  [Array]$arguments = "a", "-afzip", "-df", "-ep1", "$zipFileName", "$directory";
  & $pathToWinRar $arguments;
}

引数の意味: afzip は zip アーカイブを作成し、df はファイルを削除し、ep1 はアーカイブ内に完全なディレクトリ パスを作成しません。

于 2014-10-06T10:12:52.187 に答える
3

これはsonjzの回答のわずかに改善されたバージョンであり、上書きオプションが追加されています。

function Zip-Files(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$false)]
        [string] $zipfilename,
        [Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$false)]
        [string] $sourcedir,
        [Parameter(Position=2, Mandatory=$false, ValueFromPipeline=$false)]
        [bool] $overwrite)

{
   Add-Type -Assembly System.IO.Compression.FileSystem
   $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal

    if ($overwrite -eq $true )
    {
        if (Test-Path $zipfilename)
        {
            Remove-Item $zipfilename
        }
    }

    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $false)
}
于 2015-04-10T08:29:44.730 に答える
3

これは、一時フォルダーを使用せずに、この StackOverflow answerから C# から変換されたネイティブ .Net 4.5 を使用せずに、単一のファイルを圧縮する場合にも機能するはずです。hereから取得したより適切な using 構文を使用します。

使用法:

ZipFiles -zipFilename output.zip -sourceFile input.sql -filename name.inside.zip.sql

コード:

function ZipFiles([string] $zipFilename, [string] $sourceFile, [string] $filename)
{
    $fullSourceFile = (Get-Item -Path "$sourceFile" -Verbose).FullName
    $fullZipFile = (Get-Item -Path "$zipFilename" -Verbose).FullName

    Add-Type -AssemblyName System.IO
    Add-Type -AssemblyName System.IO.Compression
    Add-Type -AssemblyName System.IO.Compression.FileSystem

    Using-Object ($fs = New-Object System.IO.FileStream($fullZipFile, [System.IO.FileMode]::Create)) {
         Using-Object ($arch = New-Object System.IO.Compression.ZipArchive($fs, [System.IO.Compression.ZipArchiveMode]::Create)) {
             [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($arch, $fullSourceFile, $filename)
        }
    }
}

使用:

function Using-Object
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowEmptyCollection()]
        [AllowNull()]
        [Object]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [scriptblock]
        $ScriptBlock
    )

    try
    {
        . $ScriptBlock
    }
    finally
    {
        if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
        {
            $InputObject.Dispose()
        }
    }
}
于 2016-09-20T01:08:38.337 に答える
2

このスニペットを使用して、まだ圧縮されていないバックアップ ファイルがないかデータベース バックアップ フォルダーを確認し、7-Zip を使用してそれらを圧縮し、最後に*.bakファイルを削除してディスク領域を節約します。一部のファイルが圧縮されないように、ファイルは圧縮前に長さ (最小から最大) で並べられることに注意してください。

$bkdir = "E:\BackupsPWS"
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'

get-childitem -path $bkdir | Sort-Object length |
where
{
    $_.extension -match ".(bak)" -and
    -not (test-path ($_.fullname -replace "(bak)", "7z"))
} |
foreach
{
    $zipfilename = ($_.fullname -replace "bak", "7z")
    Invoke-Expression "$7Zip a $zipfilename $($_.FullName)"
}
get-childitem -path $bkdir |
where {
    $_.extension -match ".(bak)" -and
   (test-path ($_.fullname -replace "(bak)", "7z"))
} |
foreach { del $_.fullname }

ここでは、PowerShell スクリプトをチェックして、これらのファイルを FTP 経由でバックアップ、圧縮、および転送できます

于 2012-05-11T19:51:56.900 に答える