簡體   English   中英

new XpsDocument(string, FileAccess) 失敗並顯示“文件包含損壞的數據”,即使文件沒有損壞

[英]new XpsDocument(string, FileAccess) fails with “File contains corrupted data”, even though the file is not corrupt

我正在處理一個 SQLCE 數據庫,其中一個表有一個image類型列,用於存儲 XPS 文檔的原始二進制數據。

我正在將此數據讀入byte[] ,然后將其保存到磁盤,如下所示:

File.WriteAllBytes(myPath, myByteArray);

這有效。 我可以雙擊myPath的文件並在 Microsoft XPS 查看器中查看它。 如果我將其重命名為 ZIP 文件,則可以在 WinZip 中打開它。

但是當我嘗試將完全相同的文件加載到我的 WPF 應用程序中的 DocumentViewer 時,如下所示:

var xpsDocument = new XpsDocument(myPath, FileAccess.Read);
var sequence = xpsDocument.GetFixedDocumentSequence();
// ...

它在第一行失敗,但有以下異常:

File contains corrupted data.

A System.IO.FileFormatException occurred
   at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(Stream archiveStream)
   at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager)
   at MS.Internal.IO.Zip.ZipArchive..ctor(Stream archiveStream, FileMode mode, FileAccess access, Boolean streaming, Boolean ownStream)
   at MS.Internal.IO.Zip.ZipArchive.OpenOnFile(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
   at System.IO.Packaging.ZipPackage..ctor(String path, FileMode mode, FileAccess access, FileShare share, Boolean streaming)
   at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare, Boolean streaming)
   at System.Windows.Xps.Packaging.XpsManager..ctor(String path, FileAccess packageAccess, CompressionOption compressionOption)
   at System.Windows.Xps.Packaging.XpsDocument..ctor(String path, FileAccess packageAccess, CompressionOption compressionOption)

我不明白為什么該文件會在 Microsoft XPS Viewer / WinZip 中打開(表明它實際上沒有損壞),而不是通過我的代碼打開。

令人沮喪的是,它並不一致。 對於數據庫中的某些值,它起作用,而對於其他值則不起作用。 (盡管哪些值會失敗,哪些不會失敗是一致的)。

有沒有人以前遇到過這個問題,或者知道原因/解決方法?

您可能需要將字節作為流讀取並使用 xps 打包。 這個解決方案對我有用:

var webClient = new System.Net.WebClient();
var data = webClient.DownloadData(myPath);
var package = System.IO.Packaging.Package.Open(new System.IO.MemoryStream(data));
var xpsDocument = new System.Windows.Xps.Packaging.XpsDocument(package,
                                                          System.IO.Packaging.CompressionOption.SuperFast,
                                                          myPath);
var sequence = xpsDocument.GetFixedDocumentSequence();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM