两个关于使用ICSharpCode.SharpZipLib.dll对文件进行压缩和解压方法的类
2011年7月21日晚在We7CMS中看到的两个关于使用ICSharpCode.SharpZipLib.dll对文件进行压缩和解压的方法。
1 /// <summary>
2 /// zip压缩业务处理包括模板组等
3 /// </summary>
4 public static class ZipClass
5 {
6 //压缩
7
8 public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
9 {
10 //如果檔沒有找到,則報錯
11 if (!System.IO.File.Exists(FileToZip))
12 {
13 throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
14 }
15
16 System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
17 System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
18 ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
19 ZipEntry ZipEntry = new ZipEntry("ZippedFile");
20 ZipStream.PutNextEntry(ZipEntry);
21 ZipStream.SetLevel(CompressionLevel);
22 byte[] buffer = new byte[BlockSize];
23 System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
24 ZipStream.Write(buffer, 0, size);
25 try
26 {
27 while (size < StreamToZip.Length)
28 {
29 int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
30 ZipStream.Write(buffer, 0, sizeRead);
31 size += sizeRead;
32 }
33 }
34 catch (System.Exception ex)
35 {
36 throw ex;
37 }
38 ZipStream.Finish();
39 ZipStream.Close();
40 StreamToZip.Close();
41 }
42
43 public static string AddZip(string fileName, string zipName, ZipOutputStream s)
44 {
45 Crc32 crc = new Crc32();
46 try
47 {
48 FileStream fs = File.OpenRead(fileName);
49 byte[] buffer = new byte[fs.Length];
50 fs.Read(buffer, 0, buffer.Length);
51 fileName = Path.GetFileName(fileName);
52 long fileLength = fs.Length;
53 fs.Close();
54
55 ZipEntry entry = new ZipEntry(zipName);
56 entry.DateTime = DateTime.Now;
57 entry.Size = fileLength;
58
59 crc.Reset();
60 crc.Update(buffer);
61 entry.Crc = crc.Value;
62 s.PutNextEntry(entry);
63 s.Write(buffer, 0, buffer.Length);
64
65 return string.Empty;
66 }
67 catch (Exception addEx)
68 {
69 return addEx.ToString();
70 }
71 }
72
73 public static void AddFolder(string folderName, string zipName, ZipOutputStream s)
74 {
75 if (Directory.Exists(folderName))
76 {
77 foreach (string str in Directory.GetFileSystemEntries(folderName))
78 {
79 if (File.Exists(str))
80 AddZip(str, zipName + "\\" + str.Substring(str.LastIndexOf("\\") + 1), s);
81 else
82 AddFolder(str, zipName + "\\" + str.Substring(str.LastIndexOf("\\") + 1), s);
83 }
84 }
85 }
86
87 public static void AddTemplateFolder(string folderName, ZipOutputStream s)
88 {
89 if (Directory.Exists(folderName))
90 {
91 foreach (string str in Directory.GetFileSystemEntries(folderName))
92 {
93 if (File.Exists(str))
94 AddZip(str,str.Substring(str.LastIndexOf("\\") + 1), s);
95 else
96 AddFolder(str,str.Substring(str.LastIndexOf("\\") + 1), s);
97 }
98 }
99 }
100
101 public static void CreateTemplateZip(string[] args)
102 {
103 string dirName = null;
104 dirName = Path.GetDirectoryName(Path.GetFullPath(args[1]));
105 if (!Directory.Exists(dirName))
106 {
107 Directory.CreateDirectory(dirName);
108 }
109 ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
110
111 s.SetLevel(6); // 0 - store only to 9 - means best compression
112
113 AddTemplateFolder(args[0],s);
114
115 s.Finish();
116 s.Close();
117 }
118
119 public static void ZipFileMain(string[] args)
120 {
121 string dirName = null;
122 dirName = Path.GetDirectoryName(Path.GetFullPath(args[1]));
123 if (!Directory.Exists(dirName))
124 {
125 Directory.CreateDirectory(dirName);
126 }
127 ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
128
129 s.SetLevel(6); // 0 - store only to 9 - means best compression
130
131 //AddFolder(args[0], args[0].Substring(args[0].Length - 1) == "\\" ? args[0].Substring(0, args[0].Length - 1).Substring(args[0].Substring(0, args[0].Length - 1).LastIndexOf("\\") + 1) : args[0].Substring(args[0].LastIndexOf("\\") + 1), s);
132 AddTemplateFolder(args[0], s);
133
134 s.Finish();
135 s.Close();
136 }
137
138 /// <summary>
139 /// 通用压缩目录方法
140 /// </summary>
141 /// <param name="args">分别放:源文件夹路径;目标文件路径;</param>
142 public static void ZipPathToFile(string[] args)
143 {
144 string dirName = null;
145 dirName = Path.GetDirectoryName(Path.GetFullPath(args[1]));
146 if (!Directory.Exists(dirName))
147 {
148 Directory.CreateDirectory(dirName);
149 }
150 ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
151
152 s.SetLevel(6); // 0 - store only to 9 - means best compression
153
154 AddFolder(args[0], args[1], s);
155
156 s.Finish();
157 s.Close();
158 }
159
160 }
1 /// <summary>
2 /// ZIP压缩基础操作类
3 /// </summary>
4 public static class ZipUtils
5 {
6 public static Stream GetFileFromZip(Stream input, string fileName)
7 {
8 MemoryStream ms = new MemoryStream();
9
10 fileName=fileName.Replace(@"\\", "/");
11 fileName = fileName.Replace(@"\","/");
12 using (ZipInputStream stream = new ZipInputStream(input))
13 {
14 ZipEntry ze;
15 while ((ze = stream.GetNextEntry()) != null)
16 {
17 if (ze.IsFile && ze.Name == fileName)
18 {
19 byte[] temp = new byte[0x1000];
20 int num=0;
21 while ((num=stream.Read(temp, 0, temp.Length))>0)
22 {
23 ms.Write(temp, 0, num);
24 }
25 }
26 }
27 }
28 ms.Position = 0;
29 return ms;
30 }
31
32 public static void CreateZip(string path, Stream os)
33 {
34 FastZip fz = new FastZip();
35 fz.CreateZip(os, path, true, null, null);
36 }
37
38 public static void ExtractZip(Stream input, string target)
39 {
40 using (ZipInputStream st = new ZipInputStream(input))
41 {
42 ZipEntry ze;
43 byte[] buffer = new byte[0x1000];
44 while ((ze = st.GetNextEntry()) != null)
45 {
46 string filename = ze.Name;
47 string dir = Path.Combine(target, filename);
48 string path = Path.GetDirectoryName(Path.GetFullPath(dir));
49 if (!Directory.Exists(path))
50 {
51 Directory.CreateDirectory(path);
52 }
53 if (ze.IsFile)
54 {
55 using (FileStream ts = File.Create(dir))
56 {
57 int num = 0;
58 while ((num = st.Read(buffer, 0, buffer.Length)) > 0)
59 {
60 if (num > 0)
61 {
62 ts.Write(buffer, 0, num);
63 }
64 }
65 ts.Flush();
66 ts.Close();
67 }
68 }
69 else if (ze.IsDirectory)
70 {
71 if (!Directory.Exists(dir))
72 {
73 Directory.CreateDirectory(dir);
74 }
75 }
76 }
77 }
78 }
79
80 public static string ExtractZipWithRoot(Stream input, string target)
81 {
82 string rootDir = "";
83 using (ZipInputStream st = new ZipInputStream(input))
84 {
85 ZipEntry ze;
86 byte[] buffer = new byte[0x1000];
87 while ((ze = st.GetNextEntry()) != null)
88 {
89 string filename = ze.Name;
90 string dir = Path.Combine(target, filename);
91 string path = Path.GetDirectoryName(Path.GetFullPath(dir));
92 if (!Directory.Exists(path))
93 {
94 Directory.CreateDirectory(path);
95 }
96 if (ze.IsFile)
97 {
98 using (FileStream ts = File.Create(dir))
99 {
100 int num = 0;
101 while ((num = st.Read(buffer, 0, buffer.Length)) > 0)
102 {
103 if (num > 0)
104 {
105 ts.Write(buffer, 0, num);
106 }
107 }
108 ts.Flush();
109 ts.Close();
110 }
111 }
112 else if (ze.IsDirectory)
113 {
114 if (String.IsNullOrEmpty(rootDir))
115 {
116 rootDir = dir;
117 }
118 if (!Directory.Exists(dir))
119 {
120 Directory.CreateDirectory(dir);
121 }
122 }
123 }
124 }
125 return rootDir;
126 }
127 }

浙公网安备 33010602011771号