Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 创建zipfile时出现异常错误_Vb.net_Vb.net 2010 - Fatal编程技术网

Vb.net 创建zipfile时出现异常错误

Vb.net 创建zipfile时出现异常错误,vb.net,vb.net-2010,Vb.net,Vb.net 2010,下面的代码引发异常 第一个例外是正常的文件不应该在那里 第二个异常也可以,因为该文件已被删除或重命名,或者可能已经存在。 然而,尽管我使用thread.sleep(认为该文件可能仍在处理中),但在创建zip文件时,我无法消除第三个异常。错误文件正在被另一个进程使用,并且没有得到解决 ' Create ZIP from "source" directory. Debug.Print("===========Starting Process============="

下面的代码引发异常

第一个例外是正常的文件不应该在那里 第二个异常也可以,因为该文件已被删除或重命名,或者可能已经存在。 然而,尽管我使用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=============
”从“源”目录创建ZIP。
Debug.Print(“===================启动进程=================”)
尝试
调试.打印(“================尝试删除文件=================”)
My.Computer.FileSystem.DeleteFile(“G:\abc\~tmp000.tmp”)
特例
Debug.Print(“================删除文件时异常=================”)
调试.打印(例如消息)
结束尝试
Debug.Print(“===================finshed with Delete File===================”)
'线程.睡眠(100)
尝试
Debug.Print(“===================尝试重命名文件=================”)
My.Computer.FileSystem.RenameFile(“~tmp.0000.zip”、“destination.zip”)
捕获ex1作为异常
Debug.Print(“================重命名文件时出现异常====================”)
Debug.Print(ex1.Message)
结束尝试
Debug.Print(“===================使用重命名文件查找=================”)
线程。睡眠(100)
Debug.Print(“====================================================”)
尝试
Debug.Print(“================尝试创建FromDirectory文件====================”)
CreateFromDirectory(“g:\abc”,“g:\abc\~tmp0000.tmp”)
捕获ex2作为异常
Debug.Print(“================CreateFromDirectory时异常=======================”)
Debug.Print(ex2.Message)
结束尝试
Debug.Print(“===================使用重命名文件查找=================”)
线程。睡眠(100)
Debug.Print(“====================================================”)
'将ZIP解压缩到“目标”文件夹。
尝试
Debug.Print(“================尝试提取文件=================”)
ZipFile.ExtractToDirectory(“g:\abc\destination.zip”,“g:\abc\destination”)
捕获ex2作为异常
Debug.Print(“================提取到目录时出现异常====================”)
Debug.Print(ex2.Message)
结束尝试
调试.打印(“===================已完成的进程=================”)
------------------异常日志------------------
    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
=============开始处理============= =============尝试删除文件============= ============使用删除文件查找============= ==========尝试重命名文件============= Microsoft.VisualBasic.dll中发生类型为“System.IO.FileNotFoundException”的第一次意外异常 =========重命名文件时出现异常============= 找不到文件“g:\Debug\~tmp.0000.zip”。 ============使用重命名文件查找============= ===============因睡眠而结束============= ==========尝试创建FromDirectory文件============= mscorlib.dll中发生类型为“System.IO.IOException”的第一次意外异常 =========CreateFromDirectory时出现异常============= 进程无法访问文件“\\…某些路径被剥离…\Debug\~tmp0000.tmp”,因为另一个进程正在使用该文件。 ============使用重命名文件查找============= ===============因睡眠而结束============= ============尝试提取文件============= System.IO.Compression.FileSystem.dll中发生了类型为“System.IO.FileNotFoundException”的第一次意外异常 =========提取到目录时出现异常============= 找不到文件“g:\abc\destination.zip”。 =============已完成的流程=============
如果您试图压缩目录,请查看以下内容。以您的例子:

ZipDir(“g:\abc”,“destination.zip”) ''' ''打开一个目录 ''' ''要压缩的目录 ''要创建的zip文件的名称,例如docs.zip ''放置压缩文件的目录。如果未指定,请使用正在压缩的目录 如果成功,则返回压缩文件的“”路径 ''' 公共函数ZipDir(dirToZip作为字符串,ZipFileName作为字符串_ 可选ZipFileLocation作为字符串“”)作为字符串 "一些初步检查, Dim rv As String=“” 如果IO.Directory.Exists(dirToZip),则 如果ZipFileLocation=“”,则 ZipFileLocation=dirToZip 如果结束 如果IO.Directory.Exists(ZipFileLocation)和IO.Path.GetExtension(ZipFileName.ToLower=“.zip”)都存在,那么 “目的地 Dim zipDest As String=IO.Path.Combine(ZipFileLocation,IO.Path.GetFileName(ZipFileName)) 如果IO.File.Exists(zipDest),则 尝试 IO.File.Delete(zipDest) 特例 投 结束尝试 如果结束 '准备压缩,在本地创建压缩文件,然后移动 Dim tmpDir作为字符串=Environment.GetEnvironmentVariable(“TEMP”) Dim guidfn As String=Guid.NewGuid.ToString&“.zip” guidfn=IO.Path.Combine(tmpDir,guidfn) 尝试 IO.Compression.ZipFile.CreateFromDirectory(dirToZip,guidfn) IO.File.Move(guidfn,zipDest) rv=zipDest 特例 投 结束尝试 如果结束 如果结束 返回rv 端函数