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;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM