Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6069057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:46:53+00:00 2026-05-23T09:46:53+00:00

I’m trying to decompress a stream, using a GZipStream and BinaryStream, but I’m failing.

  • 0

I’m trying to decompress a stream, using a GZipStream and BinaryStream, but I’m failing.

Can you help me?

    public static LicenseOwnerRoot GetLicenseFromStream(Stream stream)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            string keyCrypto = br.ReadString();
            string xmlCrypto = br.ReadString();
            string key = Cryptography.Decrypt(keyCrypto);
            string xml = Cryptography.Decrypt(key, xmlCrypto);
            byte[] data = Encoding.UTF8.GetBytes(xml.ToCharArray());

            using (MemoryStream ms = new MemoryStream(data))
            {
                using (GZipStream decompress = new GZipStream(ms, CompressionMode.Decompress))
                {
                    xml = Encoding.UTF8.GetString(data);
                    LicenseOwnerRoot root = (LicenseOwnerRoot)Utility.XmlDeserialization(typeof(LicenseOwnerRoot), xml);
                    foreach (LicenseOwnerItem loi in root.Licenses)
                        loi.Root = root;
                    return root;
                }
            }
        }
    }

That xml is compressed and encrypted, so I have to decompress and then decrypt. When I try to read, throws one expections with this message: The magic number in GZip header is not correct. I tried so many times to fix that, but It’s sounds workable.
The question is: how I should use the ‘usings’ and if that way is right, or exists another way to do what I’m trying to do?
I have to decompress before to use BinaryReader?

Actually, I have to do the inverse of this method:

    public static void GenerateLicenseStream(string key, LicenseOwnerRoot root, Stream stream)
    {
        using (BinaryWriter sw = new BinaryWriter(stream))
        {
            string xml = Utility.XmlSerialization(root);
            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream compress = new GZipStream(ms, CompressionMode.Compress))
                {
                    byte[] data = Encoding.UTF8.GetBytes(xml.ToCharArray());
                    compress.Write(data, 0, data.Length);
                    string keyCrypto = Cryptography.Encrypt(key);
                    string xmlCrypto = Cryptography.Encrypt(key, Encoding.UTF8.GetString(ms.ToArray()));
                    sw.Write(keyCrypto);
                    sw.Write(xmlCrypto);
                }
            }
        }
   } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:46:53+00:00Added an answer on May 23, 2026 at 9:46 am

    I wrote up a quick sample for you, it doesnt do the encryption but it highlights where encryption / decryption should occur. This is the content of a .NET Console app that you can run “as is”:

        static void Main(string[] args)
        {
            var content = @"someTextOrXMLContentGoesHereCanBeAnything#$&%&*(@#$^";
            var data = Encoding.UTF8.GetBytes(content.ToCharArray());
            var fs = new StreamWriter(@"c:\users\stackoverflow\desktop\sample.bin");
            using (var bw = new BinaryWriter(fs.BaseStream))
            {
                using (var ms = new MemoryStream())
                {
                    using (var compress = new GZipStream(ms, CompressionMode.Compress, true))
                    {
                        compress.Write(data, 0, data.Length);
                    }
                    // encrypt goes here
                    var compressedData = ms.ToArray();
                    Console.WriteLine(compressedData.Length); // 179
                    bw.Write(compressedData);
                }
            }
            // and the reverse...
            using (var fs2 = new StreamReader(@"c:\users\stackoverflow\desktop\sample.bin"))
            {
                using (var br = new BinaryReader(fs2.BaseStream))
                {
                    var len = (int)br.BaseStream.Length;
                    var encrypted = br.ReadBytes(len);
                    // decrypt here
                    var decrypted = encrypted; // <== new result after decryption
                    using (var ms = new MemoryStream(decrypted))
                    {
                        List<byte> bytesList = new List<byte>();
                        using (var decompress = new GZipStream(ms, CompressionMode.Decompress, true))
                        {
                            int val = decompress.ReadByte();
                            while (val > -1)
                            {
                                bytesList.Add((byte)val);
                                val = decompress.ReadByte();
                            }  
                        }
                        var final_result = new String(Encoding.UTF8.GetChars(bytesList.ToArray()));
                        Console.WriteLine(final_result);
                    }
                }
            }
    
            Console.ReadLine();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.