VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之【随笔系列】C#使用第三方SharpZipLib进行(2)

); 13 14 ///判断路径最后一个字符是否为当前系统的DirectorySeparatorChar 15 if (directory[directory.Length - 1] != Path.DirectorySeparatorChar) 16 { 17 directory += Path.DirectorySeparatorChar; 18 } 19 try 20 { 21 ///使用using语句,资源使用完毕,自动释放(类需继承IDispose接口) 22 ///打开压缩文件进行读取 23 using (FileStream fs = File.OpenRead(compressedFile)) 24 { 25 ///创建压缩文件的输入流 26 using (ZipInputStream inStream = new ZipInputStream(fs)) 27 { 28 ///加密的密码 29 inStream.Password = encryptPassword; 30 ZipEntry entry; 31 while ((entry = inStream.GetNextEntry()) != null) 32 { 33 ///文件的父级目录名称 34 string directoryName = null; 35 ///文件的名称,例如git\\git.exe 36 string entryName = entry.Name; 37 38 if (string.IsNullOrWhiteSpace(entryName) == false) 39 { 40 directoryName = Path.GetDirectoryName(entryName) + Path.DirectorySeparatorChar; 41 } 42 ///获取文件名称,例如git.exe 43 string name = Path.GetFileName(entryName); 44 ///文件的父级目录的绝对路径 45 string newDirectory = directory + directoryName; 46 if (Directory.Exists(newDirectory) == false) 47 { 48 Directory.CreateDirectory(newDirectory); 49 } 50 51 if (string.IsNullOrWhiteSpace(name) == false) 52 { 53 ///文件的绝对路径 54 string fileName = directory + directoryName + name; 55 ///如果覆盖解压或者本地不存在当前文件则进行解压缩 56 if (overWrite || File.Exists(fileName) == false) 57 { 58 using (FileStream fsWrite = File.Create(fileName)) 59 { 60 ///缓存区对象 61 byte[] buffer = new byte[blockSize]; 62 ///读取的字节数 63 int sizeRead = 0; 64 ///读取完成,解压完成 65 do 66 { 67 ///从流中读取字节,将此数据写入缓存区 68 sizeRead = inStream.Read(buffer, 0, buffer.Length); 69 ///将字节写入文件流 70 fsWrite.Write(buffer, 0, buffer.Length); 71 } while (sizeRead > 0); 72 } 73 } 74 } 75 } 76 } 77 } 78 } 79 catch (Exception ex) 80 { 81 LogHelper.Write(ex.ToString()); 82 } 83 }
复制代码

 

四:在Program类Main方法中调用方法

 1、新建几个文件目录用来测试,目录结构如下图

2、编写代码,进行压缩与解压测试,如下

复制代码
 1 using System;
 2 using System.IO;
 3 
 4 namespace ConsoleCompressApp
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             var projectDirectory = Path.GetFullPath("../..");
11             var directorySeparatorChar = Path.DirectorySeparatorChar;
12             Console.WriteLine("您要压缩Files\\Packages\\git.exe文件么?");
13             if (Console.ReadLine().Trim().ToLower()=="yes")
14             {
15                 SharpZipLibHelper.CompressFile(
16                  fileName: projectDirectory + directorySeparatorChar + "Files" + directorySeparatorChar +
17                  "Packages" + directorySeparatorChar + "git.exe",
18                  compressedFilePath: projectDirectory + directorySeparatorChar + "ZipFiles",
19                  aliasFileName: "",
20                  compressionLevel: 8,
21                  blockSize: 2048,
22                  isEncrypt: true,
23                  encryptPassword: "123");
24             }
25             Console.WriteLine("您要压缩Files整个目录么?");
26             if (Console.ReadLine().Trim().ToLower() == "yes")
27             {
28                 SharpZipLibHelper.CompressDirectory(
29                    directory: projectDirectory + directorySeparatorChar + "Files",
30                    compressedDirectory: projectDirectory + directorySeparatorChar + "ZipDirectory",
31                    aliasFileName: "Files",
32                    isEncrypt: true,
33                    encryptPassword: "456");
34             }
35             Console.WriteLine("您要将ZipDirectory中的Files.zip解压缩到UnZipFiles目录中么?");
36             if (Console.ReadLine().Trim().ToLower() == "yes")
37             {
38                 SharpZipLibHelper.UnCompressFile(compressedFile: projectDirectory + directorySeparatorChar +
39                 "ZipDirectory" + directorySeparatorChar + "Files.zip",
40                 directory: projectDirectory + directorySeparatorChar + "UnZipFiles",
41                 encryptPassword: "456");
42             }
43             Console.WriteLine("恭喜您,操作完成了!");
44             Console.ReadLine();
45         }
46     }
47 }
复制代码

3、点击启动,进行测试,结果如下:

文件目录如下

 


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号