Issues using System.IO.Compression.ZipFile with powershell.

This code works. The zip file is created and in the explorer preview I can see the structure and even navigate through and even open files within the zip. I can open and extract the contents of the file with winzip. However, if I use windows explorer to extract the file I get invalid compressed folder.  If I double click on the file I just get an explorer window that says empty.

function Add-ZipFile($zipfilename, $sourcedir)
{
    Add-Type -assembly "system.io.compression.filesystem"
    $destfile=[System.IO.Compression.ZipFile]::Open($zipfilename, "Update")
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    $in = Get-ChildItem $Sourcedir  -Recurse | where {!$_.PsisContainer} | select -expand fullName
    [array]$files = $in
    ForEach ($file In $files)
        {
            $file2 = $file
            write-host "adding  $file as $file2 to archive $destfile"
            $null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($destfile,$file, $file2,$compressionlevel)
        }
    $destfile.Dispose()
}
LVL 12
Gary DewrellSenior Network AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mauro CazabonnetSenior Software EngineerCommented:
I've had success with gzip.exe and gunzip.exe

$gzip = "d:\temp\gzip.exe"

#Compress file
try
{
    $zipfile = Start-Process $gzip $file -Wait -NoNewWindow -PassThru -ErrorAction silentlyContinue; $zipfile.WaitForExit() | Out-Null
}
catch 
{
    $Return = $zipfile.ExitCode
}

Open in new window

0
Mauro CazabonnetSenior Software EngineerCommented:
0
sarabandeCommented:
if I use windows explorer to extract the file
you could make winzip your default zip tool in explorer. right-click on a zip file in explorer (perhaps you may additionally have the shift key pressed), choose 'Open with ...' -> 'Standard ...', set winzip as the default zip tool.

Sara
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

sarabandeCommented:
the setup program of winzip also allows you to make winzip the default tool for all zip formats (file extensions).

Sara
0
Gary DewrellSenior Network AdministratorAuthor Commented:
Unfortunately one of the requirements is that it must be able to use windows internal compression which is why I chose the .net assembly in the first place. Otherwise I would be using 7-zip.
0
sarabandeCommented:
it must be able to use windows internal compression
then you should use windows zip compression when creating the zip file. that means you shouldn't have defined winzip as your default zip tool when creating the zip file.

you also could try to use 'normal' compression rather than 'optimal'.

Sara
0
Gary DewrellSenior Network AdministratorAuthor Commented:
I did not use winzip to create or add to the file. Simply to test if it could open the zip file when explorer could not.

I will try the normal compression.
0
sarabandeCommented:
I did not use winzip to create or add to the file.

in my opinion windows uses the standard tool for the required (zip) file extension.

type 'assoc .zip' in a command window and you will see which tool is associated to it.

Sara
0
Gary DewrellSenior Network AdministratorAuthor Commented:
Changing the compression level to normal had no affect.  Very weird that is works in preview windows but not if you open it directly. Gotta love Microsoft.
0
Gary DewrellSenior Network AdministratorAuthor Commented:
H:\>assoc .zip
.zip=WinZip
0
Gary DewrellSenior Network AdministratorAuthor Commented:
Changed it to CompressedFolder. DId not help.

H:\>assoc .zip
.zip=CompressedFolder
0
Gary DewrellSenior Network AdministratorAuthor Commented:
Finally broke down and opened a premier case with Microsoft. The issue was related to passing the full path of the file to be compressed. Apparently Windows does not do a good job supporting relative paths.

Please close this case.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gary DewrellSenior Network AdministratorAuthor Commented:
Finally broke down and opened a premier case with Microsoft. The issue was related to passing the full path of the file to be compressed. Apparently Windows does not do a good job supporting relative paths.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.