
首先要引用一下类库:using Ionic.Zip;这个类库可以到网上下载。
下面对类库使用的封装方法:
得到指定的输入流的ZIP压缩流对象
/// <summary> /// 得到指定的输入流的ZIP压缩流对象【原有流对象不会改变】 /// </summary> /// <param name="sourceStream"></param> /// <returns></returns> 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解压流对象
/// <summary> /// 得到指定的字节数组的ZIP解压流对象 /// 当前方法仅适用于只有一个压缩文件的压缩包,即方法内只取压缩包中的第一个压缩文件 /// </summary> /// <param name="sourceStream"></param> /// <returns></returns> 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文件
/// <summary> /// 压缩ZIP文件 /// 支持多文件和多目录,或是多文件和多目录一起压缩 /// </summary> /// <param name="List">待压缩的文件或目录集合</param> /// <param name="strZipname">压缩后的文件名</param> /// <param name="IsDirstruct">是否按目录结构压缩</param> /// <returns>成功:true/失败:false</returns> public static bool CompressMulti(List<string> List,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文件
/// <summary> /// 解压ZIP文件 /// </summary> /// <param name="strZipPath">待解压的ZIP文件</param> /// <param name="strUnZipPath">解压的目录</param> /// <param name="overWrite">是否覆盖</param> /// <returns>成功:true/失败:false</returns> 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#打包文件解压缩的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的c#打包文件解压缩的实例全部内容,希望文章能够帮你解决c#打包文件解压缩的实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)