Link to home
Start Free TrialLog in
Avatar of Gary Dewrell
Gary DewrellFlag for United States of America

asked on

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()
}
Avatar of Mauro Cazabonnet
Mauro Cazabonnet
Flag of United States of America image

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

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
the setup program of winzip also allows you to make winzip the default tool for all zip formats (file extensions).

Sara
Avatar of Gary Dewrell

ASKER

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.
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
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.
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
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.
H:\>assoc .zip
.zip=WinZip
Changed it to CompressedFolder. DId not help.

H:\>assoc .zip
.zip=CompressedFolder
ASKER CERTIFIED SOLUTION
Avatar of Gary Dewrell
Gary Dewrell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.