2009-07-20 115 views

回答

105

如果您轉到CodePlex並獲取PowerShell Community Extensions,則可以使用它們的write-zip cmdlet。

由於

CodePlex上是隻讀的準備模式關機

你可以去PowerShell Gallary

+92

是的,它使用7Z爲核心庫的大多數壓縮的cmdlet我知道,我becaues實現它。)+1 – x0n 2009-07-21 01:10:34

+1

笑不錯的工作,x0n。我imlpemented在PSCX飼料存儲提供稍微不太實用,但噸樂趣:) – 2009-07-21 02:37:23

+1

如果它使用7Z,是否有可能使用密碼壓縮 – mack 2013-04-17 20:20:27

3

這確實很晦澀,但很有效。 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 

} 
6

對於壓縮,我會使用一個庫(7-Zip好像Michal suggests)。

如果您安裝了7-Zip,則安裝的目錄將包含7z.exe這是一個控制檯應用程序。
你可以直接調用它並使用你想要的任何壓縮選項。

如果你想使用DLL,那也應該是可以的。
7-Zip是免費和開源的。

+2

下面是使用Powershell的AES加密使用7 zip的示例:http ://codeblog.theg2.net/2010/02/powershell-7-zip-amazon-s3-upload.html – 2010-02-19 23:22:44

4

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, "application/zip", 
     [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() 

通過Anders Hesselbom

+2

10有完整源代碼的任何示例? – Kiquenet 2012-06-08 08:25:13

37

安裝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" 
+6

如果7zip的是在您的路徑,那麼所有你需要寫的是「&7z格式C:\ TEMP \ MyFolder文件C:\ TEMP \ myFolder.zip」 – aboy021 2013-06-24 02:17:34

1

我使用這個片段檢查我的數據庫備份文件夾中尚未壓縮的備份文件,使用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 script to backup, compress and transfer those files over FTP

11

編輯兩個 - 這段代碼是一個醜陋,醜陋的克魯日從古代的日子。你不想要。

使用System.IO.Packaging將.\in的內容壓縮爲.\out.zip。ZipPackage下面的例子here

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

編輯:Unreliable較大的文件,也許> 10MB,情況因人而異。 Somethingto do與appdomain證據和孤立的存儲。友好的.NET 4.5 approach從PS v3很好地工作,但在我的情況下需要更多的內存。要使用PS v2中的.NET 4,配置文件需要一個unsupportedtweak

223

純PowerShell的替代方案,使用PowerShell 3和.NET 4.5(如果你可以使用它)的工作原理:

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檔案,你想創建和包含要壓縮的文件的目錄的完整路徑。

45

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

如前所述,完全功能少,所以不要指望覆蓋標誌。

1

如果你有安裝了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不會創建歸檔中的完整目錄路徑

148

PowerShell v5.0增加了Compress-ArchiveExpand-Archive cmdlets。該鏈接的網頁有充分的例子,但它的要點是:

# Create a zip file with the contents of C:\Stuff\ 
Compress-Archive -Path C:\Stuff -DestinationPath archive.zip 

# Add more files to the zip file 
# (Existing files in the zip file with the same name are replaced) 
Compress-Archive -Path C:\OtherStuff\*.txt -Update -DestinationPath archive.zip 

# Extract the zip file to C:\Destination\ 
Expand-Archive -Path archive.zip -DestinationPath C:\Destination 
3

如果有人需要壓縮單個文件(而不是文件夾):http://blogs.msdn.com/b/jerrydixon/archive/2014/08/08/zipping-a-single-file-with-powershell.aspx

[CmdletBinding()] 
Param(
    [Parameter(Mandatory=$True)] 
    [ValidateScript({Test-Path -Path $_ -PathType Leaf})] 
    [string]$sourceFile, 

    [Parameter(Mandatory=$True)] 
    [ValidateScript({-not(Test-Path -Path $_ -PathType Leaf)})] 
    [string]$destinationFile 
) 

<# 
    .SYNOPSIS 
    Creates a ZIP file that contains the specified innput file. 

    .EXAMPLE 
    FileZipper -sourceFile c:\test\inputfile.txt 
       -destinationFile c:\test\outputFile.zip 
#> 

function New-Zip 
{ 
    param([string]$zipfilename) 
    set-content $zipfilename 
      ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    (dir $zipfilename).IsReadOnly = $false 
} 

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 $sourceFile | Add-Zip $destinationFile 
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) 
} 
9

給予低於另一個選項。這將壓縮完整的文件夾,並將檔案寫入給定名稱的給定路徑。

需要.NET 3或以上

Add-Type -assembly "system.io.compression.filesystem" 

$source = 'Source path here'  
$destination = "c:\output\dummy.zip" 

If(Test-path $destination) {Remove-item $destination} 

[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 
3

下面是工作代碼,從源文件夾壓縮和解所有文件,並創建在目標文件夾的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.." 
1

這也應該適用於壓縮單個文件,而無需使用臨時文件夾,並使用原生的.Net 4.5,從這個StackOverflow的answer從C#轉換。它使用從here採取的語法更好。

用法:

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() 
     } 
    } 
} 
3
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) 
    } 

注:
ZipNa我:你想創建的Zip文件的完整路徑。

SourceDirectory:包含要壓縮的文件的目錄的完整路徑。

2

這裏有一個完整的命令行示例,可以從cmd.exe或從ssh啓動或者您想要的!

powershell.exe -nologo -noprofile -command "&{ Add-Type -A 'System.IO.Compression.FileSystem' [System.IO.Compression.ZipFile]::CreateFromDirectory('c:/path/to/source/folder/', 'c:/path/to/output/file.zip');}" 

問候

-3

該腳本遍歷所有的目錄和zip每一個

GET-ChildItem -attributes開發|的foreach {寫-ZIP $ 請將.Name「$($請將.Name)的.zip」}

1

加載[System.IO.IOException]類和使用它的方法是爲了抑制不必要的錯誤的一個重要步驟,因爲事實上,它是一個這個類不是PowerShell的本機,所以如果沒有它,期待各種各樣的錯誤上下文。

我誤差控制我的腳本到T,但得到了很多額外的紅色,而用[System.IO.Compression.ZipFile]類的文件是否存在「輸出

function zipFiles(
    [Parameter(Position=0, Mandatory=$true] 
    [string] $sourceFolder, 
    [Parameter(Position=1, Mandatory=$true] 
    [string]$zipFileName, 
    [Parameter(Position=2, Mandatory=$false] 
    [bool]$overwrite) 

{ 
Add-Type -Assembly System.IO 
Add-Type -Assembly System.IO.Compression.FileSystem 

$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal 

$directoryTest = (Test-Path $dailyBackupDestFolder) 
$fileTest = (Test-Path $zipFileName) 

if ($directoryTest -eq $false) 
{ 
    New-Item -ItemType Directory -Force -Path $dailyBackupDestFolder 
} 

    if ($fileTest -eq $true) 
    { 
      if ($overwrite -eq $true){Remove-Item $zipFileName} 
    } 


    try 
    { 
     [System.IO.Compression.ZipFile]::CreateFromDirectory($sourceFolder,$zipFileName,$compressionLevel)  

    } 
    catch [System.IO.IOException] 
    { 
     Write-Output ($dateTime + ' | ' + $_.Exception.Message) | Out-File $logFile -append -force 
    } 
} 

我在做什麼這裏正趕上這些IO錯誤,如訪問已經存在的文件,捕獲該錯誤並將其指向我正在用更大的程序維護的日誌文件。

0

完整的命令行中的Windows命令用於壓縮和解壓目錄下的如下:

  • 壓縮:

powershell.exe -nologo -noprofile -command「& {添加類型-A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile] :: CreateFromDirectory('C:\ Indus', 'C:\ Indus。壓縮'); } 「

  • 提取:

powershell.exe -nologo -noprofile -command」 & {添加型-A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile] :: ExtractToDirectory('C:\ Indus.zip', 'C:\ Indus'); ?}」