Gary Dewrell
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.fil esystem"
$destfile=[System.IO.Compr ession.Zip File]::Ope n($zipfile name, "Update")
$compressionLevel = [System.IO.Compression.Com pressionLe vel]::Opti mal
$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.Zip FileExtens ions]::Cre ateEntryFr omFile($de stfile,$fi le, $file2,$compressionlevel)
}
$destfile.Dispose()
}
function Add-ZipFile($zipfilename, $sourcedir)
{
Add-Type -assembly "system.io.compression.fil
$destfile=[System.IO.Compr
$compressionLevel = [System.IO.Compression.Com
$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.Zip
}
$destfile.Dispose()
}
if I use windows explorer to extract the fileyou 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
Sara
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 compressionthen 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
ASKER
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 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
ASKER
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.
ASKER
H:\>assoc .zip
.zip=WinZip
.zip=WinZip
ASKER
Changed it to CompressedFolder. DId not help.
H:\>assoc .zip
.zip=CompressedFolder
H:\>assoc .zip
.zip=CompressedFolder
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Open in new window