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

  • Home
  • SEARCH
  • 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 9092311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:48:42+00:00 2026-06-16T22:48:42+00:00

I am attempting to create a Save/Load class that has the option for saving

  • 0

I am attempting to create a Save/Load class that has the option for saving & load files compressed files. Below is what I have so far. Stepping through it seems to work just fine, except that I get a “The magic number in GZip header is not correct” exception. I don’t understand how this can be as I am checking to make sure that the number is there before I pass it over, and I have verified via an external program that it is a GZip file.

Any assistance in finding out where I went wrong would be appreciated. Constructive criticism of my code is always welcome – Thanks!

public static class SaveLoad
{
    public static void Save(string fileName, object savefrom, bool compress)
    {
        FileStream stream = new FileStream(fileName, FileMode.Create);

        BinaryFormatter formatter = new BinaryFormatter();
        if (compress)
        {
            GZipStream compressor = new GZipStream(stream, CompressionMode.Compress);
            formatter.Serialize(compressor, savefrom);
            compressor.Close();
        }
        else { formatter.Serialize(stream, savefrom); }

        stream.Close();
    }

    public static object Load(string fileName)
    {
        object loadedObject = null;

        try
        {
            FileStream stream = new FileStream(fileName, FileMode.Open);

            BinaryFormatter formatter = new BinaryFormatter();

            if (stream.Length > 4)
            {
                byte[] data = new byte[4];
                stream.Read(data, 0, 4);

                if (BitConverter.ToUInt16(data, 0) == 0x8b1f) //GZIP_LEAD_BYTES == 0x8b1f
                {
                    GZipStream decompressor = new GZipStream(stream, CompressionMode.Decompress);
                    loadedObject = formatter.Deserialize(decompressor); //Exception
                    decompressor.Close();
                }
                else { loadedObject = formatter.Deserialize(stream); }
            }
            stream.Close();
        }
        catch (Exception e)
        {
            Logger.StaticLog.AddEvent(new Logger.lEvent(null, Logger.lEvent.EventTypes.Warning, "Failed to load file: " + fileName, e)
            {
                SendingObject = "SaveLoad"
            });
            Logger.StaticLog.WriteLog();
            throw;
        }

        return loadedObject;
    }
}
  • 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-06-16T22:48:44+00:00Added an answer on June 16, 2026 at 10:48 pm

    It seems that you read the magic number before passing the stream to decompressor (which won’t read the magic number then, because you’ve already read it).

    Use stream.Seek(0,SeekOrigin.Begin) before you decompress.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create a single Controller class to handle all foreseeable surveys that
I am currently attempting to create a fairly basic web app that has the
I am attempting to create a report, utilizing a matrix, that only displays columns
I'm attempting to create a CustomControl which will have various properties affected by an
I am attempting to create a script that allows the user to click a
I'm attempting to create a map that allows users to report a trailwork problem
I have a google street map and I would to create a button that
If I'm attempting to create a file that takes information a user inputs using
I'm attempting to create a form inside a table that adds a new row
I am attempting to create a validation rule that passes an error if the

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.