Posted in
Windows Powershell |
No Comment | 3,327 views | 23/04/2015 16:34
** This is alternative custom script to Compress-Archive for wide range compatibility **
I’ve recently installed Windows Server 2016 as Core installation. Due to there is no shell, my old PowerShell script didn’t work:
1
2
3
4
5
6
7
8
| $ShellApplication = New-Object -com Shell.Application
$ZipPackage = $ShellApplication.NameSpace($ZipPath)
foreach($File in $input)
{
$ZipPackage.CopyHere($File.FullName)
Start-Sleep -milliseconds 5
} |
$ShellApplication = New-Object -com Shell.Application
$ZipPackage = $ShellApplication.NameSpace($ZipPath)
foreach($File in $input)
{
$ZipPackage.CopyHere($File.FullName)
Start-Sleep -milliseconds 5
}
As you see, I’ve used Shell Application before. But now, I’m not able to run that on Server 2016 Core.
So I searched on internet about it and found MVP Jeffrey Hick‘s post:
I used that codes and made a zip function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| function ConvertTo-CompressedFile
{
param([string]$ZipPath)
# Get Files
[string[]]$Files = $input.FullName;
if ($Files -ne $Null)
{
# Load the assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
# Check Zip Extension
if ($ZipPath -notlike "*.zip")
{
Write-Output "Please check your zip file path."
break;
}
# Check Zip Path
if(-not (Test-Path($ZipPath)))
{
# Get Zip File Name
$ZipName = $ZipPath.Split("\")[-1]
if ($ZipPath -notlike "*\*")
{
# Get Current Location
$CurrentLocation = (Get-Location).Path
# Get Current Zip File Path
$CurrentZipPath = $CurrentLocation + "\" + $ZipName
# Update Zip Path
$ZipPath = $CurrentZipPath;
}
# Create Zip File
Set-Content $ZipPath ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
# Set File Attributes
(Get-ChildItem $ZipPath).IsReadOnly = $false
}
# Get Zip File
$ZipFile = [System.IO.Compression.ZipFile]::Open($ZipPath,"Update")
foreach($File in $Files)
{
# Get File Name
$FileName = $File.Split("\")[-1]
# Compress File
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipFile,$File,$FileName,"optimal") | Out-Null
# Buffer
Start-Sleep -milliseconds 5
}
# Close Zip File
$ZipFile.Dispose()
# Output Zip Path
$ZipPath
}
} |
function ConvertTo-CompressedFile
{
param([string]$ZipPath)
# Get Files
[string[]]$Files = $input.FullName;
if ($Files -ne $Null)
{
# Load the assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
# Check Zip Extension
if ($ZipPath -notlike "*.zip")
{
Write-Output "Please check your zip file path."
break;
}
# Check Zip Path
if(-not (Test-Path($ZipPath)))
{
# Get Zip File Name
$ZipName = $ZipPath.Split("\")[-1]
if ($ZipPath -notlike "*\*")
{
# Get Current Location
$CurrentLocation = (Get-Location).Path
# Get Current Zip File Path
$CurrentZipPath = $CurrentLocation + "\" + $ZipName
# Update Zip Path
$ZipPath = $CurrentZipPath;
}
# Create Zip File
Set-Content $ZipPath ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
# Set File Attributes
(Get-ChildItem $ZipPath).IsReadOnly = $false
}
# Get Zip File
$ZipFile = [System.IO.Compression.ZipFile]::Open($ZipPath,"Update")
foreach($File in $Files)
{
# Get File Name
$FileName = $File.Split("\")[-1]
# Compress File
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipFile,$File,$FileName,"optimal") | Out-Null
# Buffer
Start-Sleep -milliseconds 5
}
# Close Zip File
$ZipFile.Dispose()
# Output Zip Path
$ZipPath
}
}
So you can easily compress any file on Windows Server 2016.
Usage:
Get-ChildItem MyFile.txt | ConvertTo-CompressedFile C:\MyFile.zip |
Get-ChildItem MyFile.txt | ConvertTo-CompressedFile C:\MyFile.zip
Also it works for current directories:
Get-ChildItem MyFile.txt | ConvertTo-CompressedFile MyFile.zip |
Get-ChildItem MyFile.txt | ConvertTo-CompressedFile MyFile.zip
If zip file is not exist, that will be created by this script.
Note: This script requires .Net 4.5 Core installed on Windows Server 2016.