在C#中,可以使用System.IO.Compression命名空间中的ZipFile类来压缩文件目录。以下是一个示例代码,演示了如何压缩一个文件夹:
using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string startPath = @"C:\example\folderToCompress"; string zipPath = @"C:\example\result.zip"; ZipFile.CreateFromDirectory(startPath, zipPath); } }在上面的示例中,我们首先指定要压缩的文件夹路径startPath,然后指定压缩后的Zip文件路径zipPath。然后使用ZipFile.CreateFromDirectory方法将文件夹压缩为一个Zip文件。
需要注意的是,ZipFile类是.NET Framework 4.5及以上版本中的新功能,因此在更早的版本中可能无法使用。如果需要在较早的版本中进行压缩操作,可以考虑使用第三方库,如SharpZipLib。
提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。
版权声明:除特别声明外,本站所有文章皆是本站原创,转载请以超链接形式注明出处!