C# 解压缩

解压缩

        /// <summary>
        /// 解压缩文件
        /// </summary>
        /// <param name="dirPath">解压到目录</param>
        /// <param name="bytes">文件字节流</param>
        public static void UnZip(string dirPath, byte[] bytes, ProgressBar pb, ProgressBar pbTotal)
        {
            pb.Minimum = 0;
            pb.Value = 0;
            pb.Maximum = ZipCount(bytes);

            ZipInputStream s = new ZipInputStream(new MemoryStream(bytes));

            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                pb.Value++;
                pbTotal.Value++;
                string fileName = Path.GetFileName(theEntry.Name);

                //生成解压目录
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                if (string.IsNullOrEmpty(fileName))
                {
                    string dir = System.IO.Path.Combine(dirPath, theEntry.Name);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                }
                else
                {
                    //解压文件到指定的目录
                    FileStream streamWriter = File.Create(System.IO.Path.Combine(dirPath, theEntry.Name));

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

                    streamWriter.Close();
                }
            }
            s.Close();
        }
        /// <summary>
        /// 计算文件数量
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static int ZipCount(byte[] bytes)
        {
            int count = 0;
            ZipInputStream s = new ZipInputStream(new MemoryStream(bytes));
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                count++;

            }
            s.Close();
            return count;
        }
原文地址:https://www.cnblogs.com/Shadow3627/p/3027256.html