PoSH Challenge Part 5
For Level 6 I needed to get information from zip files not available from plain PowerShell, I decided to use a tool I had already handy 7Zip, that comes with a command line version (Instead of looking for a .NET DLL, you have to wait to level 8 for that ;-)
To make it a bit more usable in this Level I made a small wrapper function ( You need to have 7zip installed for this to work ):
Function Get-ZipFileInfo ($FileName) {
if (-not (test-path $filename)) {throw 'file not found'}
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$list = sz l $fileName -slt
$list |? {$_.split('=')[1]} |% {
$p = $_.split("=")
switch ($p[0].trim()) {
'Path'
{
$f = new-object object
Add-Member -inputobject $f -Name $p[0].trim() -MemberType NoteProperty -Value $p[1].trim()
}
'Host OS'
{
Add-Member -inputobject $f -Name $p[0].trim() -MemberType NoteProperty -Value $p[1].trim()
return $f
}
Default
{
Add-Member -inputobject $f -Name $p[0].trim() -MemberType NoteProperty -Value $p[1].trim()
}
}
}
}
Using this function You can list the contents of a zip file with 7Zip, but the wrapper parses the text output from zip into objects, so that they are easy to use from PowerShell :
PoSH> $info = Get-ZipFileInfo channel.zip
PoSH> $info | Get-Member
TypeName: System.Object
Name MemberType Definition
---- ---------- ----------
Equals Method System.Boolean Equals(Object obj)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
ToString Method System.String ToString()
Attributes NoteProperty System.String Attributes=.....
Comment NoteProperty System.String Comment=E
CRC NoteProperty System.String CRC=B8F7D6C7
Encrypted NoteProperty System.String Encrypted=-
Folder NoteProperty System.String Folder=-
Host OS NoteProperty System.String Host OS=FAT
Method NoteProperty System.String Method=Deflate
Modified NoteProperty System.String Modified=2006-06-06 06:06:06
Packed Size NoteProperty System.String Packed Size=23
Path NoteProperty System.String Path=29.txt
Size NoteProperty System.String Size=21
PoSH> $info[0]
Path : 29.txt
Folder : -
Size : 21
Packed Size : 23
Modified : 2006-06-06 06:06:06
Attributes : .....
Encrypted : -
Comment : E
CRC : B8F7D6C7
Method : Deflate
Host OS : FAT
PoSH> $info[11] | select path,comment
Path Comment
---- -------
668.txt O
PoSH> get-buffer | Out-File event6.html
After that I could solve the level like this :
$text = ""
$nothing = "90052"
while ($nothing) {
$nothing = [regex]::Match((gc "channel\$nothing.txt"),'\d+').value
$char = ($info |? {$_.path -eq "$nothing.txt"}).comment
if (-not $char) {$char = ' '}
if ($text.length % 65 -eq 0) {$char = "`n"}
$text += $char
}
$text
The lesson of this level, Commandline tools are still useful in PowerShell and a small function can make them even more useful in PowerShell as they are in Cmd.exe ;-)
Enjoy
Greetings /\/\o\/\/