C#_文件的切割与合并

一,文件切割

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

 /// <summary>
 /// 
 /// </summary>
public static class FileScripts
{
    /// <summary>
    /// 文件切割
    /// </summary>
    /// <param name="filePath">源文件,文件</param>
    /// <param name="Folder">目标路径,文件夹</param>
    /// <param name="partConunt">切割段数</param>
    public static void Split(string filePath,string Folder,int partConunt)
    {
        //如果目标路径不存在,则创建一个
        if (!Directory.Exists(Folder))
        {
            Directory.CreateDirectory(Folder);
        }
        //字节流,FileShare.Read多线程
        using (FileStream streamRead = new FileStream(filePath,FileMode.Open,FileAccess.Read,FileShare.Read))
        {
            //求每一段长度
            long partLenght = streamRead.Length / partConunt;
            //循环段数
            for (int i = 0; i < partConunt; i++)
            {
                //小文件命名:获取源文件名,不包含后缀名
                string fileName = Path.GetFileNameWithoutExtension(filePath);
                //目标文件夹,下小文件命名,PadLeft左填充(位数,'填充数')
                string foldername = Folder + "/" + fileName + i.ToString().PadLeft(3,'0');
                //判断是否是最后一段,如果是就把剩余加上
                if (partConunt - 1 == 0)
                    //加上剩余部分
                    partLenght = streamRead.Length % partLenght;
                //读取长度等于每段的长度
                byte[] part = new byte[partLenght];
                //读:存储的数组,开始,结束
                streamRead.Read(part,0,part.Length);
                //写:目标文件夹下的小物件,写入的数据数组
                File.WriteAllBytes(foldername,part);
            }
        }
    }
}

二.路径

C#_文件的切割与合并

C#_文件的切割与合并 

 

三.文件的合并

  /// <summary>
    /// 文件合并
    /// </summary>
    /// <param name="filePath">源文件夹</param>
    /// <param name="fileName">合并后文件名地址</param>
    public static void Merge(string filePath,string fileName)
    {
        //找到所有文件
        FileInfo[] allFiles = new DirectoryInfo(filePath).GetFiles();
        //计算总长度
        long lenght = FileLenght(allFiles);
        //创建大文件
        using (FileStream bigfile = new FileStream(fileName,FileMode.Create))
        {
            //循环读取文件,写入到同一个文件中
            bigfile.SetLength(lenght);
            for (int i = 0; i < allFiles.Length; i++)
            {
                byte[] fileBytes = File.ReadAllBytes(allFiles[i].FullName);
                bigfile.Write(fileBytes,0,fileBytes.Length);
            }
        }
    }

    //计算总长度的方法
    private static long FileLenght(FileInfo[] allfiles)
    {
        long lenght = 0;
        for (int i = 0; i < allfiles.Length; i++)
        {
            lenght += allfiles[i].Length;
        }
        return lenght;
    }

四,测试、

 public void OnGUI()
    {
        if (GUILayout.Button("加密"))
        {
            FileJiaMi.Encryption(path);
        }

        if (GUILayout.Button("文件拆分"))
        {
            string sourcePath = Application.dataPath + "/" + "zhandi.mp4";
            string destPath = Application.persistentDataPath + "/01";
            FileScripts.Split(sourcePath,destPath,5);
        }

        if (GUILayout.Button("文件合并"))
        {
            string sourcePath = Application.persistentDataPath + "/01";
            string destPath = Application.dataPath + "/" + "zhandi2.mp4";
            FileScripts.Merge(sourcePath,destPath);
            Debug.Log("合并成功");
        }

        GUILayout.Label("Asset文件夹"+Application.dataPath);
        GUILayout.Label("StreamingAsset文件夹" + Application.streamingAssetsPath);
        GUILayout.Label("临时路径" + Application.temporaryCachePath);
        GUILayout.Label("持久路径" + Application.persistentDataPath);
    }

上一篇:简单题 题解


下一篇:Unity资源命名管理工具(硬规范)