创建 zipfile 时出现异常错误
exception error while creating zipfile
以下代码引发异常。
第一个异常正常,文件不应该存在
第二个例外也可以,因为文件已删除或重命名或可能已经存在。
但是,尽管我使用 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=============
如果您正在尝试压缩目录,请查看此内容。使用您的示例:
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
以下代码引发异常。
第一个异常正常,文件不应该存在 第二个例外也可以,因为文件已删除或重命名或可能已经存在。 但是,尽管我使用 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=============
如果您正在尝试压缩目录,请查看此内容。使用您的示例:
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