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

【C#】使用DotNetZip压缩与解压缩

admin
2024年1月19日 14:46 本文热度 368
DotnetZip是一个开源类库,支持.NET的任何语言,可很方便的创建,读取,和更新zip文件。而且还可以使用在.NETCompact Framework中。 

下载地址在这里:附件:DotNetZip.rar

解压后放到主程序同一个目录下,引用Ionic.Zip.dll就可以了:

然后引用这个命名空间:

using Ionic.Zip;

以下是使用代码:

01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09.using Ionic.Zip;
10.namespace WindowsFormsZIP
11.{
12.    public partial class Form1 : Form
13.    {
14.        public Form1()
15.        {
16.            InitializeComponent();
17.        }
18.
19.        /// <summary>
20.        /// 压缩带中文的文件名.  
21.        /// </summary>
22.        /// <param name="sender">The source of the event.</param>
23.        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
24.        private void button1_Click(object sender, EventArgs e)
25.        {
26.
27.            using (ZipFile zip = new ZipFile("苏志.zip",Encoding.Default))
28.            {
29.                zip.AddFile("数据库文档.doc");
30.                zip.Save();
31.            }
32.
33.        }
34.
35.        /// <summary>
36.        /// 用密码加密ZIP文件.  
37.        /// </summary>
38.        /// <param name="sender">The source of the event.</param>
39.        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
40.        private void button2_Click(object sender, EventArgs e)
41.        {
42.
43.            using (ZipFile zip = new ZipFile())
44.            {
45.                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;   //设置压缩率

45.                zip.Password = "123456!";

46.                zip.AddFile("WPF 4 Unleashed.pdf");

47.                zip.Save("WPF 4 Unleashed.zip");
48.            }
49.
50.        }
51.
52.        /// <summary>
53.        /// 压缩文件夹  
54.        /// </summary>
55.        /// <param name="sender">The source of the event.</param>
56.        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
57.        private void button3_Click(object sender, EventArgs e)
58.        {
59.            using (ZipFile zip = new ZipFile())
60.            {
61.                zip.AddDirectory(@"E:\suzhi");
62.                zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
63.                zip.Save("test.zip");
64.            }
65.        }
66.
67.        /// <summary>
68.        /// 抽取ZIP中的文件到指定的文件夹.  
69.        /// </summary>
70.        /// <param name="sender">The source of the event.</param>
71.        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
72.        private void button4_Click(object sender, EventArgs e)
73.        {
74.            using (ZipFile zip = ZipFile.Read("test.zip"))
75.            {
76.                foreach (ZipEntry z in zip)
77.                {
78.                    z.Extract(@"F:\kk");
79.                }
80.            }
81.
82.        }
83.
84.    }
85.}

具体的例子:

           //获取压缩包路径
           string filename = material.CreationTime.ToyyyyMMdd().Replace("-""");

           //string zippath = AppDomain.CurrentDomain.BaseDirectory + "upload\\Zipped\\" + filename;
           
//和上面的结果一样
           string zippath = material.FilePath;
           string decomPath = "";
           string nowpath = "";
           if (material.FilePath.Substring(material.FilePath.Length - 33).Equals("zip"))
           { decomPath = material.FilePath.Replace(".zip"""); }
           if (material.FilePath.Substring(material.FilePath.Length - 33).Equals("rar"))
           { decomPath = material.FilePath.Replace(".rar"""); }
           //查询此目录下是否存在需要解压过的同名的文件夹名称
           DirectoryInfo direInfo = new DirectoryInfo(decomPath);
           if (direInfo.Exists)
           {
               DirectoryInfo[] childs = direInfo.GetDirectories();
               foreach (DirectoryInfo child in childs)
               {
                   child.Delete(true);
               }
               direInfo.Delete(true);
           }

           //解压文件夹
           using (ZipFile zip = ZipFile.Read(zippath))
           {

               foreach (ZipEntry z in zip)
               {
                   //z.Extract(material.FilePath.Replace(".zip", ""));
                   z.Extract(zippath.Replace(".zip"""));
               }
           }

           direInfo = new DirectoryInfo(zippath.Replace(".zip"""));
           if (direInfo.Exists)
           {
               DirectoryInfo[] dirchild = direInfo.GetDirectories();
               if (dirchild.Count() == 1)
               {
                    nowpath = decomPath + "\\" + dirchild[0].Name;
                   direInfo = new DirectoryInfo(nowpath);
                   DirectoryInfo[] dirchild2 = direInfo.GetDirectories();
                   foreach (DirectoryInfo childrens in dirchild2)
                   {

                       //修改文件夹的内容
                       FileStream fs = new FileStream(nowpath +"\\"+ childrens.Name + "\\index.html", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                       string fileText = "";
                       GameServer server = GameServerService.GetGameServerWithGame(materialLog.ServerId);
                       Game game = server.Game;
                       using (StreamReader sr = new StreamReader(fs))
                       {
                           fileText = sr.ReadToEnd();
                           fileText = fileText.Replace("#GameCode#", game.Code);
                           fileText = fileText.Replace("#ServerCode#", server.Code);
                           fileText = fileText.Replace("#ServerId#", Convert.ToString(server.ServerId));
                       }
                       if (fileText != "")
                       {
                           using (StreamWriter sw = new StreamWriter(nowpath + "\\" + childrens.Name + "\\index.html"false, Encoding.Default))
                           {
                               sw.Write(fileText);
                           }
                       }

                   }
               }
               
           }

           //重新压缩

           using (ZipFile zip = new ZipFile())
           {
               zip.AddDirectory(decomPath);
               zip.Save(nowpath+".zip");
           }

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