利用SharpZipLib压缩、解压文件

 public class ZipInfo
    {
        /// <summary>
        ///C#压缩文件
        /// </summary>
        /// <param name="filepath">想要压缩文件的地址</param>
        /// <param name="zippath">输出压缩文件的地址</param>
        public void GetFileToZip(string filepath, string zippath)
        {

            FileStream fs = File.OpenRead(filepath);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
             

            FileStream ZipFile = File.Create(zippath);
            ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
            ZipEntry ZipEntry = new ZipEntry("/");
            ZipStream.PutNextEntry(ZipEntry);
            ZipStream.SetLevel(6);

            ZipStream.Write(buffer, 0, buffer.Length);
            ZipStream.Finish();
            ZipStream.Close();
        }
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="address"></param>
        private void FileToZip(string path, string address)
        {
            string name = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("\\") + 1);
            FileStream StreamToZip = new FileStream(path, FileMode.Open, FileAccess.Read);
            FileStream ZipFile = File.Create(address);
            ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
            //压缩文件
            ZipEntry ZipEntry = new ZipEntry(name);
            ZipStream.PutNextEntry(ZipEntry);
            ZipStream.SetLevel(6);
            byte[] buffer = new byte[StreamToZip.Length];
            StreamToZip.Read(buffer, 0, Convert.ToInt32(StreamToZip.Length));
            ZipStream.Write(buffer, 0, Convert.ToInt32(StreamToZip.Length));
            ZipStream.Finish();
            ZipStream.Close();
            StreamToZip.Close();

        }
        /// <summary>
        /// 解压到一个目录
        /// </summary>
        /// <param name="path"></param>
        /// <param name="addres"></param>
        /// <returns></returns>
        public bool ZipToFile(string path, string addres)
        {
            try
            {
                ZipInputStream s = new ZipInputStream(File.OpenRead(path));
                ZipEntry fileEntry;
                while ((fileEntry = s.GetNextEntry()) != null)
                {
                    string filename = Path.GetFileName(fileEntry.Name);
                    if (filename != "")
                    {
                        filename = addres + filename;
                        FileStream streamWriter = File.Create(filename);
                        int size = 2048;
                        byte[] buffer = new byte[s.Length];

                        size = s.Read(buffer, 0, buffer.Length);
                        streamWriter.Write(buffer, 0, size);
                        streamWriter.Close();
                    }
                }
                s.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// #压缩目录
        /// </summary>
        /// <param name="path">要压缩的目录</param>
        /// <param name="address">输出的压缩文件</param>
        private void DirectoryToZip(string path, string address)
        {
            //获取当前文件夹中所有的文件
            string[] filenames = Directory.GetFiles(path);
            Crc32 crc = new Crc32();
            //创建输出文件(ZIP格式的文件)
            ZipOutputStream zos = new ZipOutputStream(File.Create(address));
            zos.SetLevel(6);
            //遍历所有的文件
            foreach (string name in filenames)
            {
                FileStream fs = File.OpenRead(name);
                byte[] buffer = new byte[fs.Length];
                //读取文件
                fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
                //获取文件的文件名称和后缀名
                string file = Path.GetFileName(name);
                //输出文件的名称
                ZipEntry entry = new ZipEntry(file);
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, Convert.ToInt32(fs.Length));
                fs.Close();
            }
            zos.Finish();
            zos.Close();
        }




    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值