【发布时间】:2021-07-20 04:31:07
【问题描述】:
我有下面的脚本,它将获取一个 .gz 文件并解压缩它。但是,每当我尝试使用此代码时,我都无法让脚本进行验证。
错误消息 - 未声明名称 Dts。
请问有人可以帮忙吗?我很少在 SSIS 中使用脚本任务。
Imports System
Imports System.IO
Imports System.Data
Imports System.Math
Imports System.Text
Imports System.IO.Compression
Imports Microsoft.SqlServer.Dts.Runtime
'''<summary>Decompress file</summary>
Public Class ScriptMain
Public Sub Main()
Dts.TaskResult = Dts.Results.Success
Dim success As Boolean = True
Dim workFilePath As String
workFilePath = Dts.Variables("User::sBloombergReplyFileFullPathUR0L").Value.ToString()
If File.Exists(workFilePath) Then
If Not workFilePath.EndsWith(".gz") Then
Dts.Events.FireInformation(0, "", workFilePath + " is not compressed; skipping decompression", Nothing, -1, True)
Return
End If
Dim uncompressedFileName As String
Dim bytes(Int16.MaxValue) As Byte
Dim n As Integer = 1
Try
uncompressedFileName = workFilePath.Substring(0, workFilePath.Length - 3)
Dts.Events.FireInformation(0, "", "decompressing " + workFilePath + " to " + uncompressedFileName, Nothing, -1, True)
Using writer As New FileStream(uncompressedFileName, FileMode.Create)
Using compressedStream As Stream = File.Open(workFilePath, FileMode.Open, FileAccess.Read, FileShare.None)
Using unzipper As New GZipStream(compressedStream, CompressionMode.Decompress)
Do Until n = 0
n = unzipper.Read(bytes, 0, bytes.Length)
writer.Write(bytes, 0, n)
Loop
unzipper.Close()
End Using
compressedStream.Close()
End Using
writer.Close()
End Using
Catch ex As Exception
Dts.Events.FireError(0, ex.TargetSite().ToString(), "Unable to decompress " + workFilePath + "; " + ex.Message, Nothing, -1)
success = False
Finally
If success = False And File.Exists(uncompressedFileName) Then
Dts.TaskResult = Dts.Results.Failure
File.Delete(uncompressedFileName)
End If
End Try
Else
Dts.Events.FireError(0, "", workFilePath + " does not exist", Nothing, -1)
Dts.TaskResult = Dts.Results.Failure
Return
End If
End Sub
End Class
【问题讨论】: