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

文件处理,无外乎加解密,加解压,分割合并。本着“快舟"精神,花了两天时间,写了个小程序,基本能满足个人使用。主类 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. 调优Java virtual machine常见问题汇总整理

    数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身, ...

  2. [javaSE] 反射-获取类的成员属性和构造方法

    成员属性和构造方法皆为对象,通过Class对象的方法可以得到 package com.tsh.reflect; import java.lang.reflect.Constructor; import ...

  3. javascript中this关键字详解

    不管学习什么知识,习惯于把自己所学习的知识列成一个list,会有助于我们理清思路,是一个很好的学习方法.强烈推荐. 以下篇幅有点长,希望读者耐心阅读. 以下内容会分为如下部分: 1.涵义 1.1:th ...

  4. 一级缓存、二级缓存、延迟加载、hibernate session 域 pojo三种状态

    1.一级缓存(session缓存 ).二级缓存      意义:提高hibernate查询效率.    缺点:可能会因并发,产生数据不一致.      本质:基于session 的缓存,利用hiber ...

  5. iOS 3DES加密解密(一行代码搞定)

    3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...

  6. EXCEL经纬度转化

    =LEFT(A1,FIND("°",A1)-1)+MID(A1,FIND("°",A1)+1,FIND(CHAR(39),A1)-1-FIND("°& ...

  7. Maven下SiteMesh的使用

    先白扯两句,为什么用SiteMesh,当然是减少重复代码,让程序员更加转自己的那一小块. 优点呢:结构化,重用 缺点:呵呵呵,性能,尤其是GC 至于3.0是不是有很大提升,请大神来分析一下. 性能你还 ...

  8. NSFileManager和NSFileHandler的作用

    NSFileManager类可以管理文件系统中的文件和目录,也可以定位.判断是否存在.创建.拷贝.删除文件和目录,还可以获得文件和目录的信息: 对于读写文件,NSFileManager只可以读写字符串 ...

  9. 【代码笔记】iOS-对iphone手机进行判断的一些函数

    代码: #import "RootViewController.h" //为判断手机的型号 -(NSString*)deviceString添加头文件 #import " ...

  10. 【代码笔记】iOS-GTMBase64

    一,工程文件. 二,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading th ...