Powershell 这个程序不起作用。代码需要从zip存档中提取文件,然后删除zip存档

Powershell 这个程序不起作用。代码需要从zip存档中提取文件,然后删除zip存档,powershell,Powershell,这个程序不起作用 代码需要从zip存档中提取文件,然后删除zip存档。程序不会从存档中提取文件 # Config $Username = "xxx@xxxx.ru" $Password = "xxxxxx" $LocalFile = "C:\Prices\TechnoMarin\price.zip" $RemoteFile = "ftp://00.00.00.00/price.zip" # Create a FTPWebRequest $FTPRequest =

这个程序不起作用

代码需要从zip存档中提取文件,然后删除zip存档。程序不会从存档中提取文件

# Config 
$Username = "xxx@xxxx.ru" 
$Password = "xxxxxx" 
$LocalFile = "C:\Prices\TechnoMarin\price.zip" 
$RemoteFile = "ftp://00.00.00.00/price.zip" 


    # Create a FTPWebRequest 
    $FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) 
    $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password) 
    $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
    $FTPRequest.UseBinary = $true 
    $FTPRequest.KeepAlive = $false 
    # Send the ftp request 
    $FTPResponse = $FTPRequest.GetResponse() 
    # Get a download stream from the server response 
    $ResponseStream = $FTPResponse.GetResponseStream() 
    # Create the target file on the local system and the download buffer 
    $LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
    [byte[]]$ReadBuffer = New-Object byte[] 1024 
    # Loop through the download 
    do { 
    $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
    $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
    } 
    while ($ReadLength -ne 0)

    #Wait-Event -Timeout 50

    Add-Type -AssemblyName "System.IO.Compression.FileSystem"

    $ArchiveFileName = "C:\Prices\TechnoMarin\price.zip"

    $ExtractPath = "C:\Prices\TechnoMarin\"

    [IO.Compression.ZipFile]::ExtractToDirectory($ArchiveFileName, $ExtractPath)
我得到这个错误:

C:\prices\TechnoMarin>powershell.exe-noexit C:\prices\TechnoMarin\price.ps1

使用“2”参数调用“ExtractToDirectory”时出现异常:“进程无法访问文件“C:\Prices\TechnoMarin\price.zip”,因为此文件正由另一个进程使用

C:\prices\TechnoMarin\price.ps1:35符号:1
+[IO.Compression.ZipFile]::ExtractToDirectory($ArchiveFileName,$Extra…
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:NotSpecified:(:)[],MethodInvocationException
+FullyQualifiedErrorId:IOException


文件流已打开并写入,但从未释放。因此,打开对象持有文件的写锁,提取部分无法访问文件

下载文件后,请尝试包含一个
.Close()
。就像这样

do { 
  $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
  $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
}  while ($ReadLength -ne 0)
# All data has been written, close the file.
$LocalFileFile.Close()

Add-Type -AssemblyName "System.IO.Compression.FileSystem"
...
用于删除。