using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using ICSharpCode.SharpZipLib.Zip; namespace GzRMIS.Main.Business { public class ZipHelper { /// <summary> /// 存放待压缩的文件的绝对路径 /// </summary> private List<string> AbsolutePaths { set; get; } public string errorMsg { set; get; } public ZipHelper() { errorMsg = ""; AbsolutePaths = new List<string>(); } /// <summary> /// 添加压缩文件或文件夹 /// </summary> /// <param name="_fileAbsolutePath">文件或文件夹的绝对路径</param> public void AddFile(string _fileAbsolutePath) { AbsolutePaths.Add(_fileAbsolutePath); } /// <summary> /// 压缩文件或者文件夹 /// </summary> /// <param name="_depositPath">压缩后文件的存放路径 如C:\\windows\abc.zip</param> /// <param name="_Level">压缩级别0~9,数字越大压缩率越高,默认为5</param> /// <returns></returns> public bool CompressionZip(string _depositPath,int _Level=5) { bool result = true; FileStream fs = null; try { ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath)); ComStream.SetLevel(_Level); //压缩等级 foreach (string path in AbsolutePaths) { //如果是目录 if (Directory.Exists(path)) { ZipFloder(path, ComStream, path); } else if (File.Exists(path))//如果是文件 { fs = File.OpenRead(path); byte[] bts = new byte[fs.Length]; fs.Read(bts, 0, bts.Length); ZipEntry ze = new ZipEntry(new FileInfo(path).Name); ComStream.PutNextEntry(ze); //为压缩文件流提供一个容器 ComStream.Write(bts, 0, bts.Length); //写入字节 } } ComStream.Finish(); // 结束压缩 ComStream.Close(); } catch (Exception ex) { if (fs != null) { fs.Close(); } errorMsg = ex.Message; result = false; } return result; } //压缩文件夹 private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath) { foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos()) { if (Directory.Exists(item.FullName)) { ZipFloder(_OfloderPath, zos, item.FullName); } else if (File.Exists(item.FullName))//如果是文件 { DirectoryInfo ODir = new DirectoryInfo(_OfloderPath); string fullName2 = new FileInfo(item.FullName).FullName; string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录 FileStream fs = File.OpenRead(fullName2); byte[] bts = new byte[fs.Length]; fs.Read(bts, 0, bts.Length); ZipEntry ze = new ZipEntry(path); zos.PutNextEntry(ze); //为压缩文件流提供一个容器 zos.Write(bts, 0, bts.Length); //写入字节 } } } /// <summary> /// 解压 /// </summary> /// <param name="_depositPath">压缩文件路径</param> /// <param name="_floderPath">解压的路径</param> /// <returns></returns> public bool DeCompressionZip(string _depositPath, string _floderPath) { bool result = true; FileStream fs = null; try { ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath)); ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件 Directory.CreateDirectory(_floderPath);//创建解压文件夹 while (ze != null)//如果解压完ze则是null { if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名 { string[] strs = ze.Name.Split('\\');//如果文件名中包含’\\‘则表明有文件夹 if (strs.Length > 1) { //两层循环用于一层一层创建文件夹 for (int i = 0; i < strs.Length - 1; i++) { string floderPath = _floderPath; for (int j = 0; j < i; j++) { floderPath = floderPath + "\\" + strs[j]; } floderPath = floderPath + "\\" + strs[i]; Directory.CreateDirectory(floderPath); } } fs = new FileStream(_floderPath + "\\" + ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//创建文件 //循环读取文件到文件流中 while (true) { byte[] bts = new byte[1024]; int i = InpStream.Read(bts, 0, bts.Length); if (i > 0) { fs.Write(bts, 0, i); } else { fs.Flush(); fs.Close(); break; } } } ze = InpStream.GetNextEntry(); } } catch (Exception ex) { if (fs != null) { fs.Close(); } errorMsg = ex.Message; result = false; } return result; } } }

C#和.NET提供了强大的功能和高效的开发环境。1)C#是一种现代、面向对象的编程语言,结合了C 的强大和Java的简洁性。2).NET框架是一个用于构建和运行应用程序的平台,支持多种编程语言。3)C#中的类和对象是面向对象编程的核心,类定义数据和行为,对象是类的实例。4).NET的垃圾回收机制自动管理内存,简化开发者的工作。5)C#和.NET提供了强大的文件操作功能,支持同步和异步编程。6)常见错误可以通过调试器、日志记录和异常处理来解决。7)性能优化和最佳实践包括使用StringBuild

.NETFramework是一个跨语言、跨平台的开发平台,提供一致的编程模型和强大的运行时环境。1)它由CLR和FCL组成,CLR管理内存和线程,FCL提供预构建功能。2)使用示例包括读取文件和LINQ查询。3)常见错误涉及未处理异常和内存泄漏,需使用调试工具解决。4)性能优化可通过异步编程和缓存实现,保持代码可读性和可维护性是关键。

C#.NET保持持久吸引力的原因包括其出色的性能、丰富的生态系统、强大的社区支持和跨平台开发能力。1)性能表现优异,适用于企业级应用和游戏开发;2).NET框架提供了广泛的类库和工具,支持多种开发领域;3)拥有活跃的开发者社区和丰富的学习资源;4).NETCore实现了跨平台开发,扩展了应用场景。

C#.NET中的设计模式包括Singleton模式和依赖注入。1.Singleton模式确保类只有一个实例,适用于需要全局访问点的场景,但需注意线程安全和滥用问题。2.依赖注入通过注入依赖提高代码灵活性和可测试性,常用于构造函数注入,但需避免过度使用导致复杂度增加。

C#.NET在现代世界中广泛应用于游戏开发、金融服务、物联网和云计算等领域。1)在游戏开发中,通过Unity引擎使用C#进行编程。2)金融服务领域,C#.NET用于开发高性能的交易系统和数据分析工具。3)物联网和云计算方面,C#.NET通过Azure服务提供支持,开发设备控制逻辑和数据处理。

C#.NET开发者社区提供了丰富的资源和支持,包括:1.微软的官方文档,2.社区论坛如StackOverflow和Reddit,3.GitHub上的开源项目,这些资源帮助开发者从基础学习到高级应用,提升编程技能。

C#.NET的优势包括:1)语言特性,如异步编程简化了开发;2)性能与可靠性,通过JIT编译和垃圾回收机制提升效率;3)跨平台支持,.NETCore扩展了应用场景;4)实际应用广泛,从Web到桌面和游戏开发都有出色表现。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 英文版
推荐:为Win版本,支持代码提示!