• 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;
            }
  • 相关阅读:
    黑产及社会工程学-学习历程
    ACE_Get_Opt函数笔记
    PlatformIO+Jlink进行调试
    Mac下搭建基于PlatformIO的嵌入式开发环境(STM32开发)
    选择排序
    插入排序
    JPEG原理详解 (转载)
    双链表基本操作
    单链表基本操作
    顺序表基本操作
  • 原文地址:https://www.cnblogs.com/Shadow3627/p/3027256.html
Copyright © 2020-2023  润新知