Create A Zip without the original directory located within

心不动则不痛 提交于 2019-12-25 04:19:44

问题


I'm attempting to create a zip from from a directory that directly contains the contents of the source directory and not the source directory then the content, which will then be uploaded to Azure.

So I have C:\temp\aFolder, create a zip from that using either the Write-Zip PSCXE

write-zip c:\temp\test c:\temp\test2.zip

or the Create-7zip function

function create-7zip([String] $aDirectory, [String] $aZipfile){
    [string]$pathToZipExe = "C:\Program Files (x86)\7-zip\7z.exe";
    [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory", "-r";
   & $pathToZipExe $arguments;
}

create-7zip "c:\temp\test" "c:\temp\test.zip"

Both ways will create a ZIP archive with the source directory located within.

So it'll be: TEST.ZIP -> TEST -> myContent.

What I want is: TEST.ZIP -> myContent. Is this possible? I can't find anything on this.

Any ideas/help is greatly appreciated.

EDIT: Since I can't answer my own question

OH MY GOD. @MJOLNIR has saved me again, I JUST FOUND his answer on another post. I apologize.

The answer is to use (If you have .NET 4.5 installed)

$src = "c:\temp\test"
$dst = "c:\temp\test3.zip"
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($src, $dst)

Will produce the TEST.ZIP -> Contents.


回答1:


Note that the ZipFile class is not supported prior to .Net Framework 4.5.

Neither the latest and greatest Framework nor external tools are actually required, though. Something like this should also work:

$folder  = 'C:\temp\test'
$zipfile = 'C:\path\to\your.zip'

# create the zip file
$b = 'P', 'K', 5, 6 | % { [char]$_ }
$b += 1..18 | % { [char]0 }
[IO.File]::WriteAllBytes($zipfile, [byte[]]$b)

$app = New-Object -COM 'Shell.Application'

$src = $app.NameSpace($folder)
$dst = $app.NameSpace($zipfile)

# copy items from source folder to zip file
$dst.CopyHere($src.Items())

# wait for copying to complete
do {
  Start-Sleep -Milliseconds 200
} until ($src.Items().Count -eq $dst.Items().Count)


来源:https://stackoverflow.com/questions/23087140/create-a-zip-without-the-original-directory-located-within

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!