【发布时间】:2015-06-17 06:16:13
【问题描述】:
我正在使用 C# 开发 Windows 窗体应用程序,而我正在通过我的 Windows 窗体应用程序运行 PDF。早些时候,我使用的是 MS SQL Server(它工作正常),但现在从 XML 文件中获取数据并将文件从字节转换为 PDF 文件(pdf 格式)。这是我抛出错误的方法:
public static byte[] GetUnCompressedData(byte[] value)
{
try
{
if (value != null)
using (var zipInputStream = new ZipInputStream(new MemoryStream(value)))
{
while ((zipInputStream.GetNextEntry()) != null)
{
using (var zippedInMemoryStream = new MemoryStream())
{
var data = new byte[2048];
while (true)
{
var size = zipInputStream.Read(data, 0, data.Length);
if (size <= 0)
break;
zippedInMemoryStream.Write(data, 0, size);
/// new code added
///
zippedInMemoryStream.Flush();
}
//zippedInMemoryStream.Close();
GC.WaitForPendingFinalizers();
GC.Collect();
return zippedInMemoryStream.ToArray();
}
}
}
return null;
}
catch (Exception)
{
throw;
}
}
这是上述方法中抛出错误的那一行:
using (var zippedInMemoryStream = new MemoryStream())
我在 Windows 8/7 中遇到以下错误:
file does not begin with '%pdf%-'
在调试方法时,我还遇到了另一个错误:
Wrong Local header signature: 0x41430A02
工作环境:Visual Studio 2013、.Net Framework 4.5、Acrobat PDF 组件、XML 第三方控制:使用 csharp 代码解压缩/压缩数据,使用 http://icsharpcode.github.io/SharpZipLib/
我还提供了使用以下方法压缩数据的功能:
public static byte[] GetCompressedData(string fileName, byte[] value)
{
try
{
// Code for zip file.
if (value != null && !string.IsNullOrEmpty(fileName))
using (var zippedMemoryStream = new MemoryStream())
{
// A ZIP stream
using (var zipOutputStream = new ZipOutputStream(zippedMemoryStream))
{
// Highest compression rating 0 - 9.
zipOutputStream.SetLevel(9);
var entry = new ZipEntry(fileName) { DateTime = DateTime.Now };
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(value, 0, ConvertToInt32(value.Length));
zipOutputStream.Finish();
zipOutputStream.Close();
return zippedMemoryStream.ToArray();
}
}
}
catch (Exception)
{
throw;
}
return null;
}
【问题讨论】:
-
尝试捕捉 ZipException 和 IOException 以确定您有什么异常(是 I/O 还是压缩文件时)。
-
是的,我这样做了,抛出错误 Zip Exception Caught: Wrong Local header signature: 0x44734555
-
您的文件可能格式不正确。请检查它是一个有效的 PDF 文件,并且您在
byte[] values中传递了有效数据。 -
是的,相同的数据与 SQL Server 一起使用,但它工作正常,但是在将 SQL Server 迁移到 XML 格式并尝试使用 XML 中的字节(它只有字符串格式,但 sql 提供不同的数据类型)转换为PDF 文件然后给出错误。
-
您能尝试使用外部文件解包器解压缩您的
value吗?那么请告诉我们 ZIP 图像无效(压缩问题)还是只有 C# 解压缩无效(解压缩问题)。