如何将Ole对象提取到磁盘

时间:2015-05-08 15:51:09

标签: sql-server vb.net winforms blob

我有一个带有VB .NET的Windows窗体应用程序和带有Access数据库数据的MS SQL Server数据库。这里我有一个问题是从Blob中提取Ole对象到磁盘上的文件。在表格中我没有输入文件名,因为我不知道文件类型(扩展名)。我有一个功能很好,但只有Word和Excel文件,但在表中我也有PDF和Zip文件。

有谁能说我错在哪里? 或者请给我一些想法如何将Ole对象提取到磁盘。

谢谢!

这是功能:

Public Sub DownloadOleObject(ByVal blob() As Byte, ByRef path As String, ByRef fileName As String)
'http://dotnet.itags.org/dotnet-tech/112155/
Dim ms As New MemoryStream
ms.Write(blob, 0, blob.Length)
ms.Seek(0, SeekOrigin.Begin)
Dim firstByte As Integer = ms.ReadByte()
Dim secondByte As Integer = ms.ReadByte()

Dim fileTypeLoc As Integer = 20 'begin of the file type
Dim offset As Short 'end of the file type

Dim buffer(2) As Byte
ms.Read(buffer, 0, 2)
offset = BitConverter.ToInt16(buffer, 0)

Dim seekTotal As Long = 0
seekTotal += offset

Dim docType As String = String.Empty
Dim myChar As Char
For i As Integer = fileTypeLoc To 45 'offset
  myChar = Convert.ToChar(blob(i))
  If myChar <> Nothing Then
    docType += myChar
  End If
Next
Dim packageIsPdf As Boolean = False
Dim ext As String = "dat"
'Dim filename As String = "supporting-document"
Dim contentType As String = "application/octet-stream"
If docType.Contains("Word.Document.8") _
    OrElse docType.Contains("Word.Document.9") _
    OrElse docType.Contains("Word.Document.10") _
    OrElse docType.Contains("Word.Document.11") Then
  ext = "doc"
  contentType = "application/ms-word"
ElseIf docType.Contains("Word.Document.12") _
    OrElse docType.Contains("Word.Document.14") _
    OrElse docType.Contains("Word.Document.15") Then
  ext = "docx"
  contentType = "application/ms-word"
ElseIf docType.Contains("Excel.Sheet.8") _
    OrElse docType.Contains("Excel.Sheet.9") _
    OrElse docType.Contains("Excel.Sheet.10") _
    OrElse docType.Contains("Excel.Sheet.11") Then
  ext = "xls"
  contentType = "application/ms-excel"
ElseIf docType.Contains("Excel.Sheet.12") _
    OrElse docType.Contains("Excel.Sheet.14") _
    OrElse docType.Contains("Excel.Sheet.15") Then
  ext = "xlsx"
  contentType = "application/ms-excel"
  'ElseIf docType.Contains("pdf") OrElse docType.Contains("Acrobat Document") Then
  '  ext = "pdf"
  '  contentType = "application/pdf"
  'ElseIf docType.Contains("Packager Shell ObjectPack") Then
  '  ext = "zip"
  '  'contentType = "application/zip"
  '  'Dim fs As FileStream = New FileStream(path & "\" & fileName & ".zip", FileMode.OpenOrCreate, FileAccess.Write)
  '  'fs.Write(blob, 0, blob.Length)
  '  'fs.Close()
  '  'If launchFile Then
  '  '  Process.Start(path & "\" & fileName & ".zip")
  '  'End If
  '  'fs.Dispose()
  '  'Exit Sub
End If
'magic eight bytes 01 05 00 00 02 00 00 00
ms.Seek(seekTotal, SeekOrigin.Begin)
ReDim buffer(8)
ms.Read(buffer, 0, 8)
seekTotal += 8

'Second offset to move to 
ReDim buffer(4)
ms.Read(buffer, 0, 4)
seekTotal += 4
Dim offset2 As Long = BitConverter.ToInt32(buffer, 0)
seekTotal += offset2
ms.Seek(seekTotal, SeekOrigin.Begin)

'eight empty bytes
ReDim buffer(8)
ms.Read(buffer, 0, 8)
seekTotal += 8

'next n bytes are the length of the file
ReDim buffer(4)
ms.Read(buffer, 0, 4)
seekTotal += 4
Dim fileByteLength As Long = BitConverter.ToInt32(buffer, 0)

'next N bytes are the file
Dim data(fileByteLength) As Byte

'store file bytes in data buffer
ms.Read(data, 0, Convert.ToInt32(fileByteLength))
Dim fPath As String = ""
fPath = path & "\" & fileName & "." & ext
Dim fs As FileStream = New FileStream(fPath, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(data, 0, Convert.ToInt32(fileByteLength))
fs.Close()
fs.Dispose()
ms.Dispose()
End Sub

0 个答案:

没有答案