C#解码(解压缩)放气PDF文件的数据 [英] C# decode (decompress) Deflate data of PDF File
问题描述
我想解压缩在C#中的一些DeflateCoded数据(PDF提取)。
不幸的是我每一次例外发现无效数据而解码。
,但数据是有效的。
I would like to decompress in C# some DeflateCoded data (PDF extracted). Unfortunately I got every time the exception "Found invalid data while decoding.". But the data are valid.
private void Decompress()
{
FileStream fs = new FileStream(@"S:\Temp\myFile.bin", FileMode.Open);
//First two bytes are irrelevant
fs.ReadByte();
fs.ReadByte();
DeflateStream d_Stream = new DeflateStream(fs, CompressionMode.Decompress);
StreamToFile(d_Stream, @"S:\Temp\myFile1.txt", FileMode.OpenOrCreate);
d_Stream.Close();
fs.Close();
}
private static void StreamToFile(Stream inputStream, string outputFile, FileMode fileMode)
{
if (inputStream == null)
throw new ArgumentNullException("inputStream");
if (String.IsNullOrEmpty(outputFile))
throw new ArgumentException("Argument null or empty.", "outputFile");
using (FileStream outputStream = new FileStream(outputFile, fileMode, FileAccess.Write))
{
int cnt = 0;
const int LEN = 4096;
byte[] buffer = new byte[LEN];
while ((cnt = inputStream.Read(buffer, 0, LEN)) != 0)
outputStream.Write(buffer, 0, cnt);
}
}
有没有人有一些想法?
谢谢
Does anyone has some ideas? Thanks.
推荐答案
我添加了这个测试数据: -
I added this for test data:-
private static void Compress()
{
FileStream fs = new FileStream(@"C:\Temp\myFile.bin", FileMode.Create);
DeflateStream d_Stream = new DeflateStream(fs, CompressionMode.Compress);
for (byte n = 0; n < 255; n++)
d_Stream.WriteByte(n);
d_Stream.Close();
fs.Close();
}
修改解压这样的: -
Modified Decompress like this:-
private static void Decompress()
{
FileStream fs = new FileStream(@"C:\Temp\myFile.bin", FileMode.Open);
//First two bytes are irrelevant
// fs.ReadByte();
// fs.ReadByte();
DeflateStream d_Stream = new DeflateStream(fs, CompressionMode.Decompress);
StreamToFile(d_Stream, @"C:\Temp\myFile1.txt", FileMode.OpenOrCreate);
d_Stream.Close();
fs.Close();
}
然这样的: -
Ran it like this:-
static void Main(string[] args)
{
Compress();
Decompress();
}
和有没有错误。
我的结论,无论是前两个字节是相关的(显然,他们是我的特别的测试数据。),或者你的数据有问题
I conclude that either the first two bytes are relevant (Obviously they are with my particular test data.) or that your data has a problem.
我们能有你的一些测试数据一起玩?
Can we have some of your test data to play with?
(显然不如果是大小写)
(Obviously don't if it's sensitive)
这篇关于C#解码(解压缩)放气PDF文件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!