使用DotNetZip压缩与解压缩

本文介绍了如何使用DotNetZip进行文件的压缩与解压缩。首先提供了DotNetZip库的下载链接,然后详细说明了如何将库集成到Visual Studio项目中,包括将dll文件复制到指定位置,通过gacutil和regasm命令注册dll,最后将其添加为项目引用。通过这些步骤,开发者可以方便地在自己的应用中使用DotNetZip进行文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

下载地址:http://dotnetzip.codeplex.com/

 

解压后找到\\DotNetZipLib-DevKit-v1.9\zip-v1.9\Release下的Ionic.Zip.dll文件拷贝到C盘下

 

接着打开Visual Studio Command Prompt (2010),然后定位到C盘根目录,依次输入gacutil -i Ionic.Zip.dllregasm Ionic.Zip.dll ,然后拷贝C盘下的这个Ionic.Zip.dll到您的应用程序中,添加引用即可!代码例子如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ionic.Zip;
namespace WindowsFormsZIP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 压缩带中文的文件名.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button1_Click(object sender, EventArgs e)
        {

            using (ZipFile zip = new ZipFile("苏志.zip",Encoding.Default))
            {
                zip.AddFile("数据库文档.doc");
                zip.Save();
            }

        }

        /// <summary>
        /// 用密码加密ZIP文件.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button2_Click(object sender, EventArgs e)
        {

            using (ZipFile zip = new ZipFile())
            {
                zip.Password = "123456!";
                zip.AddFile("WPF 4 Unleashed.pdf");
                zip.Save("WPF 4 Unleashed.zip");
            }

        }

        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button3_Click(object sender, EventArgs e)
        {
            using (ZipFile zip = new ZipFile())
            {
                zip.AddDirectory(@"E:\suzhi");
                zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
                zip.Save("test.zip");
            }
        }

        /// <summary>
        /// 抽取ZIP中的文件到指定的文件夹.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button4_Click(object sender, EventArgs e)
        {
            using (ZipFile zip = ZipFile.Read("test.zip"))
            {
                foreach (ZipEntry z in zip)
                {
                    z.Extract(@"F:\kk");
                }
            }

        }

    }
}


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值