Create zip from single file in PSv4

This topic contains 6 replies, has 4 voices, and was last updated by  js 3 months, 3 weeks ago.

  • Author
    Posts
  • #97274

    Nathan W
    Participant

    Hey all,
    I am looking for a script to create a zip from a single file on PSv4

    I have been looking and not found any that make since. I have a script that created a extract from a database that is over 300mb and I want to automate compressing it and emailing it. I have all the other parts working except the zip.

    Thanks!!!

  • #97277

    Simon B
    Participant
    • #97316

      Nathan W
      Participant

      Sadly that approach does not work. created a .zip but with nothing in it 🙁

  • #97293

    js
    Participant

    I would use the command line version of 7 zip. Support in powershell or .net isn't great.

  • #97373

    postanote
    Participant

    It all depends on what version of PoSH you are on.
    Prior to v5 you had to us .Net, v5 and higher has cmdlets for this.

        # Get parameters, examples, full and Online help for a cmdlet or function
    
        # Get a list of all functions
        Get-Command -CommandType Function | Format-Table -AutoSize
    
        # Get a list of all commandlets
        Get-Command -CommandType Cmdlet | Format-Table -AutoSize
    
        # Get a list of all functions for the specified name
        Get-Command -Name '*archive*' -CommandType Function | Format-Table -AutoSize
    
        CommandType Name             Version Source                      
        ----------- ----             ------- ------                      
        Function    Compress-Archive 1.0.1.0 Microsoft.PowerShell.Archive
        Function    Expand-Archive   1.0.1.0 Microsoft.PowerShell.Archive
    
        # Get a list of all commandlets for the specified name
        Get-Command -Name '*archive**'  -CommandType Cmdlet | Format-Table -AutoSize
    
        # get function / cmdlet details
        (Get-Command -Name Compress-Archive).Parameters
        Get-help -Name Compress-Archive -Examples
        Get-help -Name Compress-Archive -Full
        Get-help -Name Compress-Archive -Online
    
    
        Get-Help about_*
        Get-Help about_Functions
    
        # Find all cmdlets / functions with a target parameter
        Get-Help * -Parameter Append
    
        Get-Command -CommandType cmdlet `
        | Where-Object { $_.parameters.keys -match 'credential'} `
        | Format-Wide name -AutoSize 
    
        # All Help topics locations
        explorer "$pshome\$($Host.CurrentCulture.Name)"
    

    Other pre-built stuff to use...

    Use PowerShell to Create ZIP Archive of Folder
    'blogs.technet.microsoft.com/heyscriptingguy/2015/03/09/use-powershell-to-create-zip-archive-of-folder'

    7Zip4PowerShell 1.8.0
    Powershell module for creating and extracting 7-Zip archives
    'powershellgallery.com/packages/7Zip4Powershell/1.8.0'

  • #97401

    Simon B
    Participant

    This worked for me

    $source = "C:\Temp"

    $destination = "C:\backup\FSO_Backup.zip"

    If(Test-path $destination) {Remove-item $destination}

    Add-Type -assembly "system.io.compression.filesystem"

    [io.compression.zipfile]::CreateFromDirectory($Source, $destination)

  • #97445

    js
    Participant

    Compress-archive in PS5 doesn't preserve the path in this case:

    Get-ChildItem -recurse .\foo\ | Compress-Archive -DestinationPath foo.zip
    

    Oh, it does except for the top level folder.

    EDIT: You can zip a lot of duplicate files this way, since every folder will automatically be zipped recursively.

You must be logged in to reply to this topic.