关注开源代码的实际应用
SharpZipLib 是一个免费的Zip操作类库,可以利用它对 ZIP 等多种格式进行压缩与解压。SharpZipLib使用C#编写的,在VB.NET、C#或其他的.NET语言中都可以使用它创建Zip文件、并进行读取 和更新等操作。下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib /Download.aspx。目前的版本为0.85。
1、创建zip文件,并添加文件:
- using (ZipFile zip = ZipFile.Create(@"E:\test.zip"))
- {
- zip.BeginUpdate();
- zip.Add(@"E:\文件1.txt");
- zip.Add(@"E:\文件2.txt");
- zip.CommitUpdate();
- }
2、将文件夹压缩为文件:
(new FastZip()).CreateZip(@"E:\test.zip", @"E:\test\", true, "");
最后一个参数是使用正则表达式表示的过滤文件规则。CreateZip方法有3个重载版本,其中有目录过滤参数、文件过滤参数及用于指定是否进行子目录递归的一个bool类型的参数。
3、将文件添加到已有zip文件中:
using (ZipFile zip = new ZipFile(@"E:\test.zip"))
{
zip.BeginUpdate();
zip.Add(@"E:\test.doc");
zip.CommitUpdate();
}
4、列出zip文件中文件
using (ZipFile zip = new ZipFile(@"E:\test.zip"))
{
string list = string.Empty;
foreach (ZipEntry entry in zip)
{
list += entry.Name + "\r\n";
}
MessageBox.Show(list);
}
5、删除zip文件中的一个文件
using (ZipFile zip = new ZipFile(@"E:\test.zip"))
{
zip.BeginUpdate();
zip.Delete(@"test.doc");
zip.Delete(@"test22.txt");
zip.CommitUpdate();
}
6、解压zip文件中文件到指定目录下
(new FastZip()).ExtractZip(@"E:\test.zip", @"E:\test\", "");
7、常用类:
ZipInputStream、GZipInputStream用于解压缩Deflate、GZip格式流,ZipOutputStream、GZipOutputStream用于压缩Deflate、GZip格式流。
StreamUtil类包含了几个Stream处理辅助方法:
①、Copy(Stream, Stream, Byte[])用于从一个Stream对象中复制数据到另一Stream对象。有多个重写
②、ReadFully(Stream, Byte [])用于从Stream对象中读取所有的byte数据。有多个重写
- public static bool Wildcard(string pattern, string input)
- {
- return Wildcard(pattern, 0, input, 0, false);
- }
- public static bool Wildcard(string pattern, string input, bool insensitive)
- {
- return Wildcard(pattern, 0, input, 0, insensitive);
- }
- private static bool Wildcard(string pattern, int p, string input, int i, bool insensitive)
- {
- for(; ; )
- {
- char ic = input[i];
- char pc = pattern[p];
- switch(pc)
- {
- case '?':
- break;
- case '*':
- p++;
- for(int j = i; j < input.Length; j++)
- {
- if(Wildcard(pattern, p, input, j, insensitive))
- {
- return true;
- }
- }
- return false;
- default:
- if(insensitive)
- {
- ic = char.ToLower(ic);
- pc = char.ToLower(pc);
- }
- if(ic != pc)
- {
- return false;
- }
- break;
- }
- i++;
- p++;
- if(p >= pattern.Length)
- {
- if(i >= input.Length)
- {
- return true;
- }
- return false;
- }
- else if(i >= input.Length)
- {
- return false;
- }
- }
- }
Cassini,是Asp.net上的一个项目。但是好像只有.Net 1.0 的。最新的叫 WebDev.WebServer2. 在.NET 安装包下面就有(不知道是否可以随意发布?). Codeplex 上有一个 Cassini 的包装器,支持.NET2.0, 可以让你在计算机内针对任意目录,右键支持直接将其转变为虚拟目录,进行Web服务.
以下为几个有用的链接,有心进行Asp.net程序桌面移植的朋友,可以看看.
http://www.codeplex.com/WebServiceHoster 自己在桌面程序中支持 Asp.net Host, 基于Cassini
http://www.codeplex.com/CassiniWrapper
UltiDev Cassini Web Server: 不同与微软的那个 Cassini, 但是支持 .NET 目前所有的版本(1.0-3.5)
http://ultidev.com/products/Cassini/index.htm
其他应用文章
http://zhq.ahau.edu.cn/blog/article.asp?id=288
http://zhq.ahau.edu.cn/blog/article/297.htm
http://tv9.cnblogs.com/articles/73176.html
Mono XSP
http://www.mono-project.com/ASP.NET
Small and Reliable C++ HTTP Server with Complete ASP.NET Support
可通过System.Environment.Version.ToString()取得当前CLR版本号,然后对照下表得到Revision版本:
| .NET Framework version | Revision | Version |
|---|---|---|
| 3.5 | Original release | 3.5.21022.8 |
| 3.5 | Service Pack 1 | 3.5.30729.1 |
| 3.0 | Original release | 3.0.4506.30 |
| 3.0 | Service Pack 1 | 3.0.4506.648 |
| 3.0 | Service Pack 2 | 3.0.4506.2152 |
| 2.0 | Original release | 2.0.50727.42 |
| 2.0 | Service Pack 1 | 2.0.50727.1433 |
| 2.0 | Service Pack 2 | 2.0.50727.3053 |
| 1.1 | Original release | 1.1.4322.573 |
| 1.1 | Service Pack 1 | 1.1.4322.2032 |
| 1.1 | Service Pack 1 (Windows Server 2003 32-bit version*) | 1.1.4322.2300 |
| 1.0 | Original release | 1.0.3705.0 |
| 1.0 | Service Pack 1 | 1.0.3705.209 |
| 1.0 | Service Pack 2 | 1.0.3705.288 |
| 1.0 | Service Pack 3 | 1.0.3705.6018 |
.NET DiscUtils
纯C#实现读写ISO和VHD文件的类库,以Stream进行文件读写,无需将整个文件加载到内存中。当前支持的文件系统格式:FAT、NTFS(只读)、VHD和VDI。范例:
①、新建一个ISO文件:
- CDBuilder builder = new CDBuilder();
- builder.UseJoliet = true;
- builder.VolumeIdentifier = "A_SAMPLE_DISK";
- builder.AddFile(@"Folder\Hello.txt", Encoding.ASCII.GetBytes("Hello World!"));
- builder.Build(@"C:\temp\sample.iso");
可以像上面那样以byte数组添加文件,也可以直接从Windows文件系统中加载,或者也可以是个Stream。生成的结果保存到Stream中,也可以直接保存到Windows的文件系统中。
②、从ISO文件中读取文件
- using (FileStream isoStream = File.Open(@"C:\temp\sample.iso"))
- {
- CDReader cd = new CDReader(isoStream, true);
- Stream fileStream = cd.OpenFile(@"Folder\Hello.txt", FileMode.Open);
- // Use fileStream...
- }
目录结构以cd.Root为起点。
③、新建一个虚拟硬盘:
- long diskSize = 30 * 1024 * 1024; //30MB
- using (Stream vhdStream = File.Create(@"C:\TEMP\mydisk.vhd"))
- {
- Disk disk = Disk.InitializeDynamic(vhdStream, diskSize);
- BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsFat);
- using (FatFileSystem fs = FatFileSystem.FormatPartition(disk, 0, null))
- {
- fs.CreateDirectory(@"TestDir\CHILD");
- // do other things with the file system...
- }
- }
文件系统以fs.Root作为起点。
④、新建一个虚拟软盘:
- using (FileStream fs = File.Create(@"myfloppy.vfd"))
- {
- using (FatFileSystem floppy = FatFileSystem.FormatFloppy(fs, FloppyDiskType.HighDensity, "MY FLOPPY "))
- {
- using (Stream s = floppy.OpenFile("foo.txt", FileMode.Create))
- {
- // Use stream...
- }
- }
- }
Image Master
用于读写iso文件的.Net应用程序,主要特性:
* Read the contents of image files before burning them to disc.
* Extract the contents of image files without burning them to disc.
* Burn an image file to disc.
* Burn files and/or folders directly to disc.
* Supports multi-session burning.
* Create image files for archiving.
* Convert Bin Image Files (.bin), Nero Image Files (.nrg), Alcohol Image Files (.mdf), CloneCd Image Files (.img), DiscJuggler Image Files (.cdi) to iso images.


在Windows XP SP2刻录光盘需要安装Imapi v2.0,在Windows Vista默认就已经安装了。
C#实现的一个类库,用于操作ISO9660映像(.iso/.bin/.mdf...) ,主要特性:
* Read ISO9660 discs images (supported formats are .iso/.bin/.mdf and CloneCd image).
* Extract from discs images are by file or full.
* Create an iso disc image file from CD/DVD source.
* Convert from several disc image format to iso disc image file (supported formats are .bin/.mdf/.nrg/.img/.cdi).
* Create iso disc image file from a source directory (not available yet).
* Burn iso file.
有两种方法可以实现:
①、通过SQLiteParameter
- static void TestSqliteBinary()
- {
- using (SQLiteConnection cnn = new SQLiteConnection("data source=temp.db"))
- {
- cnn.Open();
- using (SQLiteCommand cmd = cnn.CreateCommand())
- {
- //cmd.CommandText = "Create Table test(data Image)";
- //cmd.ExecuteNonQuery();
- cmd.CommandText = "insert into test values(@data)";
- SQLiteParameter para = new SQLiteParameter("@data",DbType.Binary);
- string file = @"info.zip";
- FileStream fs = new FileStream(file, FileMode.Open);
- StreamUtil su = new StreamUtil();
- byte[] buffer = su.StreamToBytes(fs);
- fs.Close();
- para.Value = buffer;
- cmd.Parameters.Add(para);
- cmd.ExecuteNonQuery();
- }
- }
- }
②、通过16进制字符编码:
- INSERT INTO Foo (blob) VALUES(X'BADF00D')
In the above example, the binary data is quoted as hex, but inserted into the database as actual binary characters. Data is retrieved as a byte[] array.
下载AStyle.Net.Interface.rar,然后在项目中引用AStyle.Net.dll:
- string input = File.ReadAllText("in.txt");
- string temp = "";
- temp = AStyle.Net.Interface.Format(input, "--mode=cs --style=ansi --indent=tab");
- File.WriteAllText("out.txt",temp.Trim());
Berkeley DB是历史悠久的嵌入式数据库系统,早期主要应用在UNIX/LINUX操作系统上,现在也有大量的Windows应用程序使用Berkeley DB存储数据。Berkeley DB的存储的是key/value键值对,可以理解为硬盘上的超级hash表,可以管理256TB数据,而且能支撑几千个并发访问。BerkeleyDB 相关资料可以从这里下载:http://www.oracle.com/technology/products/berkeley-db/index.html。
由于SQLite只支持单写多读,大多数情况下无法满足大访问量Web站点应用的并发性要求;而DB4O的Embed模式完全不支持并发;可以考虑使用Berkeley DB作为Asp.Net的Embed数据库。
目前Berkeley DB官方有C++版和Java版。要想在.NET中使用,可以使用Berkeley DB for .NET的Binding(Wrapper)。Berkeley DB for .NET 最 新版本为0.95,支持BDB 4.3和4.5。下载到libdb-dotnet_0_95.zip后,解压,在解压缩后的bin目录找到libdb_dotNET45.dll,这就是 BDB 4.5的.NET Binding,在.NET项目中引用此DLL就可以开始使用了。