How to remove folder structure from zip RRS feed

  • Question

  • Hi , 

    I am using below powershell script to zip a file. code is working fine, issue is it zipping the file with  folder, I would like to zip the file without folder structure, can any one help.

    just want create a zip file which should only contain file.txt, with below when i unzip the file, file.txt is in test folder. any help much appreciated.

    Here is the code

    # First we add the .Net framework class needed for file compression.
    Add-Type -As System.IO.Compression.FileSystem
    # Then we need a variable of the type System.IO.Compression.CompressionLevel. 
    # The options for compression level are "Fastest", "Optimal" and "NoCompression".
    [System.IO.Compression.CompressionLevel]$compression = "Optimal"
    # Which file do you want to compress?
    $file = 'C:\test\file.txt'
    # Set the path to where you want the zip file to be created.
    $zippath = 'C:\test\path\file.zip'
    # Open the zip file and set the mode. Options for mode are "Create", "Read" and "Update".
    $ziparchive = [System.IO.Compression.ZipFile]::Open( $zippath, "Update" )
    # The compression function likes relative file paths, so lets do that.
    $relativefilepath =  (Resolve-Path $file -Relative).TrimStart(".\")
    # This is where the magic happens. 
    # Compress the file with the variables you just created as parameters.
     $null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ziparchive, $file, $relativefilepath, $compression)
    # Release the zip file. 
    # Otherwise the file will still be in read only if you are using Powershell ISE.

    Tuesday, December 12, 2017 7:43 PM

All replies

  • We don't fix scripts found on the Internet.

    To zip a single file:

    $file = '.\file.xml'
    $zipfile = 'myzipper.zip'
    Compress-Archive -Path $file -DestinationPath $zipfile


    Tuesday, December 12, 2017 8:12 PM
  • Thanks for the response Jrv, before implementing on production, I was trying hands on test scripts. :
    Tuesday, December 12, 2017 8:51 PM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, December 13, 2017 6:06 AM
  • I suggest you try Long Path Tool program for removing folder structure from zipping. It will help.

    Tuesday, December 26, 2017 3:49 PM