• x01.FileProcessor: 文件处理


    姚贝娜落选,意味着好声音失败。“我们在一起”的精彩亮相,正如同她的歌声,愈唱愈高,直入云霄。

    文件处理,无外乎加解密,加解压,分割合并。本着“快舟"精神,花了两天时间,写了个小程序,基本能满足个人使用。主类 FileProcess 如下:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.IO.Compression;
    using System.Linq;
    using System.Security.Cryptography;
    
    namespace x01.FileProcessor
    {
        class FileProcess
        {
            public string OutDir
            {
                get
                {
                    string dir = ConfigurationManager.AppSettings["Temp"];
                    if (!dir.EndsWith(@""))
                    {
                        dir += @"";
                    }
                    return dir;
                }
            }
    
            public void GenKey()
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.GenerateKey();
                des.GenerateIV();
    
                string now = DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + "-";
    
    
                FileStream fs = File.Create(OutDir + now + "Key");
                fs.Write(des.Key, 0, des.Key.Length);
                fs.Close();
    
                fs = File.Create(OutDir + now + "IV");
                fs.Write(des.IV, 0, des.IV.Length);
                fs.Close();
            }
    
            public void Encrypt(string filePath)
            {
                DESCryptoServiceProvider des = CreateDes();
    
                FileStream fs = File.OpenRead(filePath);
                byte[] buf = new byte[fs.Length];
                fs.Read(buf, 0, buf.Length);
                fs.Close();
    
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(buf, 0, buf.Length);
                cs.FlushFinalBlock();
                cs.Close();
    
                string cryPath = OutPath(filePath) + ".crypt";
                FileStream cryFile = File.Create(cryPath);
                foreach (var item in ms.ToArray())
                {
                    cryFile.WriteByte(item);
                }
                cryFile.Close();
    
                ms.Close();
            }
            public void Decrypt(string filePath)
            {
                DESCryptoServiceProvider des = CreateDes();
    
                FileStream fs = File.OpenRead(filePath);
                byte[] buf = new byte[fs.Length];
                fs.Read(buf, 0, buf.Length);
                fs.Close();
    
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(buf, 0, buf.Length);
                cs.FlushFinalBlock();
                cs.Close();
    
                string tempPath = OutPath(filePath);
                int index = tempPath.LastIndexOf('.');
                tempPath = tempPath.Substring(0, index);
    
                FileStream decryFile = File.Create(tempPath);
                foreach (var item in ms.ToArray())
                {
                    decryFile.WriteByte(item);
                }
                decryFile.Close();
    
                ms.Close();
            }
            private DESCryptoServiceProvider CreateDes()
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
                FileStream fsKey = File.OpenRead("Key");
                byte[] bufKey = new byte[fsKey.Length];
                fsKey.Read(bufKey, 0, bufKey.Length);
                des.Key = bufKey;
                fsKey.Close();
    
                FileStream fsIV = File.OpenRead("IV");
                byte[] bufIV = new byte[fsIV.Length];
                fsIV.Read(bufIV, 0, bufIV.Length);
                des.IV = bufIV;
                fsIV.Close();
    
                return des;
            }
            private string OutPath(string filePath)
            {
                int index = filePath.LastIndexOf('\');
                string fileName = filePath.Substring(index);
                return OutDir + fileName;
            }
    
            public void Compress(string filePath)
            {
                FileStream fs = File.OpenRead(filePath);
                byte[] buf = new byte[fs.Length];
                fs.Read(buf, 0, buf.Length);
                fs.Close();
    
                string gzipPath = OutPath(filePath) + ".gzip";
                FileStream fsGzip = File.Create(gzipPath);
                GZipStream gs = new GZipStream(fsGzip, CompressionMode.Compress);
                gs.Write(buf, 0, buf.Length);
                gs.Flush();
                gs.Close();
                fsGzip.Close();
            }
            public void Decompress(string filePath)
            {
                string path = OutPath(filePath);
                int index = path.LastIndexOf('.');
                path = path.Substring(0, index);
                FileStream fs = File.Create(path);
    
                FileStream fsGzip = File.OpenRead(filePath);
                GZipStream gs = new GZipStream(fsGzip, CompressionMode.Decompress);
                gs.CopyTo(fs);
                gs.Close();
                fsGzip.Close();
    
                fs.Close();
            }
    
            public void Split(string filePath, int count)
            {
                FileStream fs = File.OpenRead(filePath);
                long size = (fs.Length + count - 1) / count;
                byte[] buf = new byte[size];
    
                for (int i = 0; i < count; i++)
                {
                    int len = fs.Read(buf, 0, buf.Length);
                    string path = OutPath(filePath) + "." + i.ToString() + ".part";
                    FileStream fsPart = File.Create(path);
                    fsPart.Write(buf, 0, len);
                    fsPart.Close();
                }
    
                fs.Close();
            }
            public void Combine(IList<string> filePaths)
            {
                string first = filePaths.First();
                int i = first.LastIndexOf('.');
                first = first.Substring(0, i); // delete .part
                i = first.LastIndexOf('.');
                first = first.Substring(0, i); // delete .number
                i = first.LastIndexOf('.');
                string ext = first.Substring(i); // ext name
                string path = OutDir + "CombinedFile." + ext.Replace(".", "");
    
                FileStream fs = File.Create(path);
                foreach (var item in filePaths)
                {
                    FileStream fsSub = File.OpenRead(item);
                    byte[] buf = new byte[fsSub.Length];
                    int len = fsSub.Read(buf, 0, buf.Length);
                    fsSub.Close();
                    fs.Write(buf, 0, len);
                }
                fs.Close();
            }
        }
    }
    FileProcess

    所有操作,都保存在输出目录中,加解密的键值两个文件也放在输出目录中方可使用,这主要是为了简化。

    输出目录,采用配置的方式。这主要是为了灵活。

    源代码可在我的博客 x01.Lab.Download 中下载。

  • 相关阅读:
    [分治FFT]「CTSC2018」青蕈领主
    [边分治+线段树合并]「CTSC2018」暴力写挂
    [模板]MTT
    [模板]NTT
    [矩阵求逆+二分图匹配]BZOJ 3168 [Heoi2013]钙铁锌硒维生素
    [BZOJ1925][SDOI2010]地精部落(DP)
    [BZOJ1047][HAOI2007]理想的正方形(RMQ+DP)
    [POJ3630]Phone List (Tire)
    [POJ1193][NOI1999]内存分配(链表+模拟)
    [POJ2823]Sliding Window 滑动窗口(单调队列)
  • 原文地址:https://www.cnblogs.com/china_x01/p/3346372.html
Copyright © 2020-2023  润新知