C#压缩与解压文件源码

//获得压缩的文件夹
            //压缩文件夹的名字
            string name = file + ".rar";
            //压缩文件的流对象
            // MessageBox.Show(name);
            ZipOutputStream output = new ZipOutputStream(File.Create(name));
            output.SetLevel(6);
            string[] dir = Directory.GetFiles(file);
            //存放文件数据
            Crc32 crc = new Crc32();
            foreach (string myFile in dir)
            {
                FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read);
                byte[] bt = new byte[fs.Length];
                fs.Read(bt, 0, bt.Length);
                //存储要压缩的文件
                ZipEntry entry = new ZipEntry(myFile);
                entry.Size = fs.Length;
                entry.DateTime = DateTime.Now;
                fs.Close();
                crc.Reset();  //清除crc内容
                crc.Update(bt);  //更新文件内容到crc中
                entry.Crc = crc.Value;   //将文件内容放到压缩文件中
                output.PutNextEntry(entry);
                //将数据写入压缩流中
                output.Write(bt, 0, bt.Length);
            }
            output.Close();
            MessageBox.Show("压缩成功"); 

 

解压文件

string file = this.txtUZip.Text;
            //读压缩文件内容流
            ZipInputStream input = new ZipInputStream(File.OpenRead(file));
            ZipEntry entry = input.GetNextEntry();
            //要存放的路径
            string dir = this.txtPath.Text;
            while (entry != null)
            {
                string filename = entry.Name;
                //读压缩文件内容
                int size = 2048;
                byte[] bt = new byte[size];
                FileStream inpuFs = File.Create(dir+filename.Substring(filename.LastIndexOf("//")));
                while (true)
                {
                    size = input.Read(bt, 0, bt.Length);
                    if (size > 0)
                    {
                        inpuFs.Write(bt, 0, size);
                    }
                    else
                    {
                        inpuFs.Close();
                        break;
                    }   
                }
                entry = input.GetNextEntry();
            }
            input.Close();

源码下载

http://download.csdn.net/source/394595

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值