使用 SevenZip.SevenZipExtractor.ExtractFile實現
public static String docxParser(String filename)
{
//path to the systems temporary folder
String tempFolderPath = Path.GetTempPath();
//set the path of the 7z.dll (it needs to be in the debug folder)
SevenZipExtractor.SetLibraryPath("7z.dll");
SevenZipExtractor extractor = new SevenZipExtractor(filename);
//create a filestream for the file we are going to extract
FileStream f = new FileStream(tempFolderPath + "document.xml", FileMode.Create);
//extract the document.xml
extractor.ExtractFile("word\\document.xml", f);
//get rid of the object because it is unmanaged
extractor.Dispose();
//close the filestream
f.Close();
//send document.xml that we extracted from the .docx to the xml parser
String result = XMLParser.Parser.ParseXMLtoString(tempFolderPath + "document.xml");
//delete the extracted file from the temp folder
File.Delete(tempFolderPath + "document.xml");
return result;
}
public TickReader(FileInfo file)
{
Serializer = new PbTickSerializer();
Codec = new QuantBox.Data.Serializer.V2.PbTickCodec();
_stream = new MemoryStream();
_originalLength = (int)file.Length;
// 加載文件,支持7z解壓
var fileStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
{
try
{
using (var zip = new SevenZipExtractor(fileStream))
{
zip.ExtractFile(0, _stream);
_stream.Seek(0, SeekOrigin.Begin);
}
}
catch
{
_stream = fileStream;
_stream.Seek(0, SeekOrigin.Begin);
}
}
}
通過無需解壓讀取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; }