powershell - 创建文件夹的zip,然后删除内容

标签 powershell

我有一个简单的文件夹结构,需要压缩后才能上传到AWS Lambda函数

node-modules/
index.js
package.json

上面的文件在根目录中。基本上,最终目标是我想要所有这些文件和子文件/目录的zip文件,而不是原来的结构。

当我尝试运行下面的命令时,它说它无法访问E://apps/myapp/release.zip,因为它正在被另一个进程使用。但是我可以看到它开始创建release.zip,但是它没有所有内容。
Add-Type -Assembly "System.IO.Compression.FileSystem";
[System.IO.Compression.ZipFile]::CreateFromDirectory(E://apps/myapp, E://apps/myapp/release.zip);

所以我尝试了另一种方法。取出文件夹和两个文件,然后将它们复制到一个临时文件夹中,然后尝试将其压缩回根目录。
Copy-Item E://apps/myapp E://apps/myapp/temp -recurse

我确实看到了temp/文件夹,但是在temp文件夹中,这就像是一个开始直到文件路径变得太长才结束副本。

任何提示将不胜感激。

最佳答案

问题可能是您在尝试压缩的文件夹中创建了zip文件。当您尝试使用临时文件夹(因此启动)时,您基本上会执行相同的操作。

尝试在要压缩的源文件夹之外创建目标。

$source = "E://apps/myapp"
$destination = "E://apps/myapp.release.zip"

# If the archive already exists, an IOException exception is thrown.
if(Test-Path $destination) {
    Remove-Item -Path $destination -Force -Recurse -Confirm:$false
}

[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($source, $destination)

# Once archive created delete folder
if(Test-Path $destination) {
    Remove-Item -Path $source -Force -Recurse -Confirm:$false
    Write-Host "directory removed: $source"
}

关于powershell - 创建文件夹的zip,然后删除内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40371849/

相关文章:

powershell - 在哈希表数组中搜索键的最简洁方法

powershell - 从CSV/TXT创建文件夹并将匹配的列移到Powershell或Batch中的新目录中

azure - 获取-AzureADPolicy : The term 'Get-AzureADPolicy' is not recognized

xml - 从 PowerShell 保存格式正确的 XML

java - 如何从 Java 程序内部创建 powershell 远程 session ?

azure - 断开 Azure VM 与 loganalytics 工作区的连接

powershell - 具有Mir参数的Robocopy最大量距

powershell - 如何引用 CSV 中的多行来卸载软件?

json - 在 PowerShell 中组合 JSON 对象

winforms - 在无限循环中启动脚本并可能停止脚本