0

I have a script in place to zip all the files which were created on a specific date.

Add-Type -AssemblyName System.IO.Compression.FileSystem 

$logfolder = "c:\users\riteshthakur\desktop\abc"
$startdate = "20161205"
$enddate = "20161205"

[System.IO.Compression.ZipArchive] $arch = [System.IO.Compression.ZipFile]::Open('c:\users\riteshthakur\desktop\arch.zip', [System.IO.Compression.ZipArchiveMode]::Update)

Get-ChildItem - path $logFolder | 
Where-Object {$_.CreationDate -gt $startDate -and $_.CreationDate -lt $endDate} | 
foreach{[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($arch,$_.FullName,$_.Name)}
# archive will be updated with files after you close it. normally, in C#, you would use "using ZipArchvie arch = new ZipFile" and object would be disposed upon exiting "using" block. here you have to dispose manually:
$arch.Dispose()

I am running this script but this is throwing : "Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found."

Please help.

Matt
  • 45,022
  • 8
  • 78
  • 119
Moose
  • 751
  • 22
  • 42
  • there are `Compress-Archive` and `Expand-Archive` in Powershell (not sure which version they appeared in). – 4c74356b41 Dec 12 '16 at 12:42
  • They are in powershell version 5.0. i am still using powershell 4.0 – Moose Dec 12 '16 at 12:44
  • Pretty sure this is a .Net framework issue (see other answer). If I am wrong let me know and we can retract the dup vote. – Matt Dec 12 '16 at 13:25

2 Answers2

2

According to https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx, that assembly didn't become available until .NET Framework 4.5. What version of .NET Framework do you have installed? If you have 4.5 or later, you might need to reinstall/repair it.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
0

Originally published by Bryan C. O'Connell, here is a function to Zip:

# Purpose: Creates a .zip file of a file or folder.
# Params:
# -target: The file or folder you would like to zip.
#
# -zip_to: The location where the zip file will be created. If an old version
# exists, it will be deleted.
#
# -compression (optional): Sets the compression level for your zip file. Options:
# a. fast - Higher process speed, larger file size (default option).
# b. small - Slower process speed, smaller file size.
# c. none - Fastest process speed, largest file size.
#
# -add_timestamp (optional): Applies a timestamp to the .zip file name.
# By default, no timestamp is used.
#
# -confirm (optional): When provided, indicates that you would like to be
# prompted when the zip process is finished.
#
# |Info|

[CmdletBinding()]
Param (
  [Parameter(Mandatory=$true,Position=0)]
  [string]$target,

  [Parameter(Mandatory=$true,Position=1)]
  [string]$zip_to,

  [Parameter(Mandatory=$false,Position=2)]
  [ValidateSet("fast","small","none")]
  [string]$compression,

  [Parameter(Mandatory=$false,Position=3)]
  [bool]$timestamp,

  [Parameter(Mandatory=$false,Position=4)]
  [bool]$confirm
)

#-----------------------------------------------------------------------------#
function DeleteFileOrFolder
{ Param([string]$PathToItem)

  if (Test-Path $PathToItem)
  {
    Remove-Item ($PathToItem) -Force -Recurse;
  }
}

function DetermineCompressionLevel{
[Reflection.Assembly]::LoadFile('C:\WINDOWS\System32\zipfldr.dll')
Add-Type -Assembly System.IO.Compression.FileSystem
  $CompressionToUse = $null;

  switch($compression)
  {
    "fast" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest}
    "small" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal}
    "none" {$CompressionToUse = [System.IO.Compression.CompressionLevel]::NoCompression}
    default {$CompressionToUse = [System.IO.Compression.CompressionLevel]::Fastest}
  }

  return $CompressionToUse;
}

#-----------------------------------------------------------------------------#
Write-Output "Starting zip process...";

if ((Get-Item $target).PSIsContainer)
{
  $zip_to = ($zip_to + "\" + (Split-Path $target -Leaf) + ".zip");
}
else{

  #So, the CreateFromDirectory function below will only operate on a $target
  #that's a Folder, which means some additional steps are needed to create a
  #new folder and move the target file into it before attempting the zip process. 
  $FileName = [System.IO.Path]::GetFileNameWithoutExtension($target);
  $NewFolderName = ($zip_to + "\" + $FileName)

  DeleteFileOrFolder($NewFolderName);

  md -Path $NewFolderName;
  Copy-Item ($target) $NewFolderName;

  $target = $NewFolderName;
  $zip_to = $NewFolderName + ".zip";
}

DeleteFileOrFolder($zip_to);

if ($timestamp)
{
  $TimeInfo = New-Object System.Globalization.DateTimeFormatInfo;
  $CurrentTimestamp = Get-Date -Format $TimeInfo.SortableDateTimePattern;
  $CurrentTimestamp = $CurrentTimestamp.Replace(":", "-");
  $zip_to = $zip_to.Replace(".zip", ("-" + $CurrentTimestamp + ".zip"));
}

$Compression_Level = (DetermineCompressionLevel);
$IncludeBaseFolder = $false;

[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" );
[System.IO.Compression.ZipFile]::CreateFromDirectory($target, $zip_to, $Compression_Level, $IncludeBaseFolder);

Write-Output "Zip process complete.";

if ($confirm)
{
  write-Output "Press any key to quit ...";
  $quit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
}

Use this as follows:

zip.ps1 -target "C:\Projects\test" -zip_to "C:\projects\test2" [-compression fast] [-timestamp] [-confirm]
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • The code posted attempts to add the same assembly that the original poster said was generating the error. Why would the same error _not_ occur here? – Jeff Zeitlin Dec 12 '16 at 13:57
  • @JeffZeitlin: I believe in the system where are getting the error is not having the *zipfldr.dll* under this path `C:\WINDOWS\System32\zipfldr.dll`. Could you please check if that file is there or not. By default it should be there particularly if you are using any winrar or 7zip – Ranadip Dutta Dec 12 '16 at 15:49
  • OK, somehow the first time through I missed the `[Reflection.Assembly]::LoadFile('C:\WINDOWS\System32\zipfldr.dll')`, which might enable the function as a whole to run. I'm not convinced that even with that assembly loaded that the Add-Type immediately following won't throw the same error that the original poster was asking about. – Jeff Zeitlin Dec 12 '16 at 16:03
  • I dont think so. I t should not. If you are getting any error , could you please post the error or the screenshot. I am using this for a very long time actually, I never faced any issue if the dll is present. – Ranadip Dutta Dec 12 '16 at 16:04
  • I'm not in a position at the moment to test, as I have .NET 4.6.2 installed, so the Add-Type works for me. However, the assembly called for in the Add-Type _didn't exist prior to .NET 4.5_, which could explain the original poster's error, and which is why I queried the OP about it. – Jeff Zeitlin Dec 12 '16 at 16:19
  • I do also have to check that then . If possible , I will test that where we have .NET 4.0 or less . – Ranadip Dutta Dec 12 '16 at 16:23