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, , 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: 文件处理的更多相关文章
- bash切割文件
split -l 100 ./x01.txt -d -a 3 --additional-suffix=.txt 将 x01.txt文件,-l 100 按照每个100行, -d 以数字累加, -a 3 ...
- x01.CodeBuilder: 生成代码框架
根据 Assembly 生成代码框架. 这是学习 AvalonEdit 的一个副产品.学习时,照着源代码新建文件夹,新建文件,添加方法与属性,虽然只是个框架,也要花费大量时间.为什么不让它自动生成呢? ...
- x01.TextProc: 两三分钟完成的一个小工具
在工作中,遇到这么个问题,需要将 Excel 表中类似 2134-1234-4456 的商品编号输入到单位的程序中,而程序只认 213412344456 这种没有 ‘-’ 的输入.数量比较多,一笔一笔 ...
- x01.os.20: compile linux-0.11 on the ubuntu
为什么学习 linux 正如不能依靠美国的 GPS 为我们的导弹指示目标一样,很难想像用运行 windows 的电脑去同美国进行信息战.而朝鲜的网络崩溃,再次警示国人,信息战.网络战离我们并不遥远.l ...
- x01.os.19: linux 0.0
linux 0.0 是一个丢失的版本,但赵炯老师又在 linux 0.11 的基础上,使它起死回生.www.oldlinux.org 有大量资源可供下载,值得一看. 1.要编译运行,首先需安装:sud ...
- java解析XML文件
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源 ...
- CDH5.4.5运行Phoenix导入CSV文件
1.安装phoenix 在界面上设置Phoenix的parcel包: http://52.11.56.155:7180/cmf/settings?groupKey=config.scm.parcel. ...
- x01.os.16: 添加功能
准备工作 1.确保是 win xp,如是 win 8,运行 nasm 需按提示同意安装组件. 2.确保 src 和 z_tools 在同一目录下,nasm 已包含在 z_tools 文件夹中. ...
- x01.os.15: 看上去很美
张碧晨在韩国学的不是技巧,而是基本功:气息!声音由气息托着,似真声而不是真声,似假声又不是假声,所以才能在动听的地方唱得更动听.编程也是一样,基本功很重要:内存!所谓的黑客高手,攻击的一大手段,便是利 ...
随机推荐
- Java面试总结系列之Collections.sort()
面试中被问到,集合类中的排序方法是怎么实现的?没有回答上来,故而总结如下:你知道么? 前提:在eclipse中对于自己的代码可以通过按住Ctrl的同时单击名称跳入相应源码中.但eclipse默认没有添 ...
- No.020:Valid Parentheses
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- sso demo 取消https (cas)
基本配置 参考之前得随笔 http://www.cnblogs.com/rocky-fang/p/5354947.html 1. 修改tomcat-cas 配置 1.1 在 D:\test\sso\ ...
- http异步请求的一种调用示例
在异步编程中,经常会调用已经写好的异步方法.这时会有一个需求:根据异步方法的返回值,做一些别的操作. 1.0 重新开启一个异步方法,在这个新的异步方法内部,调用需要请求的异步方法.示例: static ...
- wso2esb之代理服务 Proxy Services
代理服务 顾名思义,代理服务充当了WSO2 ESB服务的代理,通常是一个已经存在的服务端点,代理服务可以使用不同的传输方式. 客户可以直接发送请求代理服务的ESB,客户看到服务代理. 运行示例 配置W ...
- mac下彻底卸载mysql方法
sudo rm /usr/local/mysqlsudo rm -rf /usr/local/mysql*sudo rm -rf /Library/StartupItems/MySQLCOMsudo ...
- CSS3动画处理浏览器内核时候前缀(兼容性)
Gecko内核 css前缀为"-moz-" 火狐浏览器 WebKit内核 css前缀为"-webkit-" Comodo Drangon(科摩多龙), ...
- 【javascript激增的思考01】模块化编程
前言 之前我做过一个web app(原来可以这么叫啦),在一个页面上有很多小窗口,每个小窗口都是独立的应用,比如: ① 我们一个小窗口数据来源是腾讯微博,需要形成腾讯微博app小窗口 ② 我们一个小窗 ...
- 如何设置'REUSE_ALV_GRID_DISPLAY'的单个单元格的颜色
REPORT ydemo_rick_a . TYPE-POOLS: slis. , carrid LIKE sflight-carrid, connid LIKE sflight-connid, fl ...
- arcgis批量处理mxd定义服务中的路径
>>> from arcpy import env... env.workspace=r"c:\165mxd"... out = r"c:\166mx ...