Question

I have a bunch of files with unwanted 128 byte headers. So I need to read/write the first 128 bytes to file A and the rest of the bytes to file B. Can someone help please? Thanks. PS. The file sizes vary from 100MB to 400GB

        private void SplitUnwantedHeader(string myFile)
    {
        int skipBytes = 128;
        using (FileStream fs = File.Open(myFile, FileMode.Open))
        {
            int bufferSize;
            checked
            {
                bufferSize = (int)(fs.Length - skipBytes);
            }
            byte[] buffer = new byte[bufferSize];

            fs.Position = skipBytes;
            fs.Read(buffer, 0, bufferSize);

            fs.Position = skipBytes;
            fs.Write(buffer, 0, bufferSize);
        }
    }
Was it helpful?

Solution 3

I have the following which works well. Also, included is a .NET 4.0 shortcut which uses the CopyTo() functionality.

Solution for .NET 3.5 and below:

private void SplitUnwantedHeader(string sourceFile, string destinationFile)
    {
        byte[] fByte = new byte[65534];                                                             //Declare 64k byte for read/write buffer
        long headerToSplit = 128;                                                                   //Declare the point where to start reading
        int bytesRead = 0;                                                                          //Declare total bytes read
        try
        {
            using (var fr = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))             //Open source file for reading
            using (var fw = new FileStream(destinationFile, FileMode.Create, FileAccess.Write))     //Create and open destination file for writing
            {
                fr.Position = headerToSplit;                                                        //Set reading position of source file in bytes
                do
                {
                    bytesRead = fr.Read(fByte, 0, fByte.Length);                                    //Read 64k bytes from source file
                    fw.Write(fByte, 0, bytesRead);                                                  //Write 64k bytes to destination file
                } while (bytesRead != 0);                                                           //Loop until there is no more bytes to read
            }               
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);                                   //Catch exception (if any) and display to user
        }
    }

Solution for .NET 4.0 and above:

private void SplitUnwantedHeader(string sourceFile, string destinationFile)
    {
        long headerToSplit = 128;                                                                   //Declare the point where to start reading
        try
        {
            using (var fr = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))             //Open source file for reading
            using (var fw = new FileStream(destinationFile, FileMode.Create, FileAccess.Write))     //Create and open destination file for writing
            {
                fr.Position = headerToSplit;                                                        //Set reading position of source file in bytes
                fr.CopyTo(fw, 65534);        //<-- Alternative for .NET 4.0
            }               
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);                                   //Catch exception (if any) and display to user
        }
    }

OTHER TIPS

using (FileStream stream = new FileStream())
{
    stream.Write();
}

This stream provides an overload for the offset and count of the bytes that you are looking for.

private void SplitUnwantedHeader(string myFile)
{
    const int skipBytes = 128;
    using (FileStream fs = File.Open(myFile, FileMode.Open))
    {
        // Write the skipped bytes to file A
        using (FileStream skipBytesFS = File.Open("FileA.txt", FileMode.Create))
        {
            byte[] skipBytesBuffer = new byte[skipBytes];
            fs.Read(skipBytesBuffer, 0, skipBytes);
            skipBytesFS.Write(skipBytesBuffer, 0, skipBytes);
            skipBytesFS.Flush();
        }
        // Write the rest of the bytes to file B
        using (FileStream outputFS = File.Open("FileB.txt", FileMode.Create))
        {
            long length = fs.Length - skipBytes;
            for (long i = 0; i < length; i++)
                outputFS.WriteByte((byte)fs.ReadByte());
            outputFS.Flush();
        }
    }
}

Note that when writing everything but the skipped bytes to file B, it is possible to read the entire rest of the input file into a buffer and then write that buffer to file B. However, this would easily result in an OutOfMemoryException if your files really go up to sizes like 400GB. Thus, writing one byte at a time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top