C# decode (decompress) Deflate data of PDF File

 ̄綄美尐妖づ 提交于 2019-12-05 03:46:59

问题


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.


回答1:


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();
}

And got no errors.

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)




回答2:


private static string decompress(byte[] input)
{
    byte[] cutinput = new byte[input.Length - 2];
    Array.Copy(input, 2, cutinput, 0, cutinput.Length);

    var stream = new MemoryStream();

    using (var compressStream = new MemoryStream(cutinput))
    using (var decompressor = new DeflateStream(compressStream, CompressionMode.Decompress))
        decompressor.CopyTo(stream);

    return Encoding.Default.GetString(stream.ToArray());
}

Thank you user159335 and user1011394 for bringing me on the right track! Just pass all bytes of the stream to input of above function. Make sure the bytecount is the same as the length specified.



来源:https://stackoverflow.com/questions/9197678/c-sharp-decode-decompress-deflate-data-of-pdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!