zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    python实现从生成器中取固定角标的元素
    python-高阶函数
    git学习手册
    python批量进行文件修改操作
    python第一站
    LoadRunner,一个简单的例子
    python3中urllib2的问题
    gitlab 随笔
    sed 删除文本
    sed 修改文本
  • 原文地址:https://www.cnblogs.com/Shadow3627/p/3027256.html
Copyright © 2011-2022 走看看