将文件的前100个字节移到文件的末尾然后再保存,然后再次保存

时间:2014-03-04 21:44:59

标签: c# bytearray filestream

我需要将文件的前100个字节移动到文件的末尾,然后保存该文件(windows窗体应用程序)。然后我需要反向过程(将结尾处的100个字节移回到开头然后再次保存)。

其中一些文件非常大(超过2GB),因此我无法使用file.readallbytes,因为我的内存不足。

我尝试过使用文件流并使用filestream.position,但我无法绕过移动字节然后保存文件。

任何指导都将不胜感激。

2 个答案:

答案 0 :(得分:4)

您不应将所有数据读入内存。通过使用FileStream,您可以读取多个数据块,例如每个1 KB,并将其存储在新文件中。从位置100开始,跳过第一个字节。重新排列完整文件后,在末尾添加跳过的字节。最后将新文件移动到旧文件的位置。通过提高maxBufferSize,您可以加快复制过程,但需要使用更多内存。

要还原更改,请从最后100个字节开始,然后从开始直到inputStream.Length - 100继续。

string inputFile = "C:\\input.txt";
string tempFile = "C:\\input.txt";
int dataLength = 100;
int maxBufferSize = 1024;

using (var inputStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) {
    int length = (int)inputStream.Length;
    int currentInputPosition = dataLength;

    inputStream.Position = currentInputPosition;

    using (var outputStream = new FileStream(tempFile, FileMode.Create, FileAccess.ReadWrite)) {
        var bufferSize = Math.Min(maxBufferSize, length - currentInputPosition);

        var buffer = new byte[bufferSize];
        while (inputStream.Read(buffer, 0, bufferSize) > 0) {
            currentInputPosition += bufferSize;

            outputStream.Write(buffer, 0, buffer.Length);
        }

        buffer = new byte[dataLength];
        inputStream.Position = 0;
        inputStream.Read(buffer, 0, buffer.Length);
        outputStream.Write(buffer, 0, buffer.Length);
    }
}

File.Delete(inputFile);
File.Move(tempFile, inputFile);

答案 1 :(得分:3)

关键是使用字节缓冲区,因此您一次只处理文件的一小部分。听起来你有正确的方法,但是沿途还有一些问题。

这是一个展示我如何接近它的例子:

public enum SwapType
{
    FrontToBack,
    BackToFront
}

public static class EndSwap
{
    public static void DoSwap(string path, SwapType swapType)
    {
        if (path == null)
        {
            throw new ArgumentNullException("path", 
                "You must supply a path to the file.");
        }
        if (!File.Exists(path))
        {
            throw new FileNotFoundException("File not found.");
        }

        string tempPath = Path.GetTempFileName();
        byte[] buffer = new byte[4096];
        byte[] swapBytes = new byte[100];

        using (FileStream inputFs = new FileStream(path, FileMode.Open, 
            FileAccess.Read))
        using (FileStream outputFs = new FileStream(tempPath, 
            FileMode.Open, FileAccess.Write))
        {
            int bytesRead = -1;

            if (swapType == SwapType.FrontToBack)
            {
                // We want to keep hold of the first 100 bytes of the file
                // and output them after copying the rest of file
                inputFs.Read(swapBytes, 0, 100);
            }
            else
            {
                // Read the last 100 bytes of the file
                inputFs.Seek(-100, SeekOrigin.End);
                inputFs.Read(swapBytes, 0, 100);
                // Output them straight to the output file
                outputFs.Write(swapBytes, 0, 100);
                // Reposition to the beginning of the input file
                inputFs.Seek(0, SeekOrigin.Begin);
            }

            // The number of bytes left to copy is 100 less than the file 
            // length
            long bytesRemaining = inputFs.Length - 100;

            // Copy the rest of the bytes
            while (bytesRemaining > 0)
            {
                bytesRead = inputFs.Read(buffer, 0, buffer.Length);
                // NB: the number of bytes read could be more than the 
                // number remaining
                outputFs.Write(buffer, 0,
                    (int)Math.Min(bytesRead, bytesRemaining));
                bytesRemaining -= bytesRead;
            }

            // Don't forget to append the start bytes if required
            if (swapType == SwapType.FrontToBack)
            {
                outputFs.Write(swapBytes, 0, 100);
            }
        }

        // Now swap the files themselves
        File.Delete(path);
        File.Move(tempPath, path);
        // NB: could do File.Replace() if backup is needed
    }
}

使用示例:

// Copy first 100 bytes to end
EndSwap.DoSwap(@"C:\Users\Dave\Downloads\MyTest.pdf", SwapType.FrontToBack);
// Copy those same 100 bytes back to the beginning again
EndSwap.DoSwap(@"C:\Users\Dave\Downloads\MyTest.pdf", SwapType.BackToFront);

当然,最终结果与原始文件相同。