PowerShell を使用して zip アーカイブを作成することはできますか?
23 に答える
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 したいファイルを含むディレクトリへのフル パスを渡すだけです。
CodePlex にアクセスしてPowerShell Community Extensionswrite-zip
を取得すると、そのコマンドレットを使用できます。
以来
CodePlex は、シャットダウンの準備のために読み取り専用モードになっています
PowerShell ギャラリーに移動できます。
最新の .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") ;
前述のように、完全に機能がないため、上書きフラグは期待しないでください。
更新: 長年にわたってこれを拡張してきた他の開発者については、以下を参照してください...
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"
編集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 を使用するには、構成ファイルにサポートされていない 微調整が必要です。
なぜ誰もドキュメントを見ないのですか?誰もが参照している同じ .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 アーカイブの操作方法の概要を次に示します (後でファイルを閉じることを忘れないでください)。
- の 4 番目のパラメーターを指定することで、ファイルを圧縮できます
CreateEntryFromFile(...)
。 - エントリを作成すると、 が返されます
ZipArchiveEntry
。このオブジェクトを使用すると、後で zip ファイルを検査できます。これには、レポート.CompressedLength
、表示または変更.LastWriteTime
(ニーズUpdate
モード) などが含まれます。 - 後で ZIP アーカイブを検査する必要がある場合は、その
.Entries
プロパティにアクセスし、上記のメソッドを使用して、filename、 full path、decompressed sizeを表示するか、ファイルを削除できます(needsUpdate
モード)。 - 後で 2 つの方法でアーカイブを抽出できます。最初にそれを開き、アーカイブ全体または個々のエントリを (
.Entries
またはから)抽出します.GetEntry(...)
。ファイル名だけでアーカイブを抽出することもできます。 - ストリームを操作する必要がある場合は、空のエントリを作成し、そのストリームを開いて後で書き込むことができます。既存の zip エントリを (
.Entries
またはから.GetEntry(...)
) 変更することもできます。これにより、メモリ内ですべてを実行できます。
ドキュメントを参照することをお勧めします。これがすべてを見つけた方法だからです。
どう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()
圧縮には、ライブラリを使用します ( Michal が示唆するように 7-Zip が適しています)。
7-Zipをインストールすると、インストールされたディレクトリに7z.exe
コンソール アプリケーションが含まれます。
直接呼び出して、必要な圧縮オプションを使用できます。
DLL を使用したい場合は、それも可能です。
7-Zip はフリーウェアでオープンソースです。
これは本当にあいまいですが、機能します。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
}
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 するファイルを含むディレクトリへのフル パス。
これは、ソースフォルダーからすべてのファイルを圧縮し、宛先フォルダーに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.."
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 はアーカイブ内に完全なディレクトリ パスを作成しません。
これは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)
}
これは、一時フォルダーを使用せずに、この 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()
}
}
}
このスニペットを使用して、まだ圧縮されていないバックアップ ファイルがないかデータベース バックアップ フォルダーを確認し、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 経由でバックアップ、圧縮、および転送できます。