TQC+ C# 認證教學手冊

208、圖檔分割

開啟檔案讀寫串流

打開檔案、讀取。

new FileStream(SourceFile, FileMode.Open, FileAccess.Read);

打開或新建檔案、寫入。

new FileStream(OutputFile, FileMode.OpenOrCreate, FileAccess.Write);

執行時間計算

// using System.Diagnostics.Stopwatch;
Stopwatch sw = new Stopwatch();
sw.reset();
sw.start();

// do something

sw.stop();

long timeSpan = sw.ElapsedMilliseconds; // unit: ms

參考程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace CSD02
{
    class Program
    {

        static void Main(string[] args)
        {

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Reset();
            sw.Start();


            //判斷使用者輸入參數
            if (args.Length != 3)
                return;
            if (args != null)
            {
                switch (args[0])
                {
                    case "/S"://檔案分割
                        string Source = args[1];
                        string PageSize = args[2];
                        int result;
                        if (!Int32.TryParse(PageSize, out result))
                        {
                            Console.WriteLine("請輸入正確數值");
                            Console.Read();
                            return;
                        }

                        if (!File.Exists(Source))
                        {
                            Console.WriteLine("檔案不存在");
                            Console.Read();
                            return;
                        }
                        if (SplitFile(Source, result))
                        {
                            //System.Threading.Thread.Sleep(1000);
                            sw.Stop();
                            Console.WriteLine(string.Format ("分割檔成功總共花費:{0} 秒", (sw.ElapsedMilliseconds / 1000.0)));
                            Console.Read();
                        }
                        break;
                    case "/M"://檔案合併
                        string Input = args[1];
                        string Output = args[2];
                    if (!Directory.Exists(Input))
                        {
                            Console.WriteLine("目錄不存在");
                            Console.Read();
                            return;
                        }
                        if (MergerFiles(Input, Output))
                        {
                            sw.Stop();
                            Console.WriteLine(string.Format ("合併檔成功總共花費:{0} 秒", (sw.ElapsedMilliseconds / 1000.0)));
                            Console.Read();
                        }
                        break;
                    default:
                        Console.WriteLine("參數錯誤");
                        Console.Read();
                        break;
                }
            }

        }

        //檔案分割函式
        static bool SplitFile(string SourceFile, int OutputFiles)
        {
            bool result = true;
            try
            {
                //把檔案讀到BinaryReader在把讀到的btye分在輸出的檔案裡面
                FileInfo info = new FileInfo(SourceFile);
                FileStream SplitFileStream = new FileStream(SourceFile, FileMode.Open, FileAccess.Read);
                BinaryReader SplitFileReader = new BinaryReader(SplitFileStream);
                int PageSize = (int)Math.Ceiling((double)(info.Length / OutputFiles));
                byte[] TempBytes;
                for (int idx = 0; idx < OutputFiles; idx++)
                {
                    string TempFile = info.DirectoryName + "\\" + info.Name + "_" + String.Format(@"{0:D2}", idx) + ".XX";
                    FileStream TempStream = new FileStream(TempFile, FileMode.OpenOrCreate, FileAccess.Write);
                    BinaryWriter TempWriter = new BinaryWriter(TempStream);
                    TempBytes = SplitFileReader.ReadBytes(PageSize);
                    TempWriter.Write(TempBytes);

                    Console.WriteLine("分割:{0}", TempFile);
                    Console.WriteLine("大小:{0} KB", TempBytes.Length / 1024);
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                result = false;
                Console.WriteLine(ex.Message);
                Console.Read();
            }
            return result;
        }

        //檔案合併函式
        static bool MergerFiles(string SourcePath, string OutputFile)
        {
            bool result = true;
            try
            {
                //將分割檔全部讀到 BinaryReader,透過BinaryWriter將讀到的byte寫到指定的檔案,進行檔案合併
                IEnumerable<string> Files = Directory.EnumerateFiles(SourcePath, "*.XX");
                using (FileStream OutStream = new FileStream(OutputFile, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (BinaryWriter BinWriter = new BinaryWriter(OutStream))
                    {
                        foreach (string File in Files)
                        {
                            FileStream TempStream = new FileStream(File, FileMode.Open, FileAccess.Read);
                            BinaryReader TempReader = new BinaryReader(TempStream);
                            byte[] bytes = TempReader.ReadBytes((int)new FileInfo(File).Length);
                            BinWriter.Write(bytes);
                            TempReader.Close();

                            Console.WriteLine("合併:{0}", File);
                            Console.WriteLine("大小:{0} KB", bytes.Length / 1024);
                            Console.WriteLine();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                result = false;
                Console.WriteLine(ex.Message);
                Console.Read();
            }
            return result;
        }
    }
}