X01.FileProcessor: file processing

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Yao Beina lost the election, means a good voice failed. “ &rdquo we together; the brilliant appearance, just like her singing, sang more high, straight into the sky.

Document processing, outside notting have encryption, compression and decompression, split and merge. The spirit of “ fast boat "spirit, spent two days, wrote a small program, can basically meet the personal use. The main types of FileProcess are as follows:

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

All operations, are preserved in the output directory. This is mainly in order to simplify.

Output directory, the configuration mode. This is mainly to flexible.

The source code can be downloaded at my blog x01.Lab.Download.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Harvey at November 13, 2013 - 12:29 PM