创建一个没有位于其中的原始目录的 Zip [英] Create A Zip without the original directory located within

查看:24
本文介绍了创建一个没有位于其中的原始目录的 Zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从直接包含源目录内容而不是源目录内容的目录创建 zip,然后将内容上传到 Azure.

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.

所以我有 C:\temp\aFolder,使用 Write-Zip PSCXE 从中创建一个 zip

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

或 Create-7zip 函数

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"

这两种方式都将创建一个 ZIP 存档,其中的源目录位于其中.

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

所以它将是:TEST.ZIP -> TEST -> myContent.

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

我想要的是:TEST.ZIP -> myContent.这可能吗?我找不到任何关于此的信息.

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

非常感谢任何想法/帮助.

Any ideas/help is greatly appreciated.

因为我无法回答我自己的问题

天啊.@MJOLNIR 再次救了我,我刚刚在另一篇文章中找到了他的答案.我道歉.

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

答案是使用(如果您安装了 .NET 4.5)

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)

将产生 TEST.ZIP -> 内容.

Will produce the TEST.ZIP -> Contents.

推荐答案

请注意,在 .Net Framework 4.5 之前不支持 ZipFile 类.

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)

这篇关于创建一个没有位于其中的原始目录的 Zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆