【发布时间】:2017-12-07 01:40:13
【问题描述】:
我在 Windows Server 2012R2 上有大量 500GB 的索引文件,应用程序会不断更新这些文件。
我必须使用 PowerShell 压缩文件,但是当我尝试使用以下 sn-p 代码压缩文件时,我得到了这个异常:
使用“4”参数调用“CreateFromDirectory”的异常:“进程无法访问文件 'E:\Program Files (x86)\Application Folder\File\Status.FCS',因为它正在被使用 由另一个进程。”
$zip = "E:\Folder\File.zip"
$can = "E:\Program Files (x86)\Application Folder\File
Import-Module AWSPowerShell
# functions
function ZipFiles( $zipfilename, $sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
$zipfilename, $compressionLevel, $false)
}
ZipFiles $zip $can
如果我使用 Windows GUI 压缩文件,我没有任何问题,但问题似乎只在我使用 PowerShell 脚本时发生。此外,如果我停止应用程序服务,PowerShell 脚本也可以正常工作(在 prod 环境中我不能这样做)。
其中一个解决方案是复制具有 500Gb 索引的文件夹并对其进行压缩(这应该可以),但我的 Windows 服务器上没有足够的磁盘空间来执行此操作。
那么有什么解决方案可以在使用 PowerShell 脚本写锁定时压缩文件吗?
【问题讨论】:
-
恐怕你必须编写自己的目录枚举,将每个文件读取为
IO.FileStream,并带有[IO.FileShare]::ReadWrite标志,允许打开其他进程使用的文件(锁定时除外用于显式阅读,但这里不是这种情况,因为 GUI 可以工作)并写入新的IO.Compression.ZipArchive。也许您将能够找到现有的库/示例。 -
谢谢@wOxxOm。我会调查的。
标签: c# windows powershell server windows-server-2012