WinForm解压缩

      private void button1_Click(object sender, EventArgs e)
        {
            //1.压缩包的路径及压缩包的名称
            //2.解压缩的路径及名称
            string rarPath = @"E:\test3.rar";//压缩文件的路径
            string extract = @"E:\test3";//解压缩的路径
       
            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;
                            }
                        }
                    }
                }
            }
        }
posted @ 2020-07-02 11:17  我们好像在那见过  阅读(498)  评论(0编辑  收藏  举报