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

文件处理,无外乎加解密,加解压,分割合并。本着“快舟"精神,花了两天时间,写了个小程序,基本能满足个人使用。主类 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. [python拾遗]文件操作

    文件操作 1.open()函数 open()函数主要用于文件处理,一般分为下面3个过程: 1.打开文件 2.操作文件 3.关闭文件 常见的格式示例: f = open('note.txt','r') ...

  2. 每天一命令 git stash

    git stash  命令是用于保存当前进度的命令.该命令会保存当前工作区的改动.保存的改动是已经跟踪的文件的改动,对于未跟踪的改动stash是不会保存的. git stash 命令常用于分支切换的 ...

  3. 一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架

    问题:3行代码 PDF.NET是一个开源的数据开发框架,它的特点是简单.轻量.快速,易上手,而且是一个注释完善的国产开发框架,受到不少朋友的欢迎,也在我们公司的项目中多次使用.但是,PDF.NET比起 ...

  4. 解决Wireshark没有网卡问题

    wireshark在mac上使用没有网卡问题直接在终端输入命令:sudo chmod 644 /dev/bpf*

  5. Play!中使用HTTP异步编程

    本章译者:@Sam Liu (译者未留下自己的主页,请Sam Liu见此文,加入群168013302联系‘大黄蜂@翻译play’) 这一章主要讲解如何运用异步模式实现典型的长连接(long-polli ...

  6. jQuery Wheel Menu:实现漂亮的 Path 风格旋转菜单

    相信很多用过 Path 的都对它的独特的旋转导航菜单有深刻的印象,这个功能也被很多的 Web 开发者模仿.今天分享的这款插件可以方便的在你的网站中加入和 Path 一样的旋转菜单,可以自定义效果. 您 ...

  7. Dom Animator – 提供 Dom 注释动画的 JS 库

    DOM 动画是一个极好的 JavaScript 库,用来在页面的 DOM 注释中显示小的 ASCII 动画.这对于那些检查你的代码的人是一个小彩蛋,仅此而已.它是一个独立的库,不依赖 jQuery 或 ...

  8. 微信中a链接无法进行跳转

    [问题]微信页面开发时,各个主页之间的跳转,完全是通过a链接进行的,但是来回跳转几次,再次从其他主页面跳回首页的时候,微信头部出现了跳转加载进度条,但是就是不跳转,也没有任何反应 [范围]只出现在微信 ...

  9. 在64位Windows7上安装64位Oracle11g

    我一直在用Oracle10g数据库,最近想看看11g怎么样,就试着装了一下,在安装过程中遇到的麻烦还不少,幸好有搜索引擎,根据前辈的指点,磕磕绊绊地也将Oracle装上了,作一下记录,以后也许能用得着 ...

  10. iOS常用第三方库

    1.AFNetworking 轻量级的通讯类库,使用非常简单. 下载地址:https://github.com/AFNetworking/AFNetworking   2.FMDB fmdb是一个数据 ...