首页 > 编程语言 >C# split big file into small files as, and merge the small files into big one

C# split big file into small files as, and merge the small files into big one

时间:2024-09-01 13:52:12浏览次数:11  
标签:files chunkSize buffer big into int var bytesRead string

namespace ConsoleApp59
{
    internal class Program
    {
        static string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        static void Main(string[] args)
        {
            string filePath = @"D:\Software\WallStreetWolf\WallStreetWolf.mkv";
            int chunkSize = 100000000;
            SplitLargeFile(filePath, chunkSize);
            MergeSmallFilesIntoOne(chunkSize);
            Console.WriteLine("Finished!");            
        }

        static void MergeSmallFilesIntoOne(int chunkSize = 100000000)
        {
            var mkvFiles = Directory.GetFiles(path,"*.mkv");
            using (var newFs = File.Create("WallStreetWolf.mkv"))
            {
                foreach(var kvFile in mkvFiles)
                {
                    using (var largeFileStream = File.OpenRead(kvFile))
                    {
                        var buffer = new byte[chunkSize];
                        int bytesRead;
                        while ((bytesRead = largeFileStream.Read(buffer, 0, chunkSize)) > 0)
                        {
                            newFs.Write(buffer, 0, bytesRead);
                            Console.WriteLine(kvFile);
                        }
                    }
                }
            }
            Console.WriteLine("Finised in MergeSmallFilesIntoOne!\n\n\n");
        }

        static void SplitLargeFile(string filePath, int chunkSize)
        {
            int idx = 0;
            
            string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
            string extension = System.IO.Path.GetExtension(filePath);
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[chunkSize];
                int bytesRead;
                while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    byte[] destArray = new byte[bytesRead];
                    Array.Copy(buffer, destArray, bytesRead);
                    string splitFile = $"{fileName}_{idx++}{extension}";
                    using (FileStream destStream = new FileStream(splitFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        using (MemoryStream ms = new MemoryStream(destArray))
                        {
                            ms.WriteTo(destStream);
                            Console.WriteLine(splitFile);
                        }
                    }
                }
            }
            Console.WriteLine("Finised in SplitLargeFile!\n\n\n");
        }
    }
}

 

 

 

 

 

标签:files,chunkSize,buffer,big,into,int,var,bytesRead,string
From: https://www.cnblogs.com/Fred1987/p/18391235

相关文章