【发布时间】:2021-09-09 17:20:07
【问题描述】:
早安,
我正在使用 Microsoft SSIS 执行从文件系统到 MS SQL 数据库的数据迁移,其中我使用的一些文件是 .zip 压缩文件夹。一些压缩文件夹已损坏,我想在导入过程中传递这些文件夹。
我创建的是一个 VB.Net 脚本任务,用于检查压缩文件是否有效,但我的 Try-Catch 块仅适用于第一个损坏的 zip 文件。我尝试了一些 Try-Catch 的变体,但我不知道如何在第二个损坏的 zip 之后继续执行该块。
这让我觉得我的 catch 块中需要进行一些垃圾收集,因为第一个异常填充了 The thread 0x000a has exited with code 0 (0x0). 的列表,然后是 Exception thrown: 'System.IO.InvalidDataException' in System.IO.Compression.FileSystem.dll,而第二个异常只列出了线程退出,没有异常扔了。
我尝试过的如下:
'Attempt to extract the zip contents
Try
System.IO.Compression.ZipFile.ExtractToDirectory(str_dirExtractFrom, str_dirExtractTo)
Catch ex As InvalidDataException
Console.WriteLine("Corrupted Zip " & str_dirExtractFrom)
End Try
'A helper function to determine if a zip file is valid:
Private Function IsValidZip(ByRef rstr_filePath As String) As Boolean
Dim zip_file As Compression.ZipArchive
Try
zip_file = System.IO.Compression.ZipFile.OpenRead(rstr_filePath)
'Added in attempts to force a reset
If Not zip_file Is Nothing Then
zip_file.Dispose()
Return True
End If
Catch ex As InvalidDataException
Return False
End Try
End Function
'Using both together
bool_validZip = IsValidZip(str_dirExtractFrom)
If bool_validZip Then
Try
System.IO.Compression.ZipFile.ExtractToDirectory(str_dirExtractFrom, str_dirExtractTo)
Catch ex As Exception
Console.WriteLine("Generic Exception from " & str_dirExtractFrom)
End Try
End If
奇怪的是,回到使用 On Error Resume Next 的 VBA 解决方案确实提供了我正在寻找的结果,但我怀疑这会对性能产生影响,因为每个损坏的 zip 开始需要更长的时间才能继续处理下一个文件。
我拥有的工作代码是一个更简单且看似不正确的解决方案:
On Error Resume Next
System.IO.Compression.ZipFile.ExtractToDirectory(str_dirExtractFrom, str_dirExtractTo)
If Err.Number <> 0 Then
Err.Clear()
Return False
On Error GoTo 0
有没有办法在这个脚本任务中使用 Try-Catch 块?我是否需要更改我的 SSIS 控制流以使脚本任务仅在一个文件上执行而不是查询目录?还是使用更简单的 Resume Next 的正确解决方案?
感谢您花时间阅读我的帮助请求,非常感谢您的帮助!
【问题讨论】:
标签: vb.net error-handling ssis