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.          

No comments: