【问题标题】:Delete a file from zip files in powershell 4从powershell 4中的zip文件中删除文件
【发布时间】:2018-10-05 00:03:38
【问题描述】:

我有一个运行良好的功能。它将 zip 文件提取到目标文件夹中。如果目标文件夹包含文件,它将被新的提取文件覆盖。现在我想在提取 zip 文件之前删除/删除一个大小为 12G 的文件 (bigfile.txt)。我将如何做到这一点。谁能帮帮我?下面是功能。谢谢

function Unzip($zipfile, $outdir)
{
Add-Type -AssemblyName System.IO.Compression.FileSystem
$archive = [System.IO.Compression.ZipFile]::OpenRead($zipfile)
foreach ($entry in $archive.Entries)
{
$entryTargetFilePath = [System.IO.Path]::Combine($outdir, $entry.FullName)
$entryDir = [System.IO.Path]::GetDirectoryName($entryTargetFilePath)
#Ensure the directory of the archive entry exists
if(!(Test-Path $entryDir )){
New-Item -ItemType Directory -Path $entryDir | Out-Null
}

#If the entry is not a directory entry, then extract entry
if(!$entryTargetFilePath.EndsWith("\")){
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $entryTargetFilePath, $true);
}

Unzip -zipfile "c:\temp\filezip_1.zip" -outdir "c:\folder_1\extracted_files"
Unzip -zipfile "c:\temp\filezip_2.zip" -outdir "c:\folder_2\extracted_files"e

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您只显示您的解压缩/从 zip 文件中提取的工作,而不是删除工作,这是您要询问的部分。

    但您所要求的似乎与此讨论和答案重复

    Remove files from .zip file with Powershell

    您也已经在调用 System.IO.Compression 命名空间,因此 System.IO.Compression.Filesystem 有一个更新和删除方法。

    示例:(忽略/删除暂停 - 它就在那里,因此人们可以看到每个阶段的结果。)

    # Zip file path
    $zip = 'D:\Temp\MyFile.zip'
    
    # Instantiate the .Net namespace
    add-type -AssemblyName 'System.IO.Compression.filesystem'
    
    # Remove a file from a zip archive
    foreach ($z in $zip)
    {
        # Open the zip for updating
        $tempz = [io.compression.zipfile]::Open($z,'Update')
    
        "`nShow all files in the zip"
        $tempz.Entries
        Pause
    
        "`nDelete a specific file"
        ($tempz.Entries | Where FullName -Match 'Test.clixml').Delete()
        Pause
    
        "`nValidate remove"
        $tempz.Entries
        Pause
    
        # Clean up / close the zip
        $tempz.Dispose()
    }
    
    
    Show all files in the zip
    
    
    Archive            : System.IO.Compression.ZipArchive
    CompressedLength   : 69
    ExternalAttributes : 32
    FullName           : newfile.txt
    LastWriteTime      : 30-Sep-18 20:52:08 -07:00
    Length             : 116
    Name               : newfile.txt
    
    Archive            : System.IO.Compression.ZipArchive
    CompressedLength   : 41438
    ExternalAttributes : 32
    FullName           : ps-gps.xml
    LastWriteTime      : 02-Oct-18 19:29:44 -07:00
    Length             : 767464
    Name               : ps-gps.xml
    
    Archive            : System.IO.Compression.ZipArchive
    CompressedLength   : 45
    ExternalAttributes : 32
    FullName           : MyFile.txt
    LastWriteTime      : 30-Sep-18 23:31:08 -07:00
    Length             : 55
    Name               : MyFile.txt
    
    Archive            : System.IO.Compression.ZipArchive
    CompressedLength   : 132
    ExternalAttributes : 32
    FullName           : Test.clixml
    LastWriteTime      : 02-Oct-18 17:26:00 -07:00
    Length             : 202
    Name               : Test.clixml
    
    Press Enter to continue...: 
    
    Delete a specific file
    Press Enter to continue...: 
    
    Validate remove
    Archive            : System.IO.Compression.ZipArchive
    CompressedLength   : 69
    ExternalAttributes : 32
    FullName           : newfile.txt
    LastWriteTime      : 30-Sep-18 20:52:08 -07:00
    Length             : 116
    Name               : newfile.txt
    
    Archive            : System.IO.Compression.ZipArchive
    CompressedLength   : 41438
    ExternalAttributes : 32
    FullName           : ps-gps.xml
    LastWriteTime      : 02-Oct-18 19:29:44 -07:00
    Length             : 767464
    Name               : ps-gps.xml
    
    Archive            : System.IO.Compression.ZipArchive
    CompressedLength   : 45
    ExternalAttributes : 32
    FullName           : MyFile.txt
    LastWriteTime      : 30-Sep-18 23:31:08 -07:00
    Length             : 55
    Name               : MyFile.txt
    
    Press Enter to continue...: 
    

    根据 OP 请求更新/多个文件和代码合并

    您可能需要处理的 zip 文件的数量真的不是问题。 尽可能多地通过。

    您没有说的是,您是如何获得这些 zip 文件名的。意义通过 Get-ChildItem,或从某个文本文件中,使用相同的 cmdlet 搜索它们。

    不管怎样,过程都是一样的。

    如果我直接修改您的功能,并在此过程中修复一些问题。 试试这个...

    function Expand-ZipFilesWithCleanUp
    {
        [cmdletbinding()]
        [Alias('ezc')]
    
        Param
        (
            [string[]]$ZipFiles,
            [string]$Outdir,
            [string] $FilenameToRemove
        )
    
        # Instantiate the .Net namespace
        add-type -AssemblyName 'System.IO.Compression.filesystem'
    
        "The number of zip files passed in was $($ZipFiles.Count)"
    
        # Remove unwanted files
        foreach ($ZipFile in $ZipFiles)
        {
            # Open the zip for updating
            $ProcessZipFile = [io.compression.zipfile]::Open($ZipFile,'Update')
    
            "`nShow all files in the zip"
            $ProcessZipFile.Entries | Out-GridView -PassThru
    
            "`nDeleting unwanted file $FilenameToRemove from $ZipFile"
            ($ProcessZipFile.Entries | Where FullName -Match $FilenameToRemove).Delete()
    
            "`nValidate remove"
            $ProcessZipFile.Entries | Out-GridView -PassThru
    
            # Clean up / close the zip
            $ProcessZipFile.Dispose()
        }
    
        #//Begin unzip code
    
    
    
        #//End unzip code
    
    }
    Expand-ZipFilesWithCleanUp -ZipFiles (Get-ChildItem -Path 'D:\Temp' -Filter '*.zip').FullName -FilenameToRemove 'Test.clixml'
    

    再一次,那些..

    $ProcessZipFile.Entries | Out-GridView -PassThru
    

    ... 行不是必需的,它们只是一种弹出对话框的方式,以显示在删除或给定文件之前和之后 zip 中发生的情况。

    您现在要做的就是在提供的空白处添加您的其他代码。

    【讨论】:

    • 非常感谢您的帮助。我将更改我的函数以添加一些代码行(即:删除代码、验证删除和清理/关闭 zip)。我会让你知道结果。再次感谢,非常感谢您的帮助。
    • 不用担心,如果它解决了您的用例,请务必将此标记为已接受的答案,以便其他发现自己有类似需求的人受益。
    • 我一定会做到的。
    • 您好,我将您的一些代码行用于函数中。我改变了一点,但它对我不起作用。它显示一个错误,下面是我收到的错误。您不能在空值表达式上调用方法。在 E:\PowershellScript\test2.ps1:34 char:5 + ($tempz.Entries | Where FullName -Match 'abc.txt').Delete() + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
    • 我在这里遇到的问题是我有 22 个 zip 文件要提取到 22 个文件夹中。在您的示例中,您只有 1 个文件夹和 1 个 zip 文件。我将如何使用您的代码处理超过 1 个 zip 文件和 1 个文件夹?请帮帮我?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多