John W
John W

Reputation: 129

Powershell : ExtractToDirectory with no folder created

I've got a zip file with this structure: test.zip -> Mark -> Today-> *.* ( every files)

When I extract it , as a result I get this : test (folder) -> Mark (folder) -> Today ->*.* ( every files)

How Can I get this structure : Today (folder) ->*.* (every files)

I mean , I trying to avoid the first two folders creation.

The best thing to be would be extract only the files without folders

 function Unzip
 {
     param([string]$zipfile, [string]$out)
     [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $out)
 }

unzip Test.zip $testPath

Edit : I'm sorry I made a mistake about the question. The right question is : How Can I extract only the files or the last folders (today). I wondering if I need a temporary folder or if there is a option for that, but reading the doc it seems not

Upvotes: 4

Views: 3850

Answers (1)

Sage Pourpre
Sage Pourpre

Reputation: 10333

Here is how you'd proceed to extract only the files without consideration for the folder structure.

Add-Type -AssemblyName 'System.IO.Compression.FileSystem'

function Unzip {
    param([string]$zipfile, [string]$out)


    $Archive = [System.IO.Compression.ZipFile]::OpenRead($zipfile)
    Foreach ($entry in $Archive.Entries.Where( { $_.Name.length -gt 0 })) {
        [System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, "$out\$($entry.Name)")
    }
    $Archive.Dispose()
}

# Example 
Unzip -zipfile 'Z:\_tmp\impfiles\1.zip' -out 'Z:\_tmp\impfiles'

Of course, now that you discarded the folder structure, you will have to implement — if needed — some way to deal with the possibility of file name clash (For instance, if you have a Readme.txt in 2 different folders)

Upvotes: 3

Related Questions