File System - Zip Directory Contents

Octopus.Script exported 2018-02-08 by curiousdev belongs to ‘File System’ category.

Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level, and optionally includes the base directory.

Requires .NET 4.5 as it relies on the System.IO.Compression.ZipFile class.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Source Directory

SourceDirectoryName

The path to the directory to be archived, specified as a relative or absolute path.

Destination Archive File

DestinationArchiveFileName

The path of the archive to be created, specified as a relative or absolute path.

Compression Level

CompressionLevel = Optimal

Indicates whether to emphasize speed or compression effectiveness when creating the entry.

Include Base Directory

IncludeBaseDirectory

Include the directory name from Source Directory at the root of the archive.

Overwrite Destination If Exists

OverwriteDestination

Overwrite the destination archive file if it already exists.

Script body

Steps based on this template will execute the following PowerShell script.

$SourceDirectoryName = $OctopusParameters['SourceDirectoryName']
$DestinationArchiveFileName = $OctopusParameters['DestinationArchiveFileName']
$CompressionLevel = $OctopusParameters['CompressionLevel']
$IncludeBaseDirectory = $OctopusParameters['IncludeBaseDirectory']
$OverwriteDestination = $OctopusParameters['OverwriteDestination']

if (!$SourceDirectoryName)
{
    Write-Error "No Source Directory name was specified. Please specify the name of the directory to that will be zipped."
    exit -2
}

if (!$DestinationArchiveFileName)
{
    Write-Error "No Destination Archive File name was specified. Please specify the name of the zip file to be created."
    exit -2
}

if (($OverwriteDestination) -and (Test-Path $DestinationArchiveFileName))
{
    Write-Host "$DestinationArchiveFileName already exists. Will delete it before we create a new zip file with the same name."
    Remove-Item $DestinationArchiveFileName
}

Write-Host "Creating Zip file $DestinationArchiveFileName with the contents of directory $SourceDirectoryName using compression level $CompressionLevel"

$assembly = [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
[System.IO.Compression.ZipFile]::CreateFromDirectory($SourceDirectoryName, $DestinationArchiveFileName, $CompressionLevel, $IncludeBaseDirectory)

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "fa148b10-99e2-47be-b19f-a16dee1c8f27",
  "Name": "File System - Zip Directory Contents",
  "Description": "Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level, and optionally includes the base directory.\n\nRequires .NET 4.5 as it relies on the `System.IO.Compression.ZipFile` class.",
  "Version": 12,
  "ExportedAt": "2018-02-08T22:24:00.817+00:00",
  "ActionType": "Octopus.Script",
  "Author": "curiousdev",
  "Parameters": [
    {
      "Name": "SourceDirectoryName",
      "Label": "Source Directory",
      "HelpText": "The path to the directory to be archived, specified as a relative or absolute path.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "DestinationArchiveFileName",
      "Label": "Destination Archive File",
      "HelpText": "The path of the archive to be created, specified as a relative or absolute path.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "CompressionLevel",
      "Label": "Compression Level",
      "HelpText": "Indicates whether to emphasize speed or compression effectiveness when creating the entry.",
      "DefaultValue": "Optimal",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "Optimal|Optimal\nFastest|Fastest\nNoCompression|No Compression"
      }
    },
    {
      "Name": "IncludeBaseDirectory",
      "Label": "Include Base Directory",
      "HelpText": "Include the directory name from Source Directory at the root of the archive.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    },
    {
      "Name": "OverwriteDestination",
      "Label": "Overwrite Destination If Exists",
      "HelpText": "Overwrite the destination archive file if it already exists.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "$SourceDirectoryName = $OctopusParameters['SourceDirectoryName']\n$DestinationArchiveFileName = $OctopusParameters['DestinationArchiveFileName']\n$CompressionLevel = $OctopusParameters['CompressionLevel']\n$IncludeBaseDirectory = $OctopusParameters['IncludeBaseDirectory']\n$OverwriteDestination = $OctopusParameters['OverwriteDestination']\n\nif (!$SourceDirectoryName)\n{\n    Write-Error \"No Source Directory name was specified. Please specify the name of the directory to that will be zipped.\"\n    exit -2\n}\n\nif (!$DestinationArchiveFileName)\n{\n    Write-Error \"No Destination Archive File name was specified. Please specify the name of the zip file to be created.\"\n    exit -2\n}\n\nif (($OverwriteDestination) -and (Test-Path $DestinationArchiveFileName))\n{\n    Write-Host \"$DestinationArchiveFileName already exists. Will delete it before we create a new zip file with the same name.\"\n    Remove-Item $DestinationArchiveFileName\n}\n\nWrite-Host \"Creating Zip file $DestinationArchiveFileName with the contents of directory $SourceDirectoryName using compression level $CompressionLevel\"\n\n$assembly = [Reflection.Assembly]::LoadWithPartialName(\"System.IO.Compression.FileSystem\")\n[System.IO.Compression.ZipFile]::CreateFromDirectory($SourceDirectoryName, $DestinationArchiveFileName, $CompressionLevel, $IncludeBaseDirectory)\n",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "File System",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/file-system-zip-directory-contents.json",
  "Website": "/step-templates/fa148b10-99e2-47be-b19f-a16dee1c8f27",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAKhQTFRF/////78A/6oAVVVV/++//+q/gICA1dXV/79A/9R///ff/8MQ/9dg/8cg//vv/68A/+ef//PP//rv/7Ug/68Q/+Sv/8sw/9tw/9NQ/89w/+uv/+OP/8pg/89A/8VQ/9+f/9+A/9qP/+/P/99//7cA/7IA/7wA/6sA/+/A39/f/64A2tjX//Tfn5+f/7MA/7sA/74A/60A/9Zw/7gA/74Q/7oA/7UA/7EAi6g4fwAAAuRJREFUeNrs21tT2mAUheEvaaCQQCSczyi29WzP7f//Z6UdZBIIM44us9hf13vbC+eRbLo3qnNKKaWUUkoppZR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