首页 > 其他 > 详细

C# 解压及压缩文件源码

时间:2014-04-23 11:17:39      阅读:596      评论:0      收藏:0      [点我收藏+]
using System.IO;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
   public void unZip(string strSouceFile, string strDestFile)
        {
            if (!Directory.Exists(strDestFile))
                Directory.CreateDirectory(strDestFile);
            using (ZipInputStream zip = new ZipInputStream(File.OpenRead(strSouceFile)))
            {
                ZipEntry theEntry;
                string fileName;
                FileStream streamWriter;
                while ((theEntry = zip.GetNextEntry()) != null)
                {
                    fileName = Path.GetFileName(theEntry.Name);

                    if (fileName != String.Empty)
                    {
                        streamWriter = new FileStream(strDestFile + fileName, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Write);

                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = zip.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);

                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
                MessageBox.Show("解压文件成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        public void ZipFile(string strSouceFile, string strDestFile)
        {
            using (ZipOutputStream stream = new ZipOutputStream(File.Create(strDestFile)))
            {
                stream.SetLevel(5);
                byte[] buffer = new byte[0x1000];
                ZipEntry entry = new ZipEntry(Path.GetFileName(strSouceFile));
                entry.DateTime = DateTime.Now;
                stream.PutNextEntry(entry);
                using (FileStream stream2 = File.OpenRead(strSouceFile))
                {
                    int num;
                    do
                    {
                        num = stream2.Read(buffer, 0, buffer.Length);
                        stream.Write(buffer, 0, num);
                    }
                    while (num > 0);
                    MessageBox.Show("压缩文件成功!");
                }
                stream.Finish();
                stream.Close();
            }
        }
需要引用的dll,下载以下页面的dll
http://download.csdn.net/detail/sky_cat/7236675

C# 解压及压缩文件源码,布布扣,bubuko.com

C# 解压及压缩文件源码

原文:http://blog.csdn.net/sky_cat/article/details/24336141

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!