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; }
作者:pcbren 微信号:yadnfku QQ号: 254566449
博客地址:https://www.cnblogs.com/pcbren/
声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权,贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。
如果大家感觉我的博文对大家有帮助,请推荐支持一把。