::::::::: PowerShell :::::::::
Sunday, March 12, 2006
  [MSH] display and extract zip contents
In the previous post, I have talked about how to display zip contents of a file.
I have realized that ,while writing extraction script(out-ZipContent), it'd be better to convert the script into a snapin than using a simple script to support variety of features(such as extracting zip file contents into different folders) as well as using different Zip libraries(moreover, it's hard for me to use library functions Monadishly...)
So I will try to convert these scripts into Cmdlets later on and post the source on somewhere on GotDotNet...


For now, I have separated retrieving ZipStream object into another function called "Get-ZipStream", displaying logic into "Get-ZipContent" and extracting contents into "Out-ZipContent".

Well, I don't want to go too deep into usages and stuff but here is how cmdlets can be used.

To Display zip contents
MSH>get-zipstream .\zipFile.zip | get-zipContent

To Extract zip contents
MSH>get-zipstream .\zipFile.zip | out-zipContent


Get-ZipStream: Returns ZipStream

function Get-ZipStream {
param($zipFileName = $(throw "Enter Zip File Name"))

# Load J# library
# used "out-null" instead of "[void]" since "out-null" would not display
# loaded assembly even if there was an exception thrown
[System.Reflection.Assembly]::LoadWithPartialName("vjslib") | out-null

# 1) Open "java.io.FileInputStream" for a zip file
$fis = new-object java.io.FileInputStream($zipFileName)
# 2) Pipe File Input stream object to "java.io.ZipInputStream"
$zis = new-object java.util.zip.ZipInputStream($fis)

trap {
if ($zis -ne $null) { $zis.close() }
if ($fis -ne $null) { $fis.close() }
break
}

# Return the zip stream
$zis
}






Get-ZipContent: Displays Zip contents

function Get-ZipContent {
if ($args[0] -eq $null) {
# Retrive only the first value
$input.moveNext() | out-null
$zipStream = $input.get_Current()
}

# Load J# library
# used "out-null" instead of "[void]" since "out-null" would not display
# loaded assembly even if there was an exception thrown
[System.Reflection.Assembly]::LoadWithPartialName("vjslib") | out-null

trap { break; }

while (($ze = $zipStream.getNextEntry()) -ne $null) {
$ze.toString()
}

# Close "ZipInputStream" first and then "FileInputStream"
$zipStream.close()
remove-variable zipStream
}





Out-ZipContent: Extracts zip file content

function Out-ZipContent {
if ($args[0] -eq $null) {
$input.MoveNext()
$zis = $input.get_Current()
} else {
$zis = $args[0]
}

trap { break; }

[System.Reflection.Assembly]::LoadWithPartialName("vjslib") | out-null

while (($ze = $zis.getNextEntry()) -ne $null) {
$fn = $ze.getName()
$idx = $fn.LastIndexOf("/")
Write-Host "`$fn = $($fn)"

# we need create a directory
if ($idx -gt 1) {
$dir = $fn.SubString(0, $idx)
$dir = "$((pwd).path)\$($dir)"

# Since the directory doesn't exist, we now create it
if ( !(test-path $dir -type container) ) {
new-item -path $dir -type directory
Write-host "Created $($dir)"
}
}

$file = "$((pwd).path)\$($fn)"
# Write only for leaf files
if ( !(test-path $file -type container) ) {
[sbyte[]]$buf = [sbyte[]](@(0..127) * 8)
$fos = new-object Java.io.fileOutputStream($file)

while ( ($len = $zis.read($buf)) -ge 0 ) {
$fos.Write($buf, 0, $len)
}; $fos.close()
Write-Host "Extracted $($file)"
}
}
}



Tags :
 
Comments: Post a Comment



<< Home
Let's get lazy with PowerShell!

Name:
Location: Flushing, NY, United States

Experimenting with a different format of blogs...

Links
ARCHIVES
10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /


Powered by Blogger