【问题标题】:new XpsDocument(string, FileAccess) fails with "File contains corrupted data", even though the file is not corruptnew XpsDocument(string, FileAccess) 失败并显示“文件包含损坏的数据”,即使文件没有损坏
【发布时间】:2013-12-04 07:05:27
【问题描述】:

我正在处理一个 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 中打开(表明它实际上没有损坏),但不是通过我的代码。

令人沮丧的是,它并不一致。对于数据库中的某些值,它有效,而对于其他值则无效。 (虽然它是一致的,哪些值会失败,哪些不会)。

以前有没有人遇到过这个问题,或者知道原因/解决方法?

【问题讨论】:

    标签: c# wpf corruption xps xpsdocument


    【解决方案1】:

    您可能需要将字节作为流读取并使用 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多