Zipfile - PowerShell


Overview

The ZipFile command compresses and archives files and directories into a single ZIP archive, offering reliable file compression, secure data storage, and convenient file transfer. It plays a vital role in managing and organizing large volumes of data.

Syntax

ZipFile
   [-Path] <string>
   [-Destination] <string>
   [-Force]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [-Recurse] [-RecurseDepth <int>]
   [-ExcludePattern <string>]
   [-IncludePattern <string>]
   [-Checksum]
   [-CompressLevel <int>]
   [-ThrottleLimit <int>]
   [-CommonParameters]

Options/Flags

  • -Destination: Specifies the output path and filename for the ZIP archive.
  • -Force: Overwrites an existing ZIP archive with the same name without prompting.
  • -PassThru: Returns the compressed file as a FileSystemInfo object.
  • -Recurse: Compresses subdirectories and their contents.
  • -RecurseDepth: Specifies the maximum level of subdirectories to compress.
  • -ExcludePattern: Excludes files and directories matching the specified pattern. Supports wildcards (* and ?).
  • -IncludePattern: Includes only files and directories matching the specified pattern. Supports wildcards (* and ?).
  • -Checksum: Calculates and adds a checksum to each file in the ZIP archive.
  • -CompressLevel: Sets the compression level from 0 (no compression) to 9 (maximum compression).
  • -ThrottleLimit: Limits the number of concurrent operations.

Examples

Basic Usage

ZipFile -Path ./files -Destination ./archive.zip

Compressing with Patterns and Checksums

ZipFile -Path ./directory -Destination ./archive.zip -ExcludePattern "*.txt" -IncludePattern "*.pdf" -Checksum

Compression with Recursion

ZipFile -Path ./root -Destination ./archive.zip -Recurse -RecurseDepth 2

Custom Compression Level

ZipFile -Path ./files -Destination ./high-compressed.zip -CompressLevel 9

Common Issues

  • Ensure the destination path does not contain invalid characters or special symbols.
  • Check that you have sufficient permissions to modify the destination path.
  • Exclude large, unnecessary files to reduce archive size and improve performance.
  • Use patterns carefully to avoid excluding critical files.

Integration

  • The ZipFile command can be combined with Copy-Item to backup and archive data.
  • Utilize Get-Item and Where-Object to filter specific items before archiving.
  • Compress-Archive is an alternative command that compresses files into various archive formats, including ZIP.