Creating a zip file from a single file

In the last 2 posts I shared two PowerShell functions for extracting an archive and creating a new zip file from a folder. Today I will share a new function, New-ArchiveFromFile.

This function you can use when you want to compress a single file. This can be useful for log management. Instead of archiving log files in one big zip file I prefer archive log files in separate zip files. This way it is much easier and faster to remove older files that you don’t want to keep.

 

<#

.Synopsis

   Creates a new archive from a file

.DESCRIPTION

   Creates a new archive with the contents from a file. This function relies on the

   .NET Framework 4.5. On windwows Server 2012 R2 Core you can install it with

   Install-WindowsFeature Net-Framework-45-Core

.EXAMPLE

   New-ArchiveFromFile -Source c:\test\test.txt -Destination c:\test.zip

#>

function New-ArchiveFromFile

{

    [CmdletBinding()]

    [OutputType([int])]

    Param

    (

        # Param1 help description

        [Parameter(Mandatory=$true,

                   ValueFromPipelineByPropertyName=$false,

                   Position=0)]

        [string]

        $Source,

        # Param2 help description

        [Parameter(Mandatory=$true,

                   ValueFromPipelineByPropertyName=$false,

                   Position=1)]

        [string]

        $Destination

    )

    Begin

    {

        [System.Reflection.Assembly]::LoadWithPartialName(“System.IO.Compression.FileSystem”) | Out-Null

    }

    Process

    {

        try

        {

            Write-Verbose “Creating archive $Destination….”

            $zipEntry = $Source | Split-Path -Leaf

            $zipFile = [System.IO.Compression.ZipFile]::Open($Destination, ‘Update’)

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

            [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zipfile,$Source,$zipEntry,$compressionLevel)

            Write-Verbose “Created archive $destination.”

        }

        catch [System.IO.DirectoryNotFoundException]

        {

            Write-Host “ERROR: The source $source does not exist!” -ForegroundColor Red

        }

        catch [System.IO.IOException]

        {

            Write-Host “ERROR: The file $Source is in use or $destination already exists!” -ForegroundColor Red

        }

        catch [System.UnauthorizedAccessException]

        {

            Write-Host “ERROR: You are not authorized to access the source or destination” -ForegroundColor Red

        }

    }

    End

    {

        $zipFile.Dispose()

    }

}


 

This is just a basic version. It could use some improvements.

Advertisements