[英]Create ZipArchive from XML with base64 encoded content
我正在動態創建XML文件。 它的節點之一包含一個編碼為BASE64字符串的ZIP文件。
然后,我創建另一個ZIP文件。 我添加了此XML文件和其他一些JPEG文件。 我將文件輸出到瀏覽器。
我無法打開FINAL ZIP文件。 我得到:“ Windows無法打開該文件夾。壓縮(壓縮)文件夾'c:\\ path \\ file.zip'無效。”
我可以將原始XML文件保存到文件系統中。 我可以打開該XML文件,解碼ZIP節點並保存到文件系統。 然后,我可以毫無問題地打開該Zip文件。
我可以創建最終的ZIP文件,忽略我的XML文件,並且ZIP文件打開沒有問題。
我似乎對嘗試對一個XML文件進行ZIP壓縮只有一個問題,該XML文件的節點具有ZIP內容編碼為BASE64字符串。
有任何想法嗎? 代碼段如下。 大量編輯。
XDocument xDoc = new XDocument();
XDocument xDocReport = new XDocument();
XElement xNodeReport;
using (FileStream fsData = new FileStream(strFullFilePath, FileMode.Open, FileAccess.Read)) {
xDoc = XDocument.Load(fsData);
xNodeReport = xDoc.Element("Data").Element("Reports").Element("Report");
//SNIP
//create XDocument xDocReport
//SNIO
using (MemoryStream zipInMemoryReport = new MemoryStream()) {
using (ZipArchive zipFile = new ZipArchive(zipInMemoryReport, ZipArchiveMode.Update)) {
//Add REPORT to ZIP file
ZipArchiveEntry entryReport = zipFile.CreateEntry("data.xml");
using (StreamWriter writer = new StreamWriter(entryReport.Open())) {
writer.Write(xDocReport.ToString());
} //END USING report entry
}
xNodeReport.Value = System.Convert.ToBase64String(zipInMemoryReport.GetBuffer());
//I am able to write this file to disk and manipulate it no problem.
//File.WriteAllText("c:\\users\\snip\\desktop\\Report.xml",xDoc.ToString());
}
//create ZIP for response
using (MemoryStream zipInMemory = new MemoryStream()) {
using (ZipArchive zipFile = new ZipArchive(zipInMemory, ZipArchiveMode.Update)) {
//Add REPORT to ZIP file
ZipArchiveEntry entryReportWrapper = zipFile.CreateEntry("Report.xml");
//THIS IS THE STEP THAT makes the Zip "invalid". Although i can open and manipulate this source file no problem.
//********
using (StreamWriter writer = new StreamWriter(entryReportWrapper.Open())) {
xDoc.Save(writer);
}
//Add JPEG(s) to report
//Create Charts
if (chkDLSalesPrice.Checked) {chartDownloadSP.SaveImage(entryChartSP.Open(), ChartImageFormat.Jpeg);}
if (chkDLSalesDOM.Checked) {chartDownloadDOM.SaveImage(entryChartDOM.Open(), ChartImageFormat.Jpeg);}
if (chkDLSPLP.Checked) {chartDownloadSPLP.SaveImage(entryChartSPLP.Open(), ChartImageFormat.Jpeg);}
if (chkDLSPLP.Checked) {chartDownloadLP.SaveImage(entryChartLP.Open(), ChartImageFormat.Jpeg);}
} // END USING ziparchive
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=file.zip");
Response.ContentType = "application/zip";
Response.BinaryWrite(zipInMemory.GetBuffer());
Response.End();
如果沒有一個好的, 最小的 , 完整的代碼示例 ,就不可能確定代碼中存在哪些錯誤。 但是您發布的代碼段中至少存在兩個明顯的錯誤,其中一個很容易導致“ invalid .zip”錯誤:
writer.Write(xDocReport.ToString());
,變量xDocReport
尚未初始化為任何有用的函數,至少在您發布的代碼中沒有初始化。 因此,您將在存檔中獲得一個空的XML文檔。 由於代碼示例不完整,因此您可能會在問題中的代碼示例中省略了將該變量初始化為其他內容的代碼。 無論如何,即使您不這樣做,也只會導致存檔中的XML文檔為空,而不是無效的存檔。
雖然有更多問題……
MemoryStream
對象而不是ToArray()
調用GetBuffer()
ToArray()
。 您想要后者。 前者獲取MemoryStream
對象的整個后備緩沖區,包括有效流末尾的未初始化字節。 由於有效的.zip文件在文件末尾包含CRC值,因此添加額外的數據會導致嘗試將其作為.zip歸檔文件讀取的任何內容丟失正確的CRC,而是讀取未初始化的數據。 GetBuffer()
的調用替換為對ToArray()
調用。
如果上述方法不能解決您的問題,則應編輯您的文章,以提供更好的代碼示例。
最后一條評論:如果只想用一個不同的對象替換該對象(例如,通過調用XDocument.Load()
), xDoc
這樣的變量初始化為一個空的XDocument
對象沒有任何意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.