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

文件处理,无外乎加解密,加解压,分割合并。本着“快舟"精神,花了两天时间,写了个小程序,基本能满足个人使用。主类 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. (学习笔记)HTML的<link>标签

    在HTML中<link>标签用于定义文档与外部资源的关系. <link>标签只存在于head部分. <head> <link rel="styles ...

  2. 习题:codevs 1035 火车停留解题报告

    本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...

  3. C语言的数据、常量和变量

    一.数据 图片文字等都是数据,在计算机中以0和1存储. (一)分类 数据分为静态数据和动态数据. ①. 静态数据:一些永久性的的数据,一般存储在硬盘中,只要硬盘没坏数据都是存在的.一般以文件的形式存储 ...

  4. MySQL max_allowed_packet设置及问题

    MySQL根据配置文件会限制server接受的数据包大小. 有时候大的插入和更新会被max_allowed_packet 参数限制掉,导致失败. 查看目前配置 show VARIABLES like ...

  5. Linux下如何查看哪个进程占用内存多?

    1.top top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 可以直接使用top命令后,查看%MEM的内容.可以选择按进程查看或者 ...

  6. java中newInstance()和new()

    在Java开发特别是数据库开发中,经常会用到Class.forName( )这个方法.通过查询Java Documentation我们会发现使用Class.forName( )静态方法的目的是为了动态 ...

  7. Space.js – HTML 驱动的页面 3D 滚动效果

    为了让我们的信息能够有效地沟通,我们需要创建用户和我们的媒体之间的强有力的联系.今天我们就来探讨在网络上呈现故事的新方法,并为此创造了一个开源和免费使用的 JavaScript 库称为 space.j ...

  8. LINQ / LINQ to SQL / LINQ to XXX 它们到底有什么区别

    LINQ是新生事物,不过从不少文章和讨论上看来,这方面的概念也已经有点混沌不清了.因此我们经常可以看到这样的话: LINQ只能将数据表与实体属性一一对应…… LINQ开发指南:在LINQ中进行数据库字 ...

  9. jQuery原型方法first,last,eq,slice源码分析

    这4个方法中前3个方法很常用大家都见过,但是slice方法可能会以为是数组方法,其实slice也是jQuery的一个原型方法,只不过是底层方法是为其他方法服务的(更具体点是为eq方法服务的),首先还是 ...

  10. ArcGIS JS 学习笔记2 实现仿百度的拖拽画圆

    一.前言 吐槽一下,百度在国内除了百度地图是良心产品外,其他的真的不敢恭维.在上一篇笔记里,我已经实现了自定义的地图测量模块.在百度地图里面(其他地图)都有一个周边搜索的功能,拖拽画一个圆,然后以圆半 ...