How can I split a binary file into chunks with certain size with batch script without external software?

前端 未结 3 611
情歌与酒
情歌与酒 2021-01-14 23:01

There are many reasons to want to split file into chunks - mainly for network transfer (e.g. e-mail attachments) but I\'m sure there are scenarios that could require such t

3条回答
  •  隐瞒了意图╮
    2021-01-14 23:26

    The example below will split a file, producing multiple output files all smaller than the maxChunkSize supplied. To reassemble, you can use copy /b.

    SplitFile.cs
    (compile with c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc /out:splitFile.exe SplitFile.cs)

    using System;
    using System.IO;
    
    namespace SplitFile
    {
        class Program
        {
            static void Main(string[] args)
            {
                long maxChunkSize;
                if (args.Length != 3 || !long.TryParse(args[2], out maxChunkSize) || maxChunkSize <= 81920)
                {
                    Console.WriteLine("Usage: splitfile.exe inputFile outputprefix maxchunksize");
                    Console.WriteLine(" inputfile:     File to split");
                    Console.WriteLine(" outputprefix:  Prefix to use for the output name");
                    Console.WriteLine("                Ex: out -> { out0001.bin, out0002.bin }");
                    Console.WriteLine(" maxchunksize:  Maximum number of bytes in each file");
                    Console.WriteLine("                Note: this is the maximum size, not an exact size");
                    Console.WriteLine("                Note: chunk size cannot be smaller than 81920 bytes");
                    return;
                }
    
                string inputFilePath = args[0];
                string outputFilePathFormat = string.Format("{0}{{0:0000}}.bin", args[1]);
    
                using (Stream fsInput = File.Open(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    byte[] buffer = new byte[81920 /* default from System.Stream */];
                    int cOutFileNo = 0;
                    Stream destination = getOutputFile(ref cOutFileNo, outputFilePathFormat);
                    try
                    {
                        int read;
                        while ((read = fsInput.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            if (destination.Length + read > maxChunkSize)
                            {
                                destination.Dispose();
                                destination = getOutputFile(ref cOutFileNo, outputFilePathFormat);
                            }
    
                            destination.Write(buffer, 0, read);
                        }
                    }
                    finally
                    {
                        destination.Dispose();
                    }
                }
            }
    
            private static Stream getOutputFile(ref int cOutFileNo, string outFileFormat)
            {
                string filename = string.Format(outFileFormat, cOutFileNo);
                cOutFileNo++;
    
                return File.Open(filename, FileMode.CreateNew, FileAccess.Write, FileShare.None);
            }
        }
    }
    

    Example Use:

    C:\drop>splitFile.exe ubuntu-rescue-remix-12-04.iso Ubuntu_Split_ 10485760
    C:\drop>dir
    01/29/2015  17:21       244,570,112 ubuntu-rescue-remix-12-04.iso
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0000.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0001.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0002.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0003.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0004.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0005.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0006.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0007.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0008.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0009.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0010.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0011.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0012.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0013.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0014.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0015.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0016.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0017.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0018.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0019.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0020.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0021.bin
    01/30/2015  15:27        10,485,760 Ubuntu_Split_0022.bin
    01/30/2015  15:27         3,397,632 Ubuntu_Split_0023.bin
    
    C:\drop>copy /b Ubuntu_Split_* Ubuntu_recombined.iso
    Ubuntu_Split_0000.bin
    Ubuntu_Split_0001.bin
    Ubuntu_Split_0002.bin
    Ubuntu_Split_0003.bin
    Ubuntu_Split_0004.bin
    Ubuntu_Split_0005.bin
    Ubuntu_Split_0006.bin
    Ubuntu_Split_0007.bin
    Ubuntu_Split_0008.bin
    Ubuntu_Split_0009.bin
    Ubuntu_Split_0010.bin
    Ubuntu_Split_0011.bin
    Ubuntu_Split_0012.bin
    Ubuntu_Split_0013.bin
    Ubuntu_Split_0014.bin
    Ubuntu_Split_0015.bin
    Ubuntu_Split_0016.bin
    Ubuntu_Split_0017.bin
    Ubuntu_Split_0018.bin
    Ubuntu_Split_0019.bin
    Ubuntu_Split_0020.bin
    Ubuntu_Split_0021.bin
    Ubuntu_Split_0022.bin
    Ubuntu_Split_0023.bin
               1 file(s) copied.
    
    C:\drop>dir Ubuntu*.iso
    01/29/2015  17:21       244,570,112 ubuntu-rescue-remix-12-04.iso
    01/30/2015  15:27       244,570,112 Ubuntu_recombined.iso
    
    C:\drop>fciv -sha1 ubuntu-rescue-remix-12-04.iso
    //
    // File Checksum Integrity Verifier version 2.05.
    //
    02403c37cbdb3e03e00f5176807a793ef63d877c ubuntu-rescue-remix-12-04.iso
    
    C:\drop>fciv -sha1 Ubuntu_recombined.iso
    //
    // File Checksum Integrity Verifier version 2.05.
    //
    02403c37cbdb3e03e00f5176807a793ef63d877c ubuntu-rescue-remix-12-04.iso
    
    C:\drop>
    

提交回复
热议问题