Get-ZipFile

Synopsis

Gets and Sets an archive object from a Zip file.

Syntax

Get-ZipFile [-Path] <String[]> [-Options <SelfExtractorSaveOptions>] [-ZipErrorAction <ZipErrorAction>] [-Encryption <EncryptionAlgorithm>] [-Password <String>] [-Encoding <Encoding>] [-ProgressID <Int32>] [-List] [-SortEntries] [-UnixTimeFormat] [-WindowsTimeFormat] [<CommonParameters>]

Get-ZipFile [-Path] <String[]> [-ReadOptions <ReadOptions>] [-Options <SelfExtractorSaveOptions>] [-ZipErrorAction <ZipErrorAction>] [-Encryption <EncryptionAlgorithm>] [-Password <String>] [-List] [-SortEntries] [-UnixTimeFormat] [-WindowsTimeFormat] [<CommonParameters>]

Description

Gets an archive object from a Zip file. The archive object contains only the catalog, an objects list of type ZipEntry, in order to get the contents of a catalog entry. You need to expand it on a filesystem drive. Also, this function sets the most common properties of the object archive. The returned object is locked until you don&;39;t call the &;39;Close()&;39; method. On the other side, the use of the &;39;-List&;39; parameter does not lock the archive.

Parameters

-Path <String[]>

File name(s) to read. Can be a file names with generic characters (* , ? , [A-D] ou [1CZ]).

Required? True
Position? 1
Default value  
Accept pipeline input? true (ByValue)
Accept wildcard characters? False

-Options <SelfExtractorSaveOptions>

Options used during the creation of the self-extracting archive (See &;39;New-ZipSfxOptions&;39; for more details).

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-ZipErrorAction <ZipErrorAction>

Specifies the error mode handling.Values : Throw, Skip, Retry, InvokeErrorEvent

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-Encryption <EncryptionAlgorithm>

Encryption algorythm used for compression. Need to specify a password with the &;39;-Password&;39; parameter.Values : None, PkzipWeak, WinZipAes128, WinZipAes256, Unsupported

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-Password <String>

Password used to read the Zip archive.

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-Encoding <Encoding>

Archive encoding type. Possible values are : -ASCII : ASCII characters encoding scheme (7 bits). -BigEndianUnicode : encoding for the UTF-16 format using the big endian byte order. -Default : encoding for the operating system&;39;s current ANSI code page. -Unicode : encoding for the UTF-16 format using the little endian byte order. -UTF32 : encoding for the UTF-32 format using the little endian byte order. -UTF7 : encoding for the UTF-7. -UTF8 : encoding for the UTF-8. . For better portability, the default value (&;39;DefaultEncoding&;39;) is recommended.

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-ProgressID <Int32>

This parameter makes an events manager for read operations. When opening a zip archive with a big size, you can choose to display a default progress bar. This ID allows to distinguish the internal progress bar from the others. Use this parameter when you create more than one progress bar.

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-List <SwitchParameter>

Gets the contained entries from the archive catalog. The returned objects are custom objects with the PSZipEntry type name. Aside from its property &;39;Info&;39;, all are read-only. The archive is not locked.

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-SortEntries <SwitchParameter>

Entries are sorted.

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-UnixTimeFormat <SwitchParameter>

Unix time format for date file format will be used.

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-WindowsTimeFormat <SwitchParameter>

Windows time format for date file format will be used.

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

-ReadOptions <ReadOptions>

Options applied when reading archive (.zip). Created options from the &;39;New-ReadOptions&;39; function.

Required? False
Position? named
Default value  
Accept pipeline input? false
Accept wildcard characters? False

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

Inputs

System.String

Outputs

Ionic.Zip.ZipFile

Notes

This function returns only one Zip archive object from his fullname. Most of the parameters are used to configure the archive properties if you want to modify it. For example, the &;39;Encryption&;39; parameter does not affect reading the Zip. Only the &;39;Password&;39; parameter is needed, because this is entries that are encrypted, and not the object of type [Zipfile]. So a Zip archive can theoretically contains several entries with differents compression mode for each of them. Pay attention that for each returned object you must call the &;39;Close()&;39; method in order to properly release resources of the archive.

Examples

EXAMPLE 1

PS C:\> try {
$ZipFile=Get-Zipfile -Path Test.zip
$ZipFile
} finally {
 if ($ZipFile -ne $null )
 { $ZipFile.PSDispose() }
}

This example reads a Zip file, display its contents from a file and then releases resources archive.

EXAMPLE 2

PS C:\> try {         
$File=Dir C:\Temp\Test.ps1 
$ZipFile=Get-Zipfile -Path Test.zip
Add-ZipEntry -Object $File -ZipFile $ZipFile
$ZipFile.Save()
} finally {
 if ($ZipFile -ne $null )
 { $ZipFile.PSDispose() }
}

This example reads a Zip file, adds an entry from a file to it, saves it and then releases resources archive.

EXAMPLE 3

PS C:\> try {         
$Zip=Get-Zipfile -Path Test.zip
 $ofs="`r`n"
 [string]$Text=Get-Content C:\Temp\Test.ps1
Add-ZipEntry -Object $Text -EntryName MyText -ZipFile $ZipFile
$ZipFile.Save()
} finally {
 if ($ZipFile -ne $null )
 { $ZipFile.PSDispose() }
}

This example reads a Zip file, adds an entry from a string, saves it and then releases resources archive.

EXAMPLE 4

PS C:\> try {
$Zip=Get-Zipfile -Path Test.zip
Dir *.txt|Add-ZipEntry -ZipFile $Zip
} finally {
$ZipFile.Close()
}

This example reads a Zip file, adds &;39;.txt&;39; files from a folder to it, saves it and then releases resources archive. The &;39;Close()&;39; method internally calls the method &;39;Save()&;39; then &;39;PSDispose()&;39;.

EXAMPLE 5

PS C:\> $PSZipEntries=Get-Zipfile -Path Test.zip -List
try {
$Zip=Get-Zipfile -Path Test.zip
$Entry=$Zip.Entries|Where {$_.FileName -eq 'Test.ps1'}
$Entry.Extract('C:\Temp')
} finally {
$ZipFile.Close()
}

The first instruction returns the catalog entries as objects of type &;39;PSZipEntry&;39;. The following search in the original catalog for an entry named &;39;Test.ps1&;39; then decompresses the &;39;C:\Temp&;39; Folder. The difference between the two approaches is that objects of type PSZipEntry lacks methods specific to objects of type ZipEntry.

EXAMPLE 6

PS C:\> try
{          
$ZipFile=Get-ZipFile -path C:\Temp\archive.zip -ZipErrorAction Skip -Verbose 
 Remove-ZipEntry -ZipFile $ZipFile -Query 'name = *.txt'
 Get-ChildItem C:\Temp\*.log|Add-ZipEntry -ZipFile $ZipFile
} finally {
$ZipFile.Close()
}

This example opens an archive, deletes all the .TXT files and adds all .LOG files from &;39;C:\Temp&;39; folder. Note that each change is recorded in the catalog of the archive, but only the call to &;39;Close ()&;39; method will record the changes in the archive.

Related links

Unknown https://psionic.codeplex.com/wikipage?title=Get-ZipFile-EN

PowerShell Support

  • PowerShell 2.0

Operating System Support

  • Windows 7
  • Windows 8
  • Windows 8.1
  • Windows Server 2008 all editions
  • Windows Server 2008 R2 all editions
  • Windows Server 2012 all editions
  • Windows Server 2012 R2 all editions

Last edited May 14, 2014 at 2:21 PM by LaurentDardenne, version 1

Comments

No comments yet.