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

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

问题描述

我正在尝试从直接包含源目录内容而不是源目录内容的目录中创建一个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)

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

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