7

I'm trying to add a file to a zip file using powershell

I can create the zip file but can't work out how to add my file to it

I'm using

$zipfilename = 'c:\cwRsync\backup.zip'
$file = 'c:\cwRsync\backup.log'
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
$zipfile = (New-Object -ComObject shell.application).NameSpace($zipfilename)
$zipfile.MoveHere($file.FullName)

This create the zip file but doesn't add the new file

I tried the following code I found on stackoverflow which works

$zipfilename = "a.zip"
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
$app = new-object -com shell.application
$zip = ( get-item a.zip ).fullname
$folder = $app.namespace( $zip )
$item = new-item file.txt -itemtype file -value "Mooo" -force
$folder.copyhere( $item.fullname )

but that add a file created by powershell whereas I want to add an existing file

Any help would be much appreciated

mjsolo
  • 195
  • 2
  • 5
  • 18
  • got it working by changing $zipfile.MoveHere($file.FullName) to $zipfile.MoveHere($file) and adding a sleep 1 at the end of the script, issue appears to have been caused as i was calling the script from command line – mjsolo Dec 06 '12 at 17:05
  • http://stackoverflow.com/questions/39496251/how-to-move-a-single-txt-file-to-the-zip-in-powershell-3 – JohnLBevan May 19 '17 at 11:08

3 Answers3

13

To create a zip archive:

powershell Compress-Archive %file% %file%.zip

To replace the archive (with a different file):

powershell Compress-Archive -force %different_file% %file%.zip

To add a file to the (existing) archive:

powershell Compress-Archive -update %2nd_file% %file%.zip

Tested on Win 10 CMD Powershell 5.1

Zimba
  • 2,854
  • 18
  • 26
  • 2
    This is the correct answer IMO, thank you very much, you helped me greatly :) We only lack a clear way to remove a file from an existing archive to perfection. – gaazkam Jun 23 '21 at 10:28
  • You're welcome. The [Compress-Archive cmdlet](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive?view=powershell-7.1) uses the Microsoft .NET API System.IO.Compression.ZipArchive to compress files anyway, so files still can be [removed from archives](https://gist.github.com/alantsai/7205e3a5513a66e2d1ae162ad23054e9) via powershell. – Zimba Jun 23 '21 at 11:09
1

The CopyHere function just takes a string that is the path to your file. For example:

$folder.copyhere( "c:\PathToYourFile\YourFile" )

Edit: The Powershell Pack mentioned in the tip below is no longer available. I leave it here in case someone can find it archived somewhere and can update this post with a link.

Tip:

The Powershell Pack Module has some useful tools, one of which is a zip utility, which will reduce your code to 1 line:

Copy-ToZip "c:\PathToYourFile\YourFile" -ZipFile a.zip
zdan
  • 28,667
  • 7
  • 60
  • 71
-2

I pulled this from my notes. Got it off the scripting guy's website, maybe this will help...

$source = "D:\temp\test"
$destination = "d:\temp\csvdir_Backup.zip"
If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 
zx485
  • 28,498
  • 28
  • 50
  • 59
Ryan
  • 7
  • 8
    Welcome to Stack Overflow! This looks like a try-this-and-let-me-know-how-it-goes response, which belongs as a comment rather than an answer. – Joe C Jan 02 '17 at 22:20