Filestream / BinaryFormatter / ByteArray aufsplitten

  • VB.NET

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Filestream / BinaryFormatter / ByteArray aufsplitten

    Hallo!

    Ich hab hier ein paar Funktionen:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Shared Sub FileSerialize(ByVal File As String, ByVal Obj As Object, Optional ByVal Compress As Boolean = True, Optional ByVal EncryptKey As String = Nothing)
    2. Using s As New FileStream(File, FileMode.Create, FileAccess.ReadWrite)
    3. StreamSerialize(s, Obj, Compress, EncryptKey)
    4. s.Close()
    5. End Using
    6. End Sub
    7. Private Shared Sub StreamSerialize(ByRef s As Stream, ByVal Obj As Object, Optional ByVal Compress As Boolean = True, Optional ByVal EncryptKey As String = Nothing)
    8. Dim b As New Formatters.Binary.BinaryFormatter
    9. If Compress = True Then
    10. 'we want to compress this object to a byte array
    11. Obj = New CompressedObject(Compression.CompressByte(ByteArrSerialize(Obj)))
    12. End If
    13. If EncryptKey <> "" Then
    14. Obj = New EncryptedObject(Encryption.EncryptByte(ByteArrSerialize(Obj), EncryptKey))
    15. End If
    16. b.Serialize(s, Obj)
    17. End Sub
    18. Public Shared Function CompressByte(ByVal byteSource() As Byte) As Byte()
    19. ' Create a GZipStream object and memory stream object to store compressed stream
    20. Dim objMemStream As New MemoryStream()
    21. Dim objGZipStream As New GZipStream(objMemStream, CompressionMode.Compress, True)
    22. objGZipStream.Write(byteSource, 0, byteSource.Length)
    23. objGZipStream.Dispose()
    24. objMemStream.Position = 0
    25. ' Write compressed memory stream into byte array
    26. Dim buffer(CInt(objMemStream.Length)) As Byte
    27. objMemStream.Read(buffer, 0, buffer.Length)
    28. objMemStream.Dispose()
    29. Return buffer
    30. End Function
    31. Private Shared Function ByteArrSerialize(ByVal Obj As Object) As Byte()
    32. Using MS As New MemoryStream
    33. Dim BF As New Formatters.Binary.BinaryFormatter
    34. BF.Serialize(MS, Obj)
    35. ByteArrSerialize = MS.ToArray
    36. MS.Close()
    37. End Using
    38. End Function
    39. <Serializable()>
    40. Private Class CompressedObject
    41. Public CompressedObject As Byte()
    42. Public Sub New(ByVal CompressedObject As Byte())
    43. Me.CompressedObject = CompressedObject
    44. End Sub
    45. End Class
    46. <Serializable()>
    47. Private Class EncryptedObject
    48. Public EncryptedObject As Byte()
    49. Public Sub New(ByVal EncryptedObject As Byte())
    50. Me.EncryptedObject = EncryptedObject
    51. End Sub
    52. End Class



    Diese serialisieren für mich ein DataTable welches dann in eine Datei gespeichert werden soll..mit Compression!

    Leider ist das DataTable so groß das ich eine System.OutOfMemoryException bekomme. (ca. 4GB)

    Wie kann ich den Stream oder das ByteArray aufsplitten das er mir mehrere Dateien erstellt?

    Ach ja, umstellen auf 64Bit hat nichts gebracht..naja, er hängt jetzt nicht bei 3,7GB sondern erst bei 4,5GB.
    System RAM: 16GB
    Auslagerungsdatei: 2GB