打开XPSDocument包时获取“文件包含损坏的数据”异常

时间:2017-11-10 07:15:06

标签: xpsdocument system.io.packaging

我在控制台应用程序中收到异常

HResult -2146233033 InnerException  null 
Message File contains corrupted data. 
Source  WindowsBase SourceUri   null StackTrace    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.OpenOnStream(Stream stream, FileMode mode, FileAccess access, Boolean streaming)    at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming)    at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)  

尝试了这个寻找答案,但没有帮助。

string xpsfilename = @"D:\test\sample_file.xps";

var webClient = new System.Net.WebClient();

var data = webClient.DownloadData(xpsfilename);
//var data = File.ReadAllBytes(xpsfilename);
using (MemoryStream stream = new System.IO.MemoryStream(data))
{
    var package = System.IO.Packaging.Package.Open(stream); /* Exception thrown here */
    var xpsDocument = new System.Windows.Xps.Packaging.XpsDocument(package, System.IO.Packaging.CompressionOption.SuperFast, xpsfilename);
    var sequence = xpsDocument.GetFixedDocumentSequence();

    SaveDocumentPagesToImages(sequence, @"D:\test");
}

0 个答案:

没有答案