PCB 无需解压,直接读取Genesis TGZ指定文件 实现方法

通过无需解压读取ZIP压缩包的方法,寻思者如何可以不解压直接读Genesis TGZ文件内容,

通过查找资料,原来可以通过:SharpCompress.dll工具实现此需求,此工具如此NB

感谢互联网无私奉献的网友分享,在此之前一直想完成此功能,今天终于实现了。开心。。。

一.SharpCompress  读取TGZ小结:

     1.读取TGZ内容,无法直接直接指定路径读取文件,只能通过MoveToNextEntry一个一个往下遍历查找文件

     2.MoveToNextEntry 遍历文件,采用的是深度递归

     3.区分文件名与文方件夹,无法通过Entry.IsDirectory 区分,识别方法:尾随 / 斜杆为文件夹,否则为文件

二.通过写实际例子来对此工具的用法进行说明这样来得更实际一些。哈哈

        例1:读取TGZ  STEP中有多少个step

        private string readTGZ_Dir()
        {
            string line = "";
            string tarFilePath = @"F:\2p00802ya0.tgz";
            string FileName = Path.GetFileNameWithoutExtension(tarFilePath);
            string FindDir = $@"{FileName}/steps/";  //以/斜杆结尾为文件夹
            using (Stream stream = File.OpenRead(tarFilePath))
            {
                IReader reader = ReaderFactory.Open(stream);
                while (reader.MoveToNextEntry())
                {
                    Match matchresult = Regex.Match(reader.Entry.Key, $@"^{FindDir}([\w\s]+)/$");
                    if (matchresult.Success)
                    {
                        line += matchresult.Groups[1].Value + "  ";
                    }
                }
            }
            return line;
        }

    例2:读取TGZ  指定standard字体内容

        private string readTGZ_File()
        {
            string line = "";
            string tarFilePath = @"F:\2p00802ya0.tgz";
            string FileName = Path.GetFileNameWithoutExtension(tarFilePath);
            string FindFile = $@"{FileName}/fonts/standard";
            using (Stream stream = File.OpenRead(tarFilePath))
            {
                IReader reader = ReaderFactory.Open(stream);
                while (reader.MoveToNextEntry())
                {
                    if (reader.Entry.Key == FindFile)
                    {
                        EntryStream st = reader.OpenEntryStream();
                        byte[] byData = new byte[reader.Entry.Size];
                        st.Read(byData, 0, byData.Length);
                        line = System.Text.Encoding.Default.GetString(byData);  //纯英文读取
                         //line = System.Text.Encoding.UTF8.GetString(byData);     //含中文读取
                        st.Close();
                        break;
                    }
                }
            }
            return line;
        }

猜你喜欢

转载自www.cnblogs.com/pcbren/p/9368927.html