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

文件处理,无外乎加解密,加解压,分割合并。本着“快舟"精神,花了两天时间,写了个小程序,基本能满足个人使用。主类 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, , des.Key.Length);
fs.Close(); fs = File.Create(OutDir + now + "IV");
fs.Write(des.IV, , 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, , buf.Length);
fs.Close(); MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(buf, , 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, , buf.Length);
fs.Close(); MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(buf, , buf.Length);
cs.FlushFinalBlock();
cs.Close(); string tempPath = OutPath(filePath);
int index = tempPath.LastIndexOf('.');
tempPath = tempPath.Substring(, 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, , bufKey.Length);
des.Key = bufKey;
fsKey.Close(); FileStream fsIV = File.OpenRead("IV");
byte[] bufIV = new byte[fsIV.Length];
fsIV.Read(bufIV, , 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, , buf.Length);
fs.Close(); string gzipPath = OutPath(filePath) + ".gzip";
FileStream fsGzip = File.Create(gzipPath);
GZipStream gs = new GZipStream(fsGzip, CompressionMode.Compress);
gs.Write(buf, , buf.Length);
gs.Flush();
gs.Close();
fsGzip.Close();
}
public void Decompress(string filePath)
{
string path = OutPath(filePath);
int index = path.LastIndexOf('.');
path = path.Substring(, 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 - ) / count;
byte[] buf = new byte[size]; for (int i = ; i < count; i++)
{
int len = fs.Read(buf, , buf.Length);
string path = OutPath(filePath) + "." + i.ToString() + ".part";
FileStream fsPart = File.Create(path);
fsPart.Write(buf, , len);
fsPart.Close();
} fs.Close();
}
public void Combine(IList<string> filePaths)
{
string first = filePaths.First();
int i = first.LastIndexOf('.');
first = first.Substring(, i); // delete .part
i = first.LastIndexOf('.');
first = first.Substring(, 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, , buf.Length);
fsSub.Close();
fs.Write(buf, , len);
}
fs.Close();
}
}
}

FileProcess

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

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

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

x01.FileProcessor: 文件处理的更多相关文章

  1. bash切割文件

    split -l 100 ./x01.txt -d -a 3 --additional-suffix=.txt 将 x01.txt文件,-l 100 按照每个100行,  -d 以数字累加, -a 3 ...

  2. x01.CodeBuilder: 生成代码框架

    根据 Assembly 生成代码框架. 这是学习 AvalonEdit 的一个副产品.学习时,照着源代码新建文件夹,新建文件,添加方法与属性,虽然只是个框架,也要花费大量时间.为什么不让它自动生成呢? ...

  3. x01.TextProc: 两三分钟完成的一个小工具

    在工作中,遇到这么个问题,需要将 Excel 表中类似 2134-1234-4456 的商品编号输入到单位的程序中,而程序只认 213412344456 这种没有 ‘-’ 的输入.数量比较多,一笔一笔 ...

  4. x01.os.20: compile linux-0.11 on the ubuntu

    为什么学习 linux 正如不能依靠美国的 GPS 为我们的导弹指示目标一样,很难想像用运行 windows 的电脑去同美国进行信息战.而朝鲜的网络崩溃,再次警示国人,信息战.网络战离我们并不遥远.l ...

  5. x01.os.19: linux 0.0

    linux 0.0 是一个丢失的版本,但赵炯老师又在 linux 0.11 的基础上,使它起死回生.www.oldlinux.org 有大量资源可供下载,值得一看. 1.要编译运行,首先需安装:sud ...

  6. java解析XML文件

    dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源 ...

  7. CDH5.4.5运行Phoenix导入CSV文件

    1.安装phoenix 在界面上设置Phoenix的parcel包: http://52.11.56.155:7180/cmf/settings?groupKey=config.scm.parcel. ...

  8. x01.os.16: 添加功能

    准备工作  1.确保是 win xp,如是 win 8,运行 nasm 需按提示同意安装组件.  2.确保 src 和 z_tools 在同一目录下,nasm 已包含在 z_tools 文件夹中.  ...

  9. x01.os.15: 看上去很美

    张碧晨在韩国学的不是技巧,而是基本功:气息!声音由气息托着,似真声而不是真声,似假声又不是假声,所以才能在动听的地方唱得更动听.编程也是一样,基本功很重要:内存!所谓的黑客高手,攻击的一大手段,便是利 ...

随机推荐

  1. CI框架源码阅读笔记4 引导文件CodeIgniter.php

    到了这里,终于进入CI框架的核心了.既然是“引导”文件,那么就是对用户的请求.参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位.例如,用户的请求url: http://you.host.c ...

  2. linux Centos 6.5 安装桌面环境GNOME

    在某种场合之下,我们使用的Linux还是要选择安装桌面环境的,所以在这里介绍一下如何给没有安装桌面环境的系统安装桌面环境.以Centos 6.5 为例演示一下如何安装桌面环境. 工具/原料 Linux ...

  3. CentOS添加路由表

    CentOS下静态路由修改命令方法一:添加路由route add -net 192.168.0.0/24 gw 192.168.0.1route add -host 192.168.1.1 dev 1 ...

  4. HTML5+Activex+Singr+ABP+MongoDB

    最近在.net DDD开发领域有个炒的很火的框架叫ASP.NET Boilerplate看上去很牛逼的样子,为什么我会觉得很牛逼呢?  第一:我看不懂.  第二:关注的人多,我选框架就像进饭馆,哪家人 ...

  5. 模拟Executor策略的实现

    Executor作为现在线程的一个管理工具,就像管理线程的管理器一样,不用像以前一样,通过start来开启线程 Executor将提交线程与执行线程分离开来,使得用户只需要提交线程,并不需要在乎怎么和 ...

  6. 粒子动画Particleground.js

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. Exif.js 读取图像的元数据

    Exif.js 提供了 JavaScript 读取图像的原始数据的功能扩展,例如:拍照方向.相机设备型号.拍摄时间.ISO 感光度.GPS 地理位置等数据. 注意事项: EXIF 数据主要来自拍摄的照 ...

  8. html4基础知识梳理

    基础的html知识,只放Xmind的截图. 第一部分: 第二部分: 某些标签的使用示例及注意事项,在印象笔记里.

  9. MSCRM 2011/2013/2015 修改显示记录数

    本文地址:http://www.cnblogs.com/Earson/p/4256213.html 1.针对全局的显示记录数最大值设置 在CRM2011产品中的后台MSCRM_Config数据库中表名 ...

  10. [javascript svg fill stroke stroke-width x y rect rx ry 属性讲解] svg fill stroke stroke-width rect 绘制具有圆角矩形属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...