zoukankan      html  css  js  c++  java
  • WinForm解压缩

          private void button1_Click(object sender, EventArgs e)
            {
                //1.压缩包的路径及压缩包的名称
                //2.解压缩的路径及名称
                string rarPath = @"E: est3.rar";//压缩文件的路径
                string extract = @"E: est3";//解压缩的路径
           
                using (ZipInputStream zis = new ZipInputStream(File.OpenRead(rarPath)))//首先读取文件流(内存中)
                {
                    ZipEntry entry = null;
                    while ((entry=zis.GetNextEntry())!=null)//提供了一个迭代的方法,每循环一次获取下一条数据 返回entry
                    {
                        string temp = entry.Name;//06/tt/tt/xx.txt//Name时一个虚拟的路径
                        //这一句是拼接物理路径虚拟路径到文件夹 E:\test1虚拟路径的文件夹
                        //用于创建文件夹
                        string directorName = extract +"\"+ Path.GetDirectoryName(temp);
                        string fileName = extract + "\"+entry.Name.Replace("/", "\");//获取物理文件的路径到文件
                        if (!Directory.Exists(directorName))  //判断存放解压出来的文件的存放目录是否存在
                        {
                            //创建文件夹的路径就是directorName的路径,创建文件夹时可以根据路径创建国歌文件夹
                            Directory.CreateDirectory(directorName);
                        }
                        //把一个流中的文件保存到磁盘上,首先是读取流中的数据字节, zis.read(data,0,data.length
                        using (FileStream stream = File.Create(fileName))
                        {
                            int size = 4096;//每次只是读取4096个字节
                            //设置缓冲区
                            byte[] data = new byte[4*1024];
                            //
                            while (true)
                            {
                                size= zis.Read(data, 0, data.Length);//这里时将zis的字节读取到data中
                                if (size > 0)
                                {
                                    stream.Write(data, 0, size);//写入到文件中
                                }
                                else {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
  • 相关阅读:
    Elasticsearch核心技术与实战-学习笔记
    在ABP中灵活使用AutoMapper
    使用log4net记录ABP日志
    Abp小知识-如何全局设置DontWrapResult属性
    《C#并发编程经典实例》学习笔记—2.7 避免上下文延续
    NEST 6.X升级到7.X
    django框架——十
    django——自定义分页
    django框架九
    orm数据库查询优化和数据库三大设计范式
  • 原文地址:https://www.cnblogs.com/GuoLianSheng/p/13223616.html
Copyright © 2011-2022 走看看