Zip folder with empty directories

Is there a script example that would show how to include empty directories into the final zip file?

Thanks

February 17th, 2015 1:37pm

PSCX has Write-Zip, which I believe handles folder structure:

http://pscx.codeplex.com/

I haven't used this module personally, so I'm not sure if this will meet your needs.

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 2:18pm

Try to use built in function in WinRAR
February 17th, 2015 2:54pm

But I dont want to use any third party software. My question is about PS.
Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 4:44pm


Prewritten scripts can be found here:

http://gallery.technet.microsoft.com/scriptcenter

Otherwise start playing around with the ZipFile class:

https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile%28v=vs.110%29.aspx

February 17th, 2015 4:49pm

I downloaded it from http://pscx.codeplex.com and ran installation. What do I next to have it working because I tried help write-zip and nothing came back.

Thanks


Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 4:50pm

But I dont want to use any third party software. My question is about PS.

There is no native support for ZIPs in PowerShell.  You must write your own from scratch or use a prewritten script.

February 17th, 2015 4:50pm

I downloaded it from http://pscx.codeplex.com and ran installation. What do I next to have it working because I tried help write-zip and nothing came back.

Thanks



Go back and follow the instructions for loading and using PSCX extensions.  You will also need to learn how to use PowerShell before you can understand the instructions.
Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 4:52pm

If you mean this crap:

two of three links dont work.

February 17th, 2015 4:55pm

Those links are a bit old.

Use these:
https://msdn.microsoft.com/en-US/library/ms229284(v=vs.80).aspx

https://msdn.microsoft.com/en-US/library/ms229335(v=vs.80).aspx

You have to do much of this for yourself. Thisis a technical forum and assumes a certain amount of technical knowledge of Windows an computing,

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 5:15pm

This requires Net FrameWork 4.5

add-type -AssemblyName System.IO.Compression.FileSystem
$folder='c:\test'
$archive='c:\temp2\test.zip'
[System.IO.Compression.ZipFile]::CreateFromDirectory($folder,$archive,'Optimal',$true)

February 17th, 2015 5:26pm

I downloaded it from http://pscx.codeplex.com and ran installation. What do I next to have it working because I tried help write-zip and nothing came back.

Thanks


  • Edited by markgoldin Tuesday, February 17, 2015 9:45 PM
Free Windows Admin Tool Kit Click here and download it now
February 18th, 2015 12:45am

I suppose your zip file would be performed by 7-zip or something like.
February 18th, 2015 6:06pm

Here is what I got so far:

Write-Output "Packaging a Package"

#get output directory
$outDir=$args[0]

$shell = new-object -com shell.application


$aZipPath = "${outDir}Installs\aZip.zip"

#the second part sets the content type of the file to zip
set-content $aZipPath ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

$ZipFile = $shell.NameSpace($aZipPath)

Get-ChildItem "${outDir}_PublishedWebsites\a_Package\Archive"|
                    foreach({   $copyfile = $_;
                $ZipFile.CopyHere($copyfile.fullname);

                #Due to some limitations with CopyHere method, we need to sleep until the file has finished copying.
                                #If not, we will get an error and some files will not be copied.
                                while (!($ZipFile.Items() | Where-Object { $_.Path -Match $copyfile.Name})) { Start-Sleep -m 500};
                            });

Write-Output "Complete"
Kind of works.... Can't zip empty directories. 

Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 9:16am

The shell was never intended to be used this way.  It does not work reliably and will never work reliably.

The following works under all circumstances:

add-type -AssemblyName System.IO.Compression.FileSystem
$folder='c:\test'
$archive='c:\temp2\test.zip'
[System.IO.Compression.ZipFile]::CreateFromDirectory($folder,$archive,'Optimal',$true)

February 19th, 2015 9:23am

I am getting:

Add-Type : Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found.

I am not a c# developer, sorry.

Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 11:19am

Are you running PowerShell V1 on XP?

February 19th, 2015 11:33am

No, I am running it on Windows 7. This is my command line:

PS C:\Program Files (x86)\Microsoft Visual Studio 11.0> powershell -noexit -ExecutionPolicy ByPass "& ""d:\Package.ps1""" 

Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 11:37am

I am getting:

Add-Type : Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found.

I am not a c# developer, sorry.

This has already been mentioned, but I'm going to point it out again.

This requires a minimum of .Net 4.5.

February 19th, 2015 11:38am

Here are all versions being installed:

C:\Program Files (x86)\Microsoft Visual Studio 11.0>wmic /namespace:\\root\cimv path win32_product where "name like '%%.NET%%'" get version
Version
4.5.53341
4.5.50932
10.0.30319
2.0.50217.0
3.0.40218.0
4.5.50710
4.0.20710.0
2.0.20715.0
4.5.50932
4.5.21005
4.5.50710
1.0.20710.0
4.5.53341
1.0.21925
2.0.50217.0
4.5.21005
3.11.50727
3.0.20105.0
4.1.20219.0
2.0.20525.0
14.0.10922.0
4.0.30319
2.0.3010.0
3.0.30710.0
4.5.53341
1.4.41009.0
4.1.20219.0
1.0.21925
4.5.51641
1.0.20105.0
5.2.20806.0

Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 11:50am

Open a PowerShell console and type this line only:

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

What happens?

February 19th, 2015 11:53am

Windows PowerShell

Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS H:\> Add-Type -AssemblyName System.IO.Compression.FileSystem
Add-Type : Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found.
At line:1 char:9
+ Add-Type <<<<  -AssemblyName System.IO.Compression.FileSystem
    + CategoryInfo          : ObjectNotFound: (System.IO.Compression.FileSystem:String) [Add-Type], Exception
    + FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. One or more required assemblies are missing.
At line:1 char:9
+ Add-Type <<<<  -AssemblyName System.IO.Compression.FileSystem
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

PS H:\>

Am I doing it correctly at all?

Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 11:57am

Do yourself a huge favor and upgrade to WMF4. v4 is much better th

February 19th, 2015 12:02pm

No, I am running it on Windows 7. This is my command line:

PS C:\Program Files (x86)\Microsoft Visual Studio 11.0> powershell -noexit -ExecutionPolicy ByPass "& ""d:\Package.ps1""" 

What versio of PowerShell?

You have Net 4.5. Perhaps you are missing service packs..

It won't work with PowerShell V2.

Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 12:03pm

Windows PowerShell

Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS H:\> Add-Type -AssemblyName System.IO.Compression.FileSystem
Add-Type : Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found.
At line:1 char:9
+ Add-Type <<<<  -AssemblyName System.IO.Compression.FileSystem
    + CategoryInfo          : ObjectNotFound: (System.IO.Compression.FileSystem:String) [Add-Type], Exception
    + FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. One or more required assemblies are missing.
At line:1 char:9
+ Add-Type <<<<  -AssemblyName System.IO.Compression.FileSystem
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

PS H:\>

Am I doing it correctly at all?

This - "Copyright (C) 2009 Microsoft Corporation. All rights reserved." says you are running an old version of PowerShell.  You need to upgrade.
February 19th, 2015 12:04pm

It won't work with PowerShell V2.

Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 12:05pm

PowerShell V3 and later allow loading almost any version of any assembly with no conflict although you cannot load two type of different versions without adjusting for this. V2 only allows V2 and compatible earlier assemblies.  I do not remember what was changed to make this happen.

4.5 compression is very useful and easy to use. V5 will have full compression support with real CmdLets.

February 19th, 2015 12:10pm

Thanks to all of you, working now.
Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 12:23pm

Cheers, you're welcome. Glad it worked out.
February 19th, 2015 12:28pm

try to repair your zip folder, I had the same issue.
Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 7:30pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics