Name

Add-ZipEntry

SYNOPSIS

Add an entry into an existing zip archive.

SYNTAX

Add-ZipEntry -InputObject <Object> [-ZipFile] <ZipFile> [[-Name] <String>] [-Comment <String>] [-EntryPathRoot <String>] [-Passthru] [<CommonParameters>]

DESCRIPTION

Add an entry into an existing .zip archive's catalog or a self extracting .exe archive. This entry can be a file or a directory name, a string or an array of bytes.

PARAMETERS

-InputObject <Object>

Contents associated to an entry.

Allowed types are :

- one or many files or directories objects,

- one or multiple file or directory names,

- a string,

 or an array of bytes.

All other types of object will be processed to string from ToString() method.

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

-ZipFile <ZipFile>

Target archive in which specified entry is added. This parameter waits for a ZipFile object and not a file name.

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

-Name <String>

Each entry is associated with a name in the catalog. For files or directories, their names are automatically used for entries names, in the root of the archive.

For strings or array of bytes, you must specify an entry name.

The use os the -EntryPathRoot parameter will not influence this naming.

During the call of the Expand-ZipFile function, the string or arrays entries will be extracted like for files.

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

-Comment <String>

Comment associated with the entry.

For entries such as strings or arrays, the comment will ba their name by default : [String] or [Byte[]].

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

-EntryPathRoot <String>

The new entry will be added in the specified path. By default, it is added to the root of the tree contained in the archive. The value of this parameter must reference an existing directory path.

You need to specify this parameter in order to avoid entry name clashes when compressing a recursive tree. This one allows to build the name of the entry relatively to the specified directory.

For example, specifying 'C:\Temp\Backup', the treating building name will remove 'C:\Temp\Backup' for each received file during recurisive compression of 'C:\Temp\Backup'.

So, for files 'C:\Temp\Backup\File1.ps1' and 'C:\Temp\Backup\Projet\File1.ps1', created entries in the catalog will be respectively : File1.ps1 Projet/File.ps1.

By specifying a directory name different from the begining of the archive will trigger an error and will stop current archiving path.

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

-Passthru <SwitchParameter>

Once entry is added to the archive's catalog, it is sent to the pipeline.

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.

INPUTS

System.String, System.IO.DirectoryInfo, System.IO.FileInfo, System.Byte[], System.Collections.IDictionary, Ionic.Zip.ZipEntry

OUTPUTS

Ionic.Zip.ZipEntry

NOTES

By default, the add of an existing entry will trigger an exception. This function works with the Expand-ZipEntry function.

EXAMPLES

-------------- EXAMPLE 1 --------------

PS C:\> try {         
$ZipFile=Get-Zipfile -Path C:\Temp\Test.zip
$File=Get-Item C:\Temp\Test.ps1 
Add-ZipEntry -InputObject $File -ZipFile $ZipFile
} finally {
$ZipFile.Close()
}

This commands add an entry to the C:\Temp\Test.zip archive.

-The first command makes an archive object from a file name,

-The second one builds a file object from a file name,

-The third one adds the file to an archive,

-The forth saves the archive on the disk and frees system ressources.

-------------- EXAMPLE 2 --------------

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

This commands add entries to the C:\Temp\Test.zip archive.

-The first instruction builds an archive object from a file name,

-The second one, from current directory, adds all .txt files to the archive,

-The third saves the archive to the disk and frees all system ressources.

-------------- EXAMPLE 3 --------------

PS C:\> try {         
$ZipFile=Get-Zipfile -Path C:\Temp\Test.zip
[string]$Text=Get-Content C:\Temp\Test.ps1|Out-String
Add-ZipEntry -InputObject $Text -Name MyText -ZipFile $ZipFile
} finally {
$ZipFile.Close()
}

This commands add a named entry in the C:\Temp\Test.zip archive from a string.

-The first instruction makes an archive object from a file name,

-The second reads a text file and get the result into a string,

-The third one makes an entry whose contents is saved to a string,

-The forth one saves the archive to the disk and frees the system ressources.

-------------- EXAMPLE 4 --------------

PS C:\> try {
$ZipFile=Get-Zipfile -Path C:\Temp\Test.zip         
ConvertTo-CliXml $PSVersionTable | Add-ZipEntry -Name 'PSVersiontable_clixml' -ZipFile $ZipFile
} finally {
$ZipFile.Close()
}

This commands add a named entry in the C:\Temp\Test.zip archive from a serialized object.

-The first instruction builds an archive object from a file name,

-The second serializes the contents of the object into the $PSVersiontable variable and adds it as a named entry to the archive,

-The third one saves the archive to the disk and frees the system ressources.

-------------- EXAMPLE 5 --------------

PS C:\> try {         
$ZipFile=Get-Zipfile -Path C:\Temp\Test.zip 
[byte[]] $Array=@(1..20)
Add-ZipEntry -InputObject $Array -Name MyArray -ZipFile $ZipFile
} finally {
$ZipFile.Close()
}

This commands add an entry named MyArray to the C:\Temp\Test.zip archive.

-The first instruction makes an archive object from a file name,

-The second one adds the object contained in the $Array variable and adds it to the archive as an entry named MyArray,

-The third one saves the archive to the disk and frees system ressources.

RELATED LINKS

Last edited May 14, 2014 at 2:39 PM by LaurentDardenne, version 9

Comments

No comments yet.