Showing posts with label Zipfile. Show all posts
Showing posts with label Zipfile. Show all posts

Thursday, 27 November 2014

Zip-Pshscripts3.ps1

#Requires –Version 5.0
#
#.Synopsis
#    Creates a zip file of PowerShell scripts 
#.Description
#    The script creates a zip file containing all the files, 
#    recursing through the top level PowerShell Script Library folder.
#
#.Notes
#    This script require PowerShell V5 for the zip file cmdlets!
#    Author - Thomas Lee - tfl@psp.co.uk
#
#.Example
#    PS [c:\foo]>  .\Zip-PSHScripts3.ps1
#    Total files  : 347
#    ps1 files    : 342
#    txt files    : 1
#    other  files : 4


# Define what to zip and from where
$zipfile  = "C:\foo\ScriptLib.ZIP"
$zipfrom  = "C:\Users\tfl\Dropbox\PowerShell Script Library (master)"
$recurse  = "true"
$ziptoadd = "ps1"

# Check it out
if ( ! (Test-path -Path $zipfrom ))
{
  Write-Host 'scripts folder does not exist'
}

# Zip it up!
Try 
  {
    Compress-Archive -Path $zipfrom -DestinationPath $zipfile -CompressionLevel Optimal -Update
  }
Catch 
  {
  Write-Host ' Error Zipping up the script library'
  $Error[0]
  }

# Stats
$files = ls $zipfrom -file -recurse
$files_ps1 = $files | Where-Object Extension -eq '.ps1'
$files_txt = $files | Where-Object Extension -eq '.txt'
$files_other = $files | Where-Object { $_.extension -NE '.PS1' -and $_.Extension -ne '.txt'} 

"Total files  : {0}" -f $($files.count)
"ps1 files    : {0}" -f $($files_ps1.count)
"txt files    : {0}" -f $($files_txt.count)
"other  files : {0}" -f $($files_other.count)


# All done
ls $zipfile

Monday, 29 July 2013

Get-Zip.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the Zip lib in .NET 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-ZIP.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-Zip 
  17.      
  18.     Directory: C:\example 
  19. Mode        LastWriteTime  Length Name                                                                                                                            
  20. ----        -------------  ------ ----                                                                                                                            
  21. d---- 7/29/2013   4:35 PM        extract                                                                                                                         
  22. d---- 7/29/2013   4:29 PM        start                                                                                                                           
  23. -a--- 7/29/2013   4:35 PM   1668 result.zip                                                                                                                      
  24.  
  25.  
  26.     Directory: C:\example\extract 
  27. Mode        LastWriteTime  Length Name                                                                                                                            
  28. ----        -------------  ------ ----                                                                                                                            
  29. -a---  7/29/2013  4:28 PM    5609 d1.txt                                                                                                                          
  30. -a---  7/29/2013  4:29 PM   67308 d2.txt                                                                                                                          
  31. -a---  7/29/2013  4:29 PM   67308 d3.txt                                                                                                                          
  32. -a---  7/29/2013  4:29 PM   67308 d4.txt                                                                                                                          
  33. -a---  7/29/2013  4:29 PM   67308 d5.txt                                                                                                                          
  34.  
  35.  
  36.     Directory: C:\example\start 
  37. Mode        LastWriteTime     Length Name                                                                                                                            
  38. ----        ------------     ------ ----                                                                                                                            
  39. -a---  7/29/2013  4:28 PM    5609 d1.txt                                                                                                                          
  40. -a---  7/29/2013  4:29 PM   67308 d2.txt                                                                                                                          
  41. -a---  7/29/2013  4:29 PM   67308 d3.txt                                                                                                                          
  42. -a---  7/29/2013  4:29 PM   67308 d4.txt                                                                                                                          
  43. -a---  7/29/2013  4:29 PM   67308 d5.txt                                                                                                                          
  44. #> 
  45.  
  46. # Load the compression namespace  
  47. # And yes, I know this usage is obsolete - but it works.  
  48. # Ignore the output  
  49. [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | out-null 
  50.  
  51. # Set locations 
  52. $startPath = "c:\example\start" 
  53. $zipPath = "c:\example\result.zip" 
  54. $extractPath = "c:\example\extract" 
  55. Remove-Item $zipPath -ea SilentlyContinue 
  56. Remove-Item -Path $extractPath -inc * -Recurse -ea SilentlyContinue 
  57.  
  58. # Create the zip file 
  59. [System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath
  60.  
  61. # Extract from zip and show what's all there 
  62. [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath,$extractPath); 
  63. ls c:\example -Recurse 
  64.          

Sunday, 24 February 2013

New-ZipFromDirectory.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     Creates a new zip file from an existing folder 
  4. .DESCRIPTION 
  5.     This script uses the .NET 4.5 zipfile class  
  6.     to create a zip file, getting contents from  
  7.     a folder. 
  8. .NOTES 
  9.     File Name  : New-ZipfromDirectory 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 3.0 and .NET 4.5 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     Psh> C:\foo\new-zip.ps1 
  17.     Zip file created: 
  18.      
  19.     Directory: C:\foo 
  20.          
  21.     Mode                LastWriteTime     Length Name 
  22.     ----                -------------     ------ ---- 
  23.     -a---         2/24/2013   3:00 PM     291182 ScriptLib.ZIP 
  24.  
  25. #> 
  26.  
  27. # Load the compression namespace 
  28. # and yes, I know this usage is obsolete - but it works. 
  29. # Ignore the output 
  30. [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null  
  31.  
  32. # Create a type accellerator for the zipfile class 
  33. [System.Type] $TypeAcceleratorsType=[System.Management.Automation.PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators',$True,$True) 
  34. $TypeAcceleratorsType::Add('Zipfile','System.IO.Compression.Zipfile'
  35.  
  36. # Now create a zip file 
  37. # Set target zip flie and source folder 
  38. $Folder  = 'E:\PowerShellScriptLib' 
  39. $Zipfile = 'C:\foo\ScriptLib.ZIP' 
  40.  
  41. # Ensure file does NOT exist and fodler DOES exist 
  42. If (Test-Path $zipfile -EA -0) { 
  43.    Throw "$Zipfile exists - not safe to continue"
  44. If (!(Test-Path $folder)) { 
  45.    Throw "$Folder does not seem to exist"
  46.     
  47. # Now create the Zip file 
  48. Try { 
  49.   [Zipfile]::CreateFromDirectory( $folder, $zipfile) 
  50.   "Zip file created:";ls $zipfile} 
  51. Catch { 
  52.   "Zip File NOT created" 
  53.   $Error[0]}