Solved

Split File into Multiple Byte Arrays

Posted on 2013-01-27
2
1,841 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
2 Comments
 
LVL 85

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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