C#实现文件压缩与解压功能的示例代码
作者:叫我少年
这篇文章主要为大家详细介绍了如何利用C#实现文件压缩与解压功能,文中的示例代码讲解详细,具有一定的学习价值,需要的可以参考一下
压缩
private void skinButton1_Click(object sender, EventArgs e) { FilesUploadFor.ZipDirectory(foldertozip.Text,zipedfilename.Text); filesUploadFor.DisplaylistboxMsg("压缩完成"); }
ZipDirectory
压缩用的是库函数
/// <summary> /// 压缩文件夹 /// </summary> /// <param name="folderToZip">需要压缩的文件夹</param> /// <param name="zipedFileName">压缩后的Zip完整文件名</param> public static void ZipDirectory(string folderToZip, string zipedFileName) { ZipDirectory(folderToZip, zipedFileName, string.Empty, true, string.Empty, string.Empty, true); } /// <summary> /// 压缩文件夹 /// </summary> /// <param name="folderToZip">需要压缩的文件夹</param> /// <param name="zipedFileName">压缩后的Zip完整文件名(如D:\test.zip)</param> /// <param name="isRecurse">如果文件夹下有子文件夹,是否递归压缩</param> /// <param name="password">解压时需要提供的密码</param> /// <param name="fileRegexFilter">文件过滤正则表达式</param> /// <param name="directoryRegexFilter">文件夹过滤正则表达式</param> /// <param name="isCreateEmptyDirectories">是否压缩文件中的空文件夹</param> public static void ZipDirectory(string folderToZip, string zipedFileName, string password, bool isRecurse, string fileRegexFilter, string directoryRegexFilter, bool isCreateEmptyDirectories) { FastZip fastZip = new FastZip(); fastZip.CreateEmptyDirectories = isCreateEmptyDirectories; fastZip.Password = password; fastZip.CreateZip(zipedFileName, folderToZip, isRecurse, fileRegexFilter, directoryRegexFilter); }
解压缩
private void skinButton2_Click(object sender, EventArgs e) { filesUploadFor.UnZip(zipedfilename.Text,""); filesUploadFor.DisplaylistboxMsg("解压缩完成"); }
UnZip
解压用的是库函数
/// <summary> /// 功能:解压zip格式的文件。 /// </summary> /// <param name="zipFilePath">压缩文件路径</param> /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param> /// <returns>解压是否成功</returns> public void UnZip(string zipFilePath, string unZipDir) { if (zipFilePath == string.Empty) { throw new Exception("压缩文件不能为空!"); } if (!File.Exists(zipFilePath)) { throw new FileNotFoundException("压缩文件不存在!"); } //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 if (unZipDir == string.Empty) unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath)); if (!unZipDir.EndsWith("/")) unZipDir += "/"; if (!Directory.Exists(unZipDir)) Directory.CreateDirectory(unZipDir); using (var s = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (!string.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(unZipDir + directoryName); } if (directoryName != null && !directoryName.EndsWith("/")) { } if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name)) { int size; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } } }
到此这篇关于C#实现文件压缩与解压功能的示例代码的文章就介绍到这了,更多相关C#文件压缩与解压缩内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!