Adding a complete directory to an existing zip file with System.IO.Compression.FileSystem

前端 未结 1 1014
萌比男神i
萌比男神i 2021-01-22 05:56

The following example is retraceable on the internet and on this website as a sollution to compress files using the .NET Framework 4.5 It works, but when the archive already exi

1条回答
  •  旧巷少年郎
    2021-01-22 06:39

    Noam's answer and Jurjen:

    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
    $src_folder = "D:\stuff\" 
    $destfile = "D:\stuff.zip"
    $destfile2=[System.IO.Compression.ZipFile]::Open($destfile, "Update")
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    $in = Get-ChildItem $src_folder -Recurse | where {!$_.PsisContainer}| select -expand fullName
    [array]$files = $in
    ForEach ($file In $files) 
    {
            $file2 = $file #whatever you want to call it in the zip
            $null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($destfile2,$file,$file2,$compressionlevel)
    }
    $archiver.Dispose()
    

    0 讨论(0)
提交回复
热议问题