创建zipfile时出现异常错误

时间:2015-06-20 11:44:46

标签: vb.net vb.net-2010

以下代码引发异常。

第一个例外是好的,文件不应该在那里 第二个例外也可以,因为文件已删除或重命名或可能已存在。 但是,尽管我使用thread.sleep(认为该文件可能仍处于进程中),但是我正在创建zip文件时我无法摆脱第三个异常错误文件正在被另一个进程使用解决

        ' 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 个答案:

答案 0 :(得分:0)

如果您要尝试压缩目录,请查看此内容。使用您的示例:

    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