Пример #1
0
        private void SplitFileBySize(int maxSize)
        {
            Encoding encoding = FileOperationHelper.GetEncodingType(txbFilePath.Text);


            int fileIndex = 1;
            int size      = (maxSize - 10) * 1024; //10K的Buffer
            var inFile    = new FileStream(txbFilePath.Text, FileMode.Open, FileAccess.Read);

            do
            {
                string newFile = GetWriteFilePath(fileIndex++);
                var    outFile = new FileStream(newFile, FileMode.OpenOrCreate,
                                                FileAccess.Write);
                if (fileIndex != 2) //不是第一个文件,那么就要写文件头
                {
                    FileOperationHelper.WriteFileHeader(outFile, encoding);
                }
                int data   = 0;
                var buffer = new byte[size];
                if ((data = inFile.Read(buffer, 0, size)) > 0)
                {
                    outFile.Write(buffer, 0, data);
                    bool hasContent = true;
                    do
                    {
                        int b = inFile.ReadByte();
                        if (b == 0xA || b == 0xD)
                        {
                            ReadToNextLine(inFile);

                            hasContent = false;
                        }
                        if (b != -1) //文件已经读完
                        {
                            outFile.WriteByte((byte)b);
                        }
                        else
                        {
                            hasContent = false;
                        }
                    } while (hasContent);
                }
                outFile.Close();
                rtbLogs.AppendText(newFile + "\r\n");
            } while (inFile.Position != inFile.Length);
            inFile.Close();
        }