首先要引用一下类库:using Ionic.Zip;这个类库可以到网上下载。
下面对类库使用的封装方法:
得到指定的输入流的ZIP压缩流对象
////// 得到指定的输入流的ZIP压缩流对象【原有流对象不会改变】 /// /// ///public static Stream ZipCompress(Stream sourceStream, string entryName = "zip") { MemoryStream compressedStream = new MemoryStream(); if (sourceStream != null) { long sourceOldPosition = 0; try { sourceOldPosition = sourceStream.Position; sourceStream.Position = 0; using (ZipFile zip = new ZipFile()) { zip.AddEntry(entryName, sourceStream); zip.Save(compressedStream); compressedStream.Position = 0; } } catch { } finally { try { sourceStream.Position = sourceOldPosition; } catch { } } } return compressedStream; }
得到指定的字节数组的ZIP解压流对象
////// 得到指定的字节数组的ZIP解压流对象 /// 当前方法仅适用于只有一个压缩文件的压缩包,即方法内只取压缩包中的第一个压缩文件 /// /// ///public static Stream ZipDecompress(byte[] data) { Stream decompressedStream = new MemoryStream(); if (data != null) { try { MemoryStream dataStream = new MemoryStream(data); using (ZipFile zip = ZipFile.Read(dataStream)) { if (zip.Entries.Count > 0) { zip.Entries.First().Extract(decompressedStream); // Extract方法中会操作ms,后续使用时必须先将Stream位置归零,否则会导致后续读取不到任何数据 // 返回该Stream对象之前进行一次位置归零动作 decompressedStream.Position = 0; } } } catch { } } return decompressedStream; }
压缩ZIP文件
////// 压缩ZIP文件 /// 支持多文件和多目录,或是多文件和多目录一起压缩 /// /// 待压缩的文件或目录集合 /// 压缩后的文件名 /// 是否按目录结构压缩 ///成功:true/失败:false public static bool CompressMulti(Listlist, string strZipName, bool IsDirStruct) { try { using (ZipFile zip = new ZipFile(Encoding.Default))//设置编码,解决压缩文件时中文乱码 { foreach (string path in list) { string fileName = Path.GetFileName(path);//取目录名称 //如果是目录 if (Directory.Exists(path)) { if (IsDirStruct)//按目录结构压缩 { zip.AddDirectory(path, fileName); } else//目录下的文件都压缩到Zip的根目录 { zip.AddDirectory(path); } } if (File.Exists(path))//如果是文件 { zip.AddFile(path,"imges"); } } zip.Save(strZipName);//压缩 return true; } } catch (Exception) { return false; } }
解压ZIP文件
////// 解压ZIP文件 /// /// 待解压的ZIP文件 /// 解压的目录 /// 是否覆盖 ///成功:true/失败:false public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite) { try { ReadOptions options = new ReadOptions(); options.Encoding = Encoding.Default;//设置编码,解决解压文件时中文乱码 using (ZipFile zip = ZipFile.Read(strZipPath, options)) { foreach (ZipEntry entry in zip) { if (string.IsNullOrEmpty(strUnZipPath)) { strUnZipPath = strZipPath.Split('.').First(); } if (overWrite) { entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解压文件,如果已存在就覆盖 } else { entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解压文件,如果已存在不覆盖 } } return true; } } catch (Exception) { return false; } }
使用C#实现加减乘除算法经常被用作新手练习。本篇来分别体验通过委托、接口、匿名方法、泛型委托来实现。
使用委托实现
加减乘除拥有相同的参数个数、类型和返回类型,首先想到了使用委托实现。
//创建一个委托 public delegate decimal MathOperation(decimal left, decimal right); //创建方法参数和返回结果符合委托的定义 public static decimal Add(decimal left, decimal right) { return left + right; } public static decimal Subtract(decimal left, decimal right) { return left - right; } public static decimal Multiply(decimal left, decimal right) { return left * right; } public static decimal Divide(decimal left, decimal right) { return left / right; } //返回委托类型 private static MathOperation GetOperation(char oper) { switch(oper) { case '+': return Add; case '-': return Subtract; case '*': return Multiply; case '/': return Divide; } throw new NotSupportedException(""); } //封装一个方法用来把操作数和符号考虑进来,返回委托类型 private static decimal Eval(string expr) { var elements = expr.Split(new []{' '}, 3); var left = Decimal.Parse(elements[0]); var right = Decimal.Parse(elements[1]); var ope = elements[2][0]; return GetOperation(op)(left, right); } void Main() { Console.WriteLine(Eval("1 3 +")); }
使用接口实现
以上,委托用在了方法层面。如果在类层面,也可用接口封装加减乘除的共性。
public interface IMathOperation { decimal Compute(decimal left, decimal right); } public class AddOperation : IMathOperation { decimal Compute(decimal left, decimal right) { return left + right; } } public class SubtractOperation : IMathOperation { decimal Compute(decimal left,, decimal right) { return left - right; } } public class MultiplyOperation : IMathOperation { decimal Compute(decimal left, decimal right) { return left * right; } } public class DivideOperation : IMathOperation { decimal Compute(decimal left, decimal right) { return left/right; } } //获取接口类型 private static IMathOperation GetOperation(char oper) { switch(oper) { case '+': return new AddOperation(); case '-': return new SubtractOperation(); case '*': return new MultiplyOperation(); case '/': return new DivideOperation(); } throw new NotSupportedException(""); } ...
使用匿名方法
委托还可以结合匿名方法一起使用。
public delegate decimal MathOperation(decimal left, decimal right); private static MathOperation GetOperation(char oper) { switch(oper) { case '+': return delgate(decimal left, decimal right) {return left + right;}; case '-': return delgate(decimal left, decimal right) {return left - right;}; case '*': return delgate(decimal left, decimal right) {return left * right;}; case '/': return delgate(decimal left, decimal right) {return left / right;}; } throw new NotSupportedException(""); }
泛型委托
而用泛型委托实现更简洁。
private static FuncGetOperation(char oper) { switch(oper) { case '+': return (left, right) => left + right; case '-': return (left, right) => left - right; case '*': return (left, right) => left * right; case '/': return (left, right) => left / right; } throw new NotSupportedExcepton(""); }