Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: Extract specific files/folders from a zipped archive

Say foo.zip contains:

a
b
c
|- c1.exe 
|- c2.dll 
|- c3.dll

where a, b, c are folders.

If I

Expand-Archive .\foo.zip -DestinationPath foo 

all files/folders in foo.zip are extracted.
I would like to extract only the c folder.

like image 327
antonio Avatar asked May 01 '17 07:05

antonio


People also ask

How do I extract individual files from a zip folder?

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.

How do I extract an archive folder?

To extract: A file or folder, open the archive and drag the file or folder to the location you require. The entire contents of the archive, select Extract All from the shortcut menu and follow the instructions.


Video Answer


2 Answers

try this

Add-Type -Assembly System.IO.Compression.FileSystem

#extract list entries for dir myzipdir/c/ into myzipdir.zip
$zip = [IO.Compression.ZipFile]::OpenRead("c:\temp\myzipdir.zip")
$entries=$zip.Entries | where {$_.FullName -like 'myzipdir/c/*' -and $_.FullName -ne 'myzipdir/c/'} 

#create dir for result of extraction
New-Item -ItemType Directory -Path "c:\temp\c" -Force

#extraction
$entries | foreach {[IO.Compression.ZipFileExtensions]::ExtractToFile( $_, "c:\temp\c\" + $_.Name) }

#free object
$zip.Dispose()
like image 190
Esperento57 Avatar answered Oct 23 '22 13:10

Esperento57


This one does not use external libraries:

$shell= New-Object -Com Shell.Application 
$shell.NameSpace("$(resolve-path foo.zip)").Items() | where Name -eq "c" | ? {
    $shell.NameSpace("$PWD").copyhere($_) } 

Perhaps it can be simplified a bit.

like image 44
antonio Avatar answered Oct 23 '22 13:10

antonio