LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#压缩帮助类(SharpZipLib)

admin
2023年12月29日 16:28 本文热度 529

一、SharpZipLib官网

 NuGet Gallery | SharpZipLib 1.3.2

API Documentation | SharpZipLib Help (icsharpcode.github.io)

二、压缩帮助类

1.  /***

2.  *    Title"基础工具" 项目

3.  *    Title"基础工具" 项目

4.  *     主题:压缩包帮助类

5.  *    Description

6.  *     功能:

7.  *         1、压缩单个文件

8.  *         2、压缩多个文件

9.  *         3、压缩多层目录

10.*         4、递归遍历目录

11.*         5、解压缩一个 zip 文件

12.*         6、获取压缩文件中指定类型的文件

13.*         7、获取压缩文件中的所有文件

14.*    Date2021

15.*    Version0.1版本

16.*    AuthorCoffee

17.*    Modify Recoder

18.*/

19.

20.using ICSharpCode.SharpZipLib.Zip;

21.using System;

22.using System.Collections.Generic;

23.using System.IO;

24.using System.Text;

25.

26.namespace Utils.Zip

27.{

28.    class ZipHelper2

29.    {

30.        /// <summary>

31.        /// 压缩单个文件

32.        /// </summary>

33.        /// <param name="fileToZip">要压缩的文件</param>

34.        /// <param name="zipedFile">压缩后的文件</param>

35.        /// <param name="compressionLevel">压缩等级</param>

36.        /// <param name="blockSize">每次写入大小</param>

37.        public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)

38.        {

39.            //如果文件没有找到,则报错

40.            if (!System.IO.File.Exists(fileToZip))

41.            {

42.                throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");

43.            }

44.

45.            using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))

46.            {

47.                using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))

48.                {

49.                    using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))

50.                    {

51.                        string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);

52.                        ZipEntry ZipEntry = new ZipEntry(fileName);

53.                        ZipStream.PutNextEntry(ZipEntry);

54.                        ZipStream.SetLevel(compressionLevel);

55.                        byte[] buffer = new byte[blockSize];

56.                        int sizeRead = 0;

57.

58.                        try

59.                        {

60.                            do

61.                            {

62.                                sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);

63.                                ZipStream.Write(buffer, 0, sizeRead);

64.                            }

65.                            while (sizeRead > 0);

66.                        }

67.                        catch (System.Exception ex)

68.                        {

69.                            throw ex;

70.                        }

71.                        StreamToZip.Close();

72.                    }

73.                    ZipStream.Finish();

74.                    ZipStream.Close();

75.                }

76.                ZipFile.Close();

77.            }

78.        }

79.

80.        /// <summary>

81.        /// 压缩单个文件

82.        /// </summary>

83.        /// <param name="fileToZip">要进行压缩的文件名</param>

84.        /// <param name="zipedFile">压缩后生成的压缩文件名</param>

85.        public static void ZipFile(string fileToZip, string zipedFile)

86.        {

87.            //如果文件没有找到,则报错

88.            if (!File.Exists(fileToZip))

89.            {

90.                throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");

91.            }

92.

93.            using (FileStream fs = File.OpenRead(fileToZip))

94.            {

95.                byte[] buffer = new byte[fs.Length];

96.                fs.Read(buffer, 0, buffer.Length);

97.                fs.Close();

98.

99.                using (FileStream ZipFile = File.Create(zipedFile))

100.                       {

101.                           using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))

102.                           {

103.                               string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);

104.                               ZipEntry ZipEntry = new ZipEntry(fileName);

105.                               ZipStream.PutNextEntry(ZipEntry);

106.                               ZipStream.SetLevel(5);

107.        

108.                               ZipStream.Write(buffer, 0, buffer.Length);

109.                               ZipStream.Finish();

110.                               ZipStream.Close();

111.                           }

112.                       }

113.                   }

114.               }

115.        

116.               /// <summary>

117.               /// 压缩多个文件到指定路径

118.               /// </summary>       

119.               /// <param name="sourceFileNames">压缩到哪个路径</param>

120.               /// <param name="zipFileName">压缩文件名称</param>

121.               public static void ZipFile(List<string> sourceFileNames, string zipFileName)

122.               {

123.                   //压缩文件打包

124.                   using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))

125.                   {

126.                       s.SetLevel(9);

127.                       byte[] buffer = new byte[4096];

128.                       foreach (string file in sourceFileNames)

129.                       {

130.                           if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件

131.                           {

132.                               string pPath = "";

133.                               pPath += Path.GetFileName(file);

134.                               pPath += "\\";

135.                               ZipSetp(file, s, pPath, sourceFileNames);

136.                           }

137.                           else // 否则直接压缩文件

138.                           {

139.                               ZipEntry entry = new ZipEntry(Path.GetFileName(file));

140.                               entry.DateTime = DateTime.Now;

141.                               s.PutNextEntry(entry);

142.                               using (FileStream fs = File.OpenRead(file))

143.                               {

144.                                   int sourceBytes;

145.                                   do

146.                                   {

147.                                       sourceBytes = fs.Read(buffer, 0, buffer.Length);

148.                                       s.Write(buffer, 0, sourceBytes);

149.                                   } while (sourceBytes > 0);

150.                               }

151.                           }

152.                       }

153.                       s.Finish();

154.                       s.Close();

155.                   }

156.               }

157.        

158.               /// <summary>

159.               /// 压缩多层目录

160.               /// </summary>

161.               /// <param name="strDirectory">待压缩目录</param>

162.               /// <param name="zipedFile">压缩后生成的压缩文件名,绝对路径</param>

163.               public static void ZipFileDirectory(string strDirectory, string zipedFile)

164.               {

165.                   using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))

166.                   {

167.                       using (ZipOutputStream s = new ZipOutputStream(ZipFile))

168.                       {

169.                           s.SetLevel(9);

170.                           ZipSetp(strDirectory, s, "");

171.                       }

172.                   }

173.               }

174.        

175.               /// <summary>

176.               /// 压缩多层目录

177.               /// </summary>

178.               /// <param name="strDirectory">待压缩目录</param>

179.               /// <param name="zipedFile">压缩后生成的压缩文件名,绝对路径</param>

180.               /// <param name="files">指定要压缩的文件列表(完全路径)</param>

181.               public static void ZipFileDirectory(string strDirectory, string zipedFile, List<string> files)

182.               {

183.                   using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))

184.                   {

185.                       using (ZipOutputStream s = new ZipOutputStream(ZipFile))

186.                       {

187.                           s.SetLevel(9);

188.                           ZipSetp(strDirectory, s, "", files);

189.                       }

190.                   }

191.               }

192.        

193.               /// <summary>

194.               /// 递归遍历目录

195.               /// </summary>

196.               /// <param name="strDirectory">需遍历的目录</param>

197.               /// <param name="s">压缩输出流对象</param>

198.               /// <param name="parentPath">The parent path.</param>

199.               /// <param name="files">需要压缩的文件</param>

200.               private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath, List<string> files = null)

201.               {

202.                   if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)

203.                   {

204.                       strDirectory += Path.DirectorySeparatorChar;

205.                   }

206.        

207.                   string[] filenames = Directory.GetFileSystemEntries(strDirectory);

208.        

209.                   byte[] buffer = new byte[4096];

210.                   foreach (string file in filenames)// 遍历所有的文件和目录

211.                   {

212.                       if (files != null && !files.Contains(file))

213.                       {

214.                           continue;

215.                       }

216.                       if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件

217.                       {

218.                           string pPath = parentPath;

219.                           pPath += Path.GetFileName(file);

220.                           pPath += "\\";

221.                           ZipSetp(file, s, pPath, files);

222.                       }

223.                       else // 否则直接压缩文件

224.                       {

225.                           //打开压缩文件

226.                           string fileName = parentPath + Path.GetFileName(file);

227.                           ZipEntry entry = new ZipEntry(fileName);

228.        

229.                           entry.DateTime = DateTime.Now;

230.        

231.                           s.PutNextEntry(entry);

232.                           using (FileStream fs = File.OpenRead(file))

233.                           {

234.                               int sourceBytes;

235.                               do

236.                               {

237.                                   sourceBytes = fs.Read(buffer, 0, buffer.Length);

238.                                   s.Write(buffer, 0, sourceBytes);

239.                               } while (sourceBytes > 0);

240.                           }

241.                       }

242.                   }

243.               }

244.        

245.               /// <summary>

246.               /// 解压缩一个 zip 文件。

247.               /// </summary>

248.               /// <param name="zipedFile">压缩文件</param>

249.               /// <param name="strDirectory">解压目录</param>

250.               /// <param name="password">zip 文件的密码。</param>

251.               /// <param name="overWrite">是否覆盖已存在的文件。</param>

252.               public static void UnZip(string zipedFile, string strDirectory, bool overWrite, string password)

253.               {

254.                   if (strDirectory == "")

255.                       strDirectory = Directory.GetCurrentDirectory();

256.                   if (!strDirectory.EndsWith("\\"))

257.                       strDirectory = strDirectory + "\\";

258.        

259.                   using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))

260.                   {

261.                       if (password != null)

262.                       {

263.                           s.Password = password;

264.                       }

265.                       ZipEntry theEntry;

266.                       while ((theEntry = s.GetNextEntry()) != null)

267.                       {

268.                           string directoryName = "";

269.                           string pathToZip = "";

270.                           pathToZip = theEntry.Name;

271.        

272.                           if (pathToZip != "")

273.                               directoryName = Path.GetDirectoryName(pathToZip) + "\\";

274.                           string fileName = Path.GetFileName(pathToZip);

275.                           Directory.CreateDirectory(strDirectory + directoryName);

276.                           if (fileName != "")

277.                           {

278.                               if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))

279.                               {

280.                                   using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))

281.                                   {

282.                                       int size = 2048;

283.                                       byte[] data = new byte[2048];

284.                                       while (true)

285.                                       {

286.                                           size = s.Read(data, 0, data.Length);

287.                                           if (size > 0)

288.                                               streamWriter.Write(data, 0, size);

289.                                           else

290.                                               break;

291.                                       }

292.                                       streamWriter.Close();

293.                                   }

294.                               }

295.                           }

296.                       }

297.                       s.Close();

298.                   }

299.               }

300.        

301.               /// <summary>

302.               /// 解压缩一个 zip 文件。

303.               /// </summary>

304.               /// <param name="zipedFile">压缩文件</param>

305.               /// <param name="strDirectory">解压目录</param>

306.               /// <param name="overWrite">是否覆盖已存在的文件。</param>

307.               public static void UnZip(string zipedFile, string strDirectory, bool overWrite)

308.               {

309.                   UnZip(zipedFile, strDirectory, overWrite, null);

310.               }

311.        

312.               /// <summary>

313.               /// 解压缩一个 zip 文件。

314.               /// 覆盖已存在的文件。

315.               /// </summary>

316.               /// <param name="zipedFile">压缩文件</param>

317.               /// <param name="strDirectory">解压目录</param>

318.               public static void UnZip(string zipedFile, string strDirectory)

319.               {

320.                   UnZip(zipedFile, strDirectory, true);

321.               }

322.        

323.               /// <summary>

324.               /// 获取压缩文件中指定类型的文件

325.               /// </summary>

326.               /// <param name="zipedFile">压缩文件</param>

327.               /// <param name="fileExtension">文件类型(.txt|.exe)</param>

328.               /// <returns>文件名称列表(包含子目录)</returns>

329.               public static List<string> GetFiles(string zipedFile, List<string> fileExtension)

330.               {

331.                   List<string> files = new List<string>();

332.                   if (!File.Exists(zipedFile))

333.                   {

334.                       //return files;

335.                       throw new FileNotFoundException(zipedFile);

336.                   }

337.        

338.                   using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))

339.                   {

340.                       ZipEntry theEntry;

341.                       while ((theEntry = s.GetNextEntry()) != null)

342.                       {

343.                           if (theEntry.IsFile)

344.                           {

345.                               //Console.WriteLine("Name : {0}", theEntry.Name);

346.                               if (fileExtension != null)

347.                               {

348.                                   if (fileExtension.Contains(Path.GetExtension(theEntry.Name)))

349.                                   {

350.                                       files.Add(theEntry.Name);

351.                                   }

352.                               }

353.                               else

354.                               {

355.                                   files.Add(theEntry.Name);

356.                               }

357.                           }

358.                       }

359.                       s.Close();

360.                   }

361.                   return files;

362.               }

363.        

364.               /// <summary>

365.               /// 获取压缩文件中的所有文件

366.               /// </summary>

367.               /// <param name="zipedFile">压缩文件</param>

368.               /// <returns>文件名称列表(包含子目录)</returns>

369.               public static List<string> GetFiles(string zipedFile)

370.               {

371.                   return GetFiles(zipedFile, null);

372.               }

373.           }//Class_end

374.       }

三、SharpZipLib类的用法参考

SharpZipLib 文件/文件夹压缩 - 逆心 - 博客园 (cnblogs.com)

C# 利用ICSharpCode.SharpZipLib实现在线压缩和解压缩


该文章在 2023/12/31 16:51:29 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved