Techibee.com

Reading ZIP file contents without extraction using PowerShell

As promised in my previous post, here is the script that our group developed during Singapore PowerShell Saturday #008 event.

This script relies on System.IO.Compression.FileSystem assembly to read the contents of ZIP(archive) for without extracting them to the disk. The advantage with this script is, it will not consume any resources/disk space because it is directly reading the zip file contents without extraction.

This script can take multiple ZIP files as input and return the output in PSObject format. This format is helpful if you want to feed the output of this script to some other script/function/cmdlets. It also supports exporting the output to CSV easily with the –ExportCSVFileName parameter. You need to give the CSV file path where you want to store it.

Note that this script works only if you have Dotnet Framework 4.5 or above. This is because the ZipFile class used to read the zip contents is made available starting from dotnet framework v4.5. I will share you other script in my next article which can be used without any dependency on dotnet version. But the only downside I noticed so far is, we can not determine what is the compressed file size and what is the uncompressed file size.

Code

            
[cmdletbinding()]            
param(            
 [Parameter(Mandatory=$true)]            
 [string[]]$FileName,            
 [String]$ExportCSVFileName            
)            
#Exit if the shell is using lower version of dotnet            

$dotnetversion = [Environment]::Version            
if(!($dotnetversion.Major -ge 4 -and $dotnetversion.Build -ge 30319)) {            
 write-error "You are not having Microsoft DotNet Framework 4.5 installed. Script exiting"            
 exit(1)            
}            

# Import dotnet libraries            

[Void][Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')            
$ObjArray = @()            
foreach($zipfile in $FileName) {            
if(Test-Path $ZipFile) {            
 $RawFiles = [IO.Compression.ZipFile]::OpenRead($zipFile).Entries            
 foreach($RawFile in $RawFiles) {            

  $object = New-Object -TypeName PSObject            
  $Object | Add-Member -MemberType NoteProperty -Name FileName -Value $RawFile.Name            
  $Object | Add-Member -MemberType NoteProperty -Name FullPath -Value $RawFile.FullName            
  $Object | Add-Member -MemberType NoteProperty -Name CompressedLengthInKB -Value ($RawFile.CompressedLength/1KB).Tostring("00")            
  $Object | Add-Member -MemberType NoteProperty -Name UnCompressedLengthInKB -Value ($RawFile.Length/1KB).Tostring("00")            
  $Object | Add-Member -MemberType NoteProperty -Name FileExtn -Value ([System.IO.Path]::GetExtension($RawFile.FullName))            
  $Object | Add-Member -MemberType NoteProperty -Name ZipFileName -Value $zipfile            
  $ObjArray += $Object            
  if(!$ExportCSVFileName) {            
   $Object            
  }            
 }            
} else {            
 Write-Warning "$ZipFileInput File path not found"            
}            
if ($ExportCSVFileName){            
 try {            
  $ObjArray  | Export-CSV -Path $ExportCSVFileName -NotypeInformation            
 } catch {            
  Write-Error "Failed to export the output to CSV. Details : $_"            
 }            
}            

}

Most of the code is self-explanatory. If you get any questions about this, please feel free to raise them. I am more than happy to help you.

Usage:

Output

Hope this helps and Happy learning…

Exit mobile version