Solved

Split File into Multiple Byte Arrays

Posted on 2013-01-27
2
1,986 Views
Last Modified: 2013-01-27
I was able to read a file into one byte array and write it back in another file without any issue.  However I'm trying to figure out how to read one file and split it into multiple arrays.  Then write back all the arrays into one file.  I'm not sure exactly what is going on but it seems I keep getting one extra byte in my arrays than I should have.

Working Simple Array
Imports System.IO

        Dim myFileStream As FileStream
        Dim intByte As Integer
        Dim lngLoop As Long = 0
        Dim bteRead() As Byte

        Try
            myFileStream = File.OpenRead("C:\Users\Guest\Desktop\Adobe.pdf")
            ReDim bteRead(myFileStream.Length)

            Do While Not intByte = -1
                intByte = myFileStream.ReadByte()
                If intByte <> -1 Then bteRead(lngLoop) = CByte(intByte)
                lngLoop += 1
            Loop

            myFileStream.Close()

            Dim oFileStream As System.IO.FileStream
            oFileStream = New System.IO.FileStream("C:\Users\Guest\Desktop\Adobe Test.pdf", System.IO.FileMode.Create)
            oFileStream.Write(bteRead, 0, bteRead.Length)
            oFileStream.Close()
        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try

Open in new window


Code for attempting to split the file into byte arrays.
Imports System.IO

        Dim myFileStream As FileStream
        Dim intByte As Integer
        Dim lngLoop As Long = 0
        Dim BinBlock1 As Byte()
        Dim BinBlock2 As Byte()

        BinBlock1 = Nothing
        BinBlock2 = Nothing

        Try
            myFileStream = File.OpenRead("C:\Users\Guest\Desktop\Adobe.pdf")
            ReDim BinBlock1(50000) 'Total Size = 73,478
            ReDim BinBlock2(23478)

            Do While Not intByte = -1
                intByte = myFileStream.ReadByte()
                If intByte <> -1 Then
                    If (lngLoop >= 0 And lngLoop <= 50000) Then
                        BinBlock1(lngLoop) = CByte(intByte)
                    End If

                    If (lngLoop >= 50001 And lngLoop < 99000) Then
                        If (lngLoop = 50001) Then lngLoop = 0
                        BinBlock2(lngLoop) = CByte(intByte)
                    End If
                End If
                lngLoop += 1
            Loop

            myFileStream.Close()

            Dim oFileStream As System.IO.FileStream
            oFileStream = New System.IO.FileStream("C:\Users\Guest\Desktop\Adobe Test.pdf", System.IO.FileMode.Create)
            oFileStream.Write(BinBlock1, 0, BinBlock1.Length)
            oFileStream.Write(BinBlock2, 0, BinBlock2.Length)
            oFileStream.Close()
        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try

Open in new window

0
Comment
Question by:thaburner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38825441
Try something like this:
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim chunkSize As Integer = 4096
        Dim chunks As New List(Of Byte())

        ' Read in the input file into byte arrays and store them in the "chunks" List above:
        Dim inputFilename As String = "C:\Users\Guest\Desktop\Adobe.pdf"
        Dim fsInput As New FileStream(inputFilename, IO.FileMode.Open)
        While fsInput.Position < fsInput.Length
            Dim bytes(chunkSize) As Byte
            ' the last "leftover" bit of the file may be smaller than our byte array...
            If fsInput.Length - fsInput.Position < bytes.Length Then
                ReDim bytes(fsInput.Length - fsInput.Position - 1)
            End If

            fsInput.Read(bytes, 0, bytes.Length)
            chunks.Add(bytes)
        End While
        fsInput.Close()

        ' Write the output file from the byte arrays in "chunks":
        Dim outputFilename As String = "C:\Users\Guest\Desktop\Adobe Test.pdf"
        Dim fsOutput As New FileStream(outputFilename, FileMode.Create)
        For Each chunk As Byte() In chunks
            fsOutput.Write(chunk, 0, chunk.Length)
        Next
        fsOutput.Close()

        MessageBox.Show("Done!")
    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:thaburner
ID: 38825498
This will work perfectly!!! Thanks Idle
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question