Skip to content

Instantly share code, notes, and snippets.

@lantrix
Last active February 24, 2021 16:31
Show Gist options
  • Save lantrix/738ebfa616d5222a8b1db947793bc3fc to your computer and use it in GitHub Desktop.
Save lantrix/738ebfa616d5222a8b1db947793bc3fc to your computer and use it in GitHub Desktop.

Revisions

  1. lantrix revised this gist Jul 12, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion portable-zip-pathnames.ps1
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@
    # Based upon post by Seth Jackson https://sethjackson.github.io/2016/12/17/path-separators/

    #
    # PowerShell 5 (WMF5)
    # PowerShell 5 (WMF5) & 6
    # Using class Keyword https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Core/about/about_Classes
    #

    Add-Type -AssemblyName System.Text.Encoding
    @@ -24,6 +25,7 @@ class FixedEncoder : System.Text.UTF8Encoding {

    #
    # PowerShell 4 (WMF4)
    # Using Add-Type cmdlet https://msdn.microsoft.com/en-us/powershell/reference/4.0/microsoft.powershell.utility/add-type
    #

    Add-Type -AssemblyName System.Text.Encoding
  2. lantrix revised this gist Jul 12, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion portable-zip-pathnames.ps1
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ Add-Type -AssemblyName System.Text.Encoding
    Add-Type -AssemblyName System.IO.Compression.FileSystem

    $EncoderClass=@"
    public class FixedEncoder : System.Text.UTF8Encoding {
    public class FixedEncoder : System.Text.UTF8Encoding {
    public FixedEncoder() : base(true) { }
    public override byte[] GetBytes(string s) {
  3. lantrix created this gist Jul 12, 2017.
    45 changes: 45 additions & 0 deletions portable-zip-pathnames.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # When using System.IO.Compression.ZipFile.CreateFromDirectory in PowerShell, it still uses backslashes in the zip paths
    # despite this https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/mitigation-ziparchiveentry-fullname-path-separator

    # Based upon post by Seth Jackson https://sethjackson.github.io/2016/12/17/path-separators/

    #
    # PowerShell 5 (WMF5)
    #

    Add-Type -AssemblyName System.Text.Encoding
    Add-Type -AssemblyName System.IO.Compression.FileSystem

    class FixedEncoder : System.Text.UTF8Encoding {
    FixedEncoder() : base($true) { }

    [byte[]] GetBytes([string] $s)
    {
    $s = $s.Replace("\\", "/");
    return ([System.Text.UTF8Encoding]$this).GetBytes($s);
    }
    }

    [System.IO.Compression.ZipFile]::CreateFromDirectory($dirToZip, $zipFilePath, [System.IO.Compression.CompressionLevel]::Optimal, $false, [FixedEncoder]::new())

    #
    # PowerShell 4 (WMF4)
    #

    Add-Type -AssemblyName System.Text.Encoding
    Add-Type -AssemblyName System.IO.Compression.FileSystem

    $EncoderClass=@"
    public class FixedEncoder : System.Text.UTF8Encoding {
    public FixedEncoder() : base(true) { }
    public override byte[] GetBytes(string s) {
    s = s.Replace("\\", "/");
    return base.GetBytes(s);
    }
    }
    "@
    Add-Type -TypeDefinition $EncoderClass

    $Encoder = New-Object FixedEncoder
    [System.IO.Compression.ZipFile]::CreateFromDirectory($dirToZip, $zipFilePath, [System.IO.Compression.CompressionLevel]::Optimal, $false, $Encoder)