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 6132261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:04:49+00:00 2026-05-23T17:04:49+00:00

I have a test program that demonstrates the end result that I am hoping

  • 0

I have a test program that demonstrates the end result that I am hoping for (even though in this test program the steps may seem unnecessary).

The program compresses data to a file using GZipStream. The resulting compressed file is C:\mydata.dat.

I then read this file, and write it to a new file.

//Read original file
string compressedFile = String.Empty;
using (StreamReader reader = new StreamReader(@"C:\mydata.dat"))
{
    compressedFile = reader.ReadToEnd();
    reader.Close();
    reader.Dispose();
}

//Write to a new file
using (StreamWriter file = new StreamWriter(@"C:\mynewdata.dat"))
{
    file.WriteLine(compressedUserFile);
}

When I try to decompress the two files, the original one decompresses perfectly, but the new file throws an InvalidDataException with message The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

Why are these files different?

  • 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-23T17:04:49+00:00Added an answer on May 23, 2026 at 5:04 pm

    EDIT: Apparently, my suggestions are wrong/invalid/whatever… please use one of the others which have no doubt been highly re-factored to the point where no extra performance could be possible be achieved (else, that would mean they are just as invalid as mine)

    using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\mydata.dat"))
    {
        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\mynewdata.dat"))
        {
            byte[] bytes = new byte[1024];
            int count = 0;
            while((count = sr.BaseStream.Read(bytes, 0, bytes.Length)) > 0){
                sw.BaseStream.Write(bytes, 0, count);
            }
        }
    }
    

    Read all bytes

    byte[] bytes = null;
    using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\mydata.dat"))
    {
        bytes = new byte[sr.BaseStream.Length];
        int index = 0;
        int count = 0;
        while((count = sr.BaseStream.Read(bytes, index, 1024)) > 0){
            index += count;
        }
    }
    

    Read all bytes/write all bytes (from svick’s answer):

    byte[] bytes = File.ReadAllBytes(@"C:\mydata.dat");
    File.WriteAllBytes(@"C:\mynewdata.dat", bytes);
    

    PERFORMANCE TESTING WITH OTHER ANSWERS:

    Just did a quick test between my Answer (StreamReader) (first part above, file copy) and svick’s answer (FileStream/MemoryStream) (the first one). The test is 1000 iterations of the code, here are the results from 4 tests (results are in whole seconds, all actual result where slightly over these values):

    My Code | svick code
    --------------------
    9       | 12
    9       | 14
    8       | 13
    8       | 14
    

    As you can see, in my test at least, my code performed better. One thing perhaps to note with mine is I am not reading a character stream, I am in fact accessing the BaseStream which is providing a byte stream. Perhaps svick’s answer is slow because he is using two streams for reading, then two for writing. Of course, there is a lot of optimisation that could be done to svick’s answer to improve the performance (and he also provided an alternative for simple file copy)

    Testing with third option (ReadAllBytes/WriteAllBytes)

    My Code | svick code | 3rd
    ----------------------------
    8       | 14         | 7
    9       | 18         | 9
    9       | 17         | 8
    9       | 17         | 9
    

    Note: in milliseconds the 3rd option was always better

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

Sidebar

Related Questions

This is a test program that I have written for a larger project that
I have an automated test running on my program that generates some large MPG
I wanted to write a program that test if two files are duplicates (have
I have a program that uses JAMA and need to test is a matrix
I have a network C++ program in Windows that I'd like to test for
In my C# program, I have a thread that represents a running test, which
Lets say I have hierarchy like this (This is just a test program. Please
I have a test driver program that launches a separate test server process. The
I have a small test program that creates millions of files (for testing purposes)
I have a strange build problem. I have a simple test program that sends

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.