用SharpZipLib中的组件进行压缩和解压缩

近来做了一个小程序,其中用到了压缩与解压缩,在网上找到的都是些怪码,很是郁闷.现在我把自己的源码写出下来与大家分享!

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.GZip;
using System.IO;

记得不要忘了把在项目里引用sharpziplib.dll   (添加--引用---确定)

我用的是sharpziplib.dll  中的2.0

下面是压缩代码:

            this.saveFileDialog1.Filter = "压缩文件(zip)|*.zip";
            this.saveFileDialog1.Title = "保存文件";
            this.saveFileDialog1.FileName = "数据备份";
            this.saveFileDialog1.InitialDirectory = Application.StartupPath;
            this.saveFileDialog1.RestoreDirectory = true;
            string Fname = "";
            if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string[] filenames ={ "image", "Company.xml", "config.xml", "Room.xml", "RoomMaintain.xml", "RoomRent.xml",  "UserKey.xml" };//可以自己定义所压缩的文件
                Fname = this.saveFileDialog1.FileName;
                ZipOutputStream s = new ZipOutputStream(File.Create(Fname));//创建压缩文件的目录及名称
                s.Password = "jiang";//设置压缩文件的密码
                s.SetLevel(6);// 0 - store only to 9 - means best compression
                foreach (string file in filenames)
                {
                    if (Directory.Exists(Application.StartupPath + @"/" + file))
                    {
                        strBaseDir = Application.StartupPath + @"/" + file + @"/";//压缩文件夹
                        addZipEntry(strBaseDir,s);
                    }
                    else
                    {
                        FileStream fs = File.OpenRead(Application.StartupPath + @"/" + file);//打开文件读取
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string strEntryName = file;
                        ZipEntry entry = new ZipEntry(strEntryName);
                        s.PutNextEntry(entry);
                        s.Write(buffer, 0, buffer.Length);
                        fs.Close();
                    }
                }
                s.Finish();
                s.Close();
            }

        private void addZipEntry(string PathStr,ZipOutputStream s)//压缩文件夹及内容
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName,s);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                if (item.FullName.Replace(strBaseDir, "") != "Thumbs.db")
                {
                    FileStream fs = File.OpenRead(item.FullName);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    string strEntryName = item.FullName.Replace(strBaseDir, "");
                    strEntryName = @"image/" + strEntryName;
                    ZipEntry entry = new ZipEntry(strEntryName);
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                    fs.Close();
                }
            }
        }

下面代码是解压缩

            this.saveFileDialog1.Filter = "压缩文件(zip)|*.zip";
            this.openFileDialog1.Title = "打开文件";
            this.openFileDialog1.FileName = "数据备份";
            this.openFileDialog1.InitialDirectory = Application.StartupPath;
            this.openFileDialog1.RestoreDirectory = true;
            string Fname = "";
            if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Fname = this.openFileDialog1.FileName;
                Unzip(Fname, Application.StartupPath);
            }

        private void Unzip(string zipfile, string directory)//解压缩
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            ZipInputStream zis = new ZipInputStream(File.OpenRead(zipfile));
            string str = zis.Password;
            zis.Password = "jiang";//解压密码
            ZipEntry theEntry = null;
            while ((theEntry = zis.GetNextEntry()) != null)
            {
                ZipConstants.DefaultCodePage = 936;
                string directoryName = Path.GetDirectoryName(theEntry.Name);//directoryName=null
                string fileName = Path.GetFileName(theEntry.Name);
                //创建文件
                if (directoryName != string.Empty)
                {
                    if ((directoryName == "image") && (!Directory.Exists(directory + @"/" + directoryName)))
                    {
                        Directory.CreateDirectory(directory + @"/" + directoryName);
                    }
                }
                if (fileName != string.Empty)
                {
                    FileStream streamWriter = File.Create(Path.Combine(directory, theEntry.Name));
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = zis.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            zis.Close();
        }

现在压缩与解压缩都完成了,并且还带了压缩密码哦!真的很不错,如有问题,请大家告诉我哦!^^!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值