flat extraction

Nov 30, 2010 at 6:32 AM

Is it possible to extract content of a zip with the directory structure?

EX:

+Archive.zip              -->           +Extraction Path

++ FOLDER                             ++ File.txt

+++ File.txt

Dec 3, 2010 at 2:15 AM
Edited Dec 3, 2010 at 8:13 AM

I figured it out. Here is my solution in Powershell:

$scriptDir refers to the directory where the assembly and your script is located.

*Function will overwrite existing files*

Add-Type -Path "$scriptDir\SevenZipSharp.dll"
[SevenZip.SevenZipExtractor]::SetLibraryPath("$scriptDir\7z.dll")

function Decompress {
	param([string]$FilePath, [string]$Destination)
        if(!(Test-Path $destination)){
                 New-Item $destination -Type Directory
        }
	$archive = new-object SevenZip.SevenZipExtractor($FilePath)
	$files = $archive.ArchiveFileData | ?{$_.IsDirectory -eq $False} | %{[Int]$_.Index}
	foreach($index in $files){
		$memoryStream = New-Object system.io.memorystream
		$archive.ExtractFile($index, $memoryStream)
		$filename = $archive.ArchiveFileData | ?{$_.Index -eq $index} | %{$_.FileName | Split-Path -leaf}
		$outfile = [system.io.file]::OpenWrite("$destination\$filename")
		$memoryStream.WriteTo($outfile)
		$outfile.Close()
                $memoryStream.Close()
	}
}