x01.FileProcessor: 文件处理_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > x01.FileProcessor: 文件处理

x01.FileProcessor: 文件处理

 2013/9/29 22:45:50  x01  博客园  我要评论(0)
  • 摘要:姚贝娜落选,意味着好声音失败。“我们在一起”的精彩亮相,正如同她的歌声,愈唱愈高,直入云霄。文件处理,无外乎加解密,加解压,分割合并。本着“快舟"精神,花了两天时间,写了个小程序,基本能满足个人使用。主类FileProcess如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.IO;usingSystem.IO.Compression
  • 标签:

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

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

class="code_img_closed" src="/Upload/Images/2013092922/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('e0b2c651-ecc9-4fbb-a752-e19074c80b96',event)" src="/Upload/Images/2013092922/2B1B950FA3DF188F.gif" alt="" />
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

所有操作,都保存在输出目录中。这主要是为了简化。

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

源代码可在 Download 中下载。

  • 相关文章
发表评论
用户名: 匿名