简体   繁体   English

使用 ZipArchive class 从文件夹中删除特定文件,在 war 文件中

[英]Delete specific files from folder, inside a war file using ZipArchive class

My goal is to get some files from the WAR, make changes, delete older files from WAR and then move the edited files to WAR.我的目标是从 WAR 中获取一些文件,进行更改,从 WAR 中删除旧文件,然后将编辑后的文件移动到 WAR。

At the moment I can find, edit, remove and move files from an WAR file using the ZipArchive class.目前,我可以使用 ZipArchive class 从 WAR 文件中查找、编辑、删除和移动文件。 But the problem I am facing is that I can not remove files from specific folders inside the WAR, just by name.但我面临的问题是我无法从 WAR 中的特定文件夹中删除文件,只是按名称。

Given the WAR structure I have:鉴于我的 WAR 结构:
~/war 〜/战争
~/war/client/lib/file.jar ~/war/client/lib/file.jar
~/war/server/lib/file.jar ~/war/server/lib/file.jar

Until I reached this point I have been using the following function to remove files:在我达到这一点之前,我一直在使用以下 function 来删除文件:

function Remove-FilesFromZip {
    param(
        [string]$ZipFile,
        [string]$FileName
    )

    try {
        
        [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression')
        $Retorno
        $stream = New-Object IO.FileStream($ZipFile, [IO.FileMode]::Open)
        $mode = [IO.Compression.ZipArchiveMode]::Update
        $zip = New-Object IO.Compression.ZipArchive($stream, $mode)
        Write-Host "$zip $mode $stream $Retorno" 

    ($zip.Entries | Where-Object { $FileName -contains $_.Name }) | ForEach-Object { $_.Delete() }

        Write-Host "$FileName excluido de $ZipFile" 
    }
    catch {
        Write-Host "Erro ao excluir $FileName de $ZipFile" 

        $zip.Dispose()
        $stream.Close()
        $stream.Dispose()
    }

    $zip.Dispose()
    $stream.Close()
    $stream.Dispose()

}

Since the files I was deleting only came up once on my WAR, it was not a problem, but if I try something like this:由于我要删除的文件只在我的 WAR 上出现过一次,所以这不是问题,但如果我尝试这样的事情:

Remove-FilesFromZip "C:/pathToWar/warFile.war" "file.jar"

Both files are deleted from the war.这两个文件都从战争中删除。

And if I try to use the path like so:如果我尝试像这样使用路径:

Remove-FilesFromZip "C:/pathToWar/warFile.war" "/server/lib/file.jar"

Nothing happens.什么都没发生。

How can I edit my function so I can describe the path to the file I would like to delete?如何编辑我的 function 以便描述要删除的文件的路径?

Also, for context I have another function that I use after this one, that I can move files to the war and to the desired folder, but when I move without deleting the file with same name, I got duplicate files inside and this is not desirable.另外,对于上下文,我还有另一个 function ,我在这个之后使用,我可以将文件移动到战争和所需的文件夹,但是当我移动而不删除同名文件时,我在里面得到了重复的文件,这不是可取的。

This is the function:这是 function:

function Move-FileToZip {
    param(
        $ZipFileName,
        $FileNameInZip,
        $NewFileToAdd
    )
    try {
        # Write-LogWIthTime "-------------- Move-FileToZip --------------"

        [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
        $zip = [System.IO.Compression.ZipFile]::Open($ZIPFileName, "Update")

        [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
        [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $NewFileToAdd, $FileNameInZip, "Optimal") | Out-Null
        $zip.Dispose()

        Write-Host "Adicionado com sucesso $NewFileToAdd as $FileNameInZip para $ZIPFileName "
    }
    catch {
        $zip.Dispose()
        Write-Host "Falha ao adicionar $NewFileToAdd para $ZIPFileName . Detalhes : $_" 
    }
}

That I use as:我用作:

Move-FileToZip "C:/pathToWar/warFile.war" "/server/lib/file.jar" "C:/updatedFiles/server/lib/file.jar"

The ideal scenario for me would be to simply update the file when I use the Move-FileToZip function, but just getting the Remove-FilesFromZip to work as I intend it would be great!对我来说,理想的情况是在我使用 Move-FileToZip function 时简单地更新文件,但只要让 Remove-FilesFromZip 按照我的意图工作就太好了!

The issue with your Remove-FilesFromZip function is that you are filtering the Zip entries by using the Name property and so you're not able to distinguish between the files with the same name but in different directories.您的Remove-FilesFromZip function 的问题是您正在使用Name属性过滤 Zip 条目,因此您无法区分具有相同名称但位于不同目录的文件。

 $zip.Entries | Where-Object { $FileName -contains $_.Name }

If you filter instead using the FullName property then you should be able to target the specific file by using it's full name within the zip file:如果您使用FullName属性进行过滤,那么您应该能够使用 zip 文件中的全名来定位特定文件:

 $zip.Entries | Where-Object { $FileName -eq $_.FullName }

With that change you should be able to do something like the following, assuming that client is a top level directory within the zip file:通过该更改,您应该能够执行以下操作,假设client是 zip 文件中的顶级目录:

Remove-FilesFromZip "C:/pathToWar/warFile.war" "client/lib/file.jar"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM