missing file permission after creating zip file

by netzambo at 2012-10-09 05:57:27

Hi.
Using powershell script to create a zip file and put in it some files:


$ZipTimestamp = Get-Date -format yyyy-MM-dd-HH-mm-ss;
$ZipFileName = $DestZip + "Backup_" + $ZipTimestamp + ".zip"

set-content $ZipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

# Wait for the zip file to be created.
while (!(Test-Path -PathType leaf -Path $ZipFileName))
{
Start-Sleep -Milliseconds 20;
}

$ZipFile = (new-object -com shell.application).NameSpace($ZipFileName)

Write-Output (">> Waiting Compression : " + $ZipFileName);
$FileIdx = 0;
$NewArBkf = Get-ChildItem $Dest;
foreach($bkname in $NewArBkf) #$ArBkf)
{
Write-Output(">> Now compress : " + $bkname.FullName);
#$FileIdx += 1;
$ZipFile.CopyHere($bkname.FullName) | Out-Null
........


The script has ever worked well but at some point $ZipFile.CopyHere(…) started to give an error because the zipped file has permission and file owner completely empty; for empty i mean no permission and owner assigned to the file!!!

I have disabled antivirus without success; note that just after creating the file with "set-content $ZipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))" the file is ok but for some reason when CopyHere is invoked the file becomes without permissions.

Some suggestion?
by JeffH at 2012-10-09 06:47:43
I’m a little confused. When you copy the file are you saying it is copied without permissions? Are you creating the zip file on an NTFS partition?
by bobsdesk at 2012-10-09 10:23:46
How large are the files being zipped? Powershell does not wait for the .CopyHere to be completed. What is happening ion your script after the .CopyHere?
by netzambo at 2012-10-09 14:11:35
Thank you very much for your interest.
First of all:
(bobsdesk)
I’ve reported in the code the important part; yes i know .CopyHere it is async, after copy here there is a loop:

$ZipFile.CopyHere($bkname.FullName) | Out-Null

while($ZipFile.Items().Item($bkname.Name) -Eq $null)
{
Write-Output("----- " + $ZipFile.Items().Count);
Start-Sleep -Milliseconds 500;
}


(JeffH)
Yes, it is created on NTFS partition, but i give you a better explanation:

1) after set-content $ZipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) the file it is created and it is ok
2) something changes executing .CopyHere method and it fails exactly when called because at that point all permission is lost, even the file owner

It drive me crazy; i don’t understand what happens.
This thing happens on my customer pc (Windows 7 64 bit); on my pc (Windows 8 64 bit) something changes (the previously found readonly permission for generic Users group disappear and a new guid appear with full permission) but at least the other permission settings remain.
by JeffH at 2012-10-10 06:47:49
I get the same result. Digging.
by JeffH at 2012-10-10 07:06:41
I think the issue is the asynch nature. I have no problems adding a single file. But when using a ForEach loop I got the same errors. I threw in a healthy sleep between copy commands and everything worked. So something with your loop to wait on progress isn’t working as you expect.
by nohandle at 2012-10-12 01:14:48
I have come to the same conclusion as JeffH did, 10 ms of sleep on every iteration resolved the issue for me. But I would go with passing a collection of items to single function call.
$DestZip='C:\temp'
$Dest = "C:\Temp\test"
$ZipTimestamp = Get-Date -format yyyy-MM-dd-HH-mm-ss;
$ZipFileName = $DestZip + "Backup_" + $ZipTimestamp + ".zip"

set-content $ZipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

# Wait for the zip file to be created.
while (!(Test-Path -PathType leaf -Path $ZipFileName))
{
Start-Sleep -Milliseconds 20;
}

$ZipFile = (new-object -com shell.application).NameSpace($ZipFileName)
$NewArBkf = (new-object -com shell.application).NameSpace($dest)

Write-Output (">> Waiting Compression : " + $ZipFileName);
$ZipFile.CopyHere($NewArBkf.Items())