exception error while creating zipfile

994 views Asked by At

following code is raising exceptions.

1st exception is ok the file should not be there 2nd exception is also ok as the file either deleted or renamed or might already exist. however, i can't get rid of the third exception while creating zip file despite the fact that i use thread.sleep (thinking that the file might be still under-process) the error file is in use by another process is not resolved

        ' Create ZIP from "source" directory.
        Debug.Print("===========Starting Process=============")
        Try
            Debug.Print("===========Attempt to Delete File=============")
            My.Computer.FileSystem.DeleteFile("G:\abc\~tmp0000.tmp")
        Catch ex As Exception
            Debug.Print("===========Exception while DeleteFile=============")
            Debug.Print(ex.Message)
        End Try
        Debug.Print("===========finshed with Delete File=============")

        'Thread.Sleep(100)
        Try
            Debug.Print("===========Attempt to Rename File=============")
            My.Computer.FileSystem.RenameFile("~tmp.0000.zip", "destination.zip")
        Catch ex1 As Exception
            Debug.Print("===========Exception while RenameFile=============")
            Debug.Print(ex1.Message)
        End Try
        Debug.Print("===========finshed with Rename File=============")
        Thread.Sleep(100)
        Debug.Print("===========finshed with sleep=============")

        Try
            Debug.Print("===========Attempt to CreateFromDirectory File=============")
            ZipFile.CreateFromDirectory("g:\abc", "g:\abc\~tmp0000.tmp")
        Catch ex2 As Exception
            Debug.Print("===========Exception while CreateFromDirectory=============")
            Debug.Print(ex2.Message)
        End Try
        Debug.Print("===========finshed with Rename File=============")
        Thread.Sleep(100)
        Debug.Print("===========finshed with sleep=============")
        ' Extract ZIP to "destination" folder.
        Try
            Debug.Print("===========Attempt to Extract File=============")
            ZipFile.ExtractToDirectory("g:\abc\destination.zip", "g:\abc\destination")
        Catch ex2 As Exception
            Debug.Print("===========Exception while ExtractToDirectory=============")
            Debug.Print(ex2.Message)
        End Try
        Debug.Print("===========Completed Process=============")

</pre>

------------------exceptions log --------------------------------       

<pre>
    ===========Starting Process=============
    ===========Attempt to Delete File=============
    ===========finshed with Delete File=============
    ===========Attempt to Rename File=============
    A first chance exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.VisualBasic.dll
    ===========Exception while RenameFile=============
    Could not find file 'g:\Debug\~tmp.0000.zip'.
    ===========finshed with Rename File=============
    ===========finshed with sleep=============
    ===========Attempt to CreateFromDirectory File=============
    A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
    ===========Exception while CreateFromDirectory=============
    The process cannot access the file '\\..some path striped-out...\Debug\~tmp0000.tmp' because it is being   used by another process.
    ===========finshed with Rename File=============
    ===========finshed with sleep=============
    ===========Attempt to Extract File=============
    A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.IO.Compression.FileSystem.dll
    ===========Exception while ExtractToDirectory=============
    Could not find file 'g:\abc\destination.zip'.
    ===========Completed Process=============
1

There are 1 answers

0
dbasnett On BEST ANSWER

If you are trying to zip a directory take a look at this. Using your example:

    ZipDir("g:\abc", "destination.zip")


''' <summary>
''' zips a directory
''' </summary>
''' <param name="dirToZip">the directory to zip</param>
''' <param name="ZipFileName">the name of the zip file to create e.g. docs.zip </param>
''' <param name="ZipFileLocation">the directory to place the zipped file.  if not specified use the directory being zipped</param>
''' <returns>path to the zipped file if success</returns>
''' <remarks></remarks>
Public Function ZipDir(dirToZip As String, ZipFileName As String, _
                       Optional ZipFileLocation As String = "") As String
    'some preliminary checks
    Dim rv As String = ""
    If IO.Directory.Exists(dirToZip) Then
        If ZipFileLocation = "" Then
            ZipFileLocation = dirToZip
        End If
        If IO.Directory.Exists(ZipFileLocation) AndAlso IO.Path.GetExtension(ZipFileName).ToLower = ".zip" Then
            'destination
            Dim zipDest As String = IO.Path.Combine(ZipFileLocation, IO.Path.GetFileName(ZipFileName))
            If IO.File.Exists(zipDest) Then
                Try
                    IO.File.Delete(zipDest)
                Catch ex As Exception
                    Throw
                End Try
            End If
            'ready to zip, create zip file locally and then move
            Dim tmpDir As String = Environment.GetEnvironmentVariable("TEMP")
            Dim guidfn As String = Guid.NewGuid.ToString & ".zip"
            guidfn = IO.Path.Combine(tmpDir, guidfn)
            Try
                IO.Compression.ZipFile.CreateFromDirectory(dirToZip, guidfn)
                IO.File.Move(guidfn, zipDest)
                rv = zipDest
            Catch ex As Exception
                Throw
            End Try
        End If
    End If
    Return rv
End Function