Create zip from single file in PSv4

Welcome Forums General PowerShell Q&A Create zip from single file in PSv4

This topic contains 6 replies, has 4 voices, and was last updated by

js
 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #97274

    Participant
    Topics: 13
    Replies: 25
    Points: 29
    Rank: Member

    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

    Participant
    Topics: 18
    Replies: 244
    Points: 109
    Rank: Participant
    • #97316

      Participant
      Topics: 13
      Replies: 25
      Points: 29
      Rank: Member

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

  • #97293
    js

    Participant
    Topics: 21
    Replies: 592
    Points: 1,149
    Helping Hand
    Rank: Community Hero

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

  • #97373

    Participant
    Topics: 2
    Replies: 933
    Points: 1,611
    Helping Hand
    Rank: Community Hero

    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

    Participant
    Topics: 18
    Replies: 244
    Points: 109
    Rank: 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
    Topics: 21
    Replies: 592
    Points: 1,149
    Helping Hand
    Rank: Community Hero

    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.

The topic ‘Create zip from single file in PSv4’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort