Save failing, zip in use by another process

Jan 12, 2012 at 6:36 PM

I'm trying to archive log files, and am getting the following error occasionally (not every run, but maybe 1 in 2 or 1 in 3 runs):

Exception calling "Save" with "0" argument(s): "The process cannot access the file because it is being used by another process."
At C:\scripts\test3.ps1:11 char:17
+         $zf.Save <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

The code in question looks like this:

    $zf = new-object Ionic.Zip.ZipFile $zfile
    if($zf.ContainsEntry($p2zip) -ne $true)
        $out = $zf.AddFile($p2zip)
    Start-sleep -milliseconds 500
where $zfile is the zip filename, $p2zip is the fully-qualified filename to add.
The start-sleep eliminates 90% of these errors, but it's a bad way to approach this. I've read where these operations are supposed to be synchronous, so it's not that, but maybe the filesystem or Windows aren't quite done with the zip file yet. Is there something i need to check to ensure the file is done? Or am i missing something else causing the error? Help!
Thanks! -bill