Powershell and ZIP/UNZIP

The consultant wanted all the W3SVC log files.  We didn’t have them (all), but Blackboard stores them – along with other logs, in the archive directory.  In a ZIP file.  I did NOT want to unzip hundreds of archives to dig into the logs to get the right files.

There are several ways to handle ZIP files with Powershell – 7ZIP has an API, the community has sample code and i found a simple way to use the com shell to accomplish want i needed: http://serverfault.com/questions/18872/how-to-zip-unzip-files-in-powershell  I downloaded the archive directory, got a directory on the downloaded files, got the name property to the file (tostring()) and constructed a path to the zip files, and then to the location where i wanted to extract it.  Once extracted, i copied the log file from within the folders to a new location, then removed the current zip file.  Repeat as needed.

# download directory for archives
$s = dir K:\archives\
foreach ($ss in $s){ $filename = $ss.name.ToString() 
$shell_app=new-object -com shell.application 
# $filename # echo if you want to see progress
# use below if you want to cd to the directory, 
# use 2x down if you want to hardcode the location
# $zip_file = $shell_app.namespace((Get-Location).Path + "\$filename") 
$zip_file = $shell_app.namespace("K:\archives\$filename") 
# current directory vs. hard code for destination
$destination = $shell_app.namespace((Get-Location).Path) 
$destination = $shell_app.namespace("C:\mytemp") 
# copy extracted log files to the dump directory.
Copy-Item c:\mytemp\logs\httpd\W3SVC2\*.log K:\dump\
Remove-Item c:\myTemp\* -recurse

Worked just fine, albeit not that speedy, until i ran out of space on the destination drive.  Text files are SO MUCH LARGER uncompressed.

This entry was posted in powershell. Bookmark the permalink.