Click here to Skip to main content
15,888,271 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have .zip file having one file inside it in csv format. I have to use .net 4.0, so I am using GZipStream to unzip the file . But I am getting an error: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.".

Note: I have to achieve this using .net 4.0 and I cannot use third party libraries.

What I have tried:

public MainWindow()
        {
            InitializeComponent();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            if(ofd.ShowDialog().HasValue && !string.IsNullOrEmpty(ofd.FileName))
            {
                FileInfo fi = new FileInfo(ofd.FileName);
                Decompress(fi);
            }
        }

        public static void Decompress(FileInfo fileToDecompress)
        {
            using (FileStream originalFileStream = fileToDecompress.OpenRead())
            {
                string currentFileName = fileToDecompress.FullName;
                string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

                using (FileStream decompressedFileStream = File.Create(newFileName))
                {
                    using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                    {
                        decompressionStream.CopyTo(decompressedFileStream);
                        Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                    }
                }
            }
        }
Posted
Comments
RickZeeland 5-Jul-17 8:42am    
If a single executable is required, you can also use tools like ILMerge or Fody Costura to merge dll's into the exe. I did this in .NET 3.0 with the Ionic.Zip.Reduced.dll
Richard MacCutchan 5-Jul-17 9:07am    
The error message is quite clear; either the file was not created with GZip, or it has been corrupted. You should inspect it with a hex editor to check the content.
Ramana Bellary 5-Jul-17 9:14am    
I have created the .zip file manually, which has one csv file inside it. If the zip file has a .txt file, in that case am able to unzip it.
Richard MacCutchan 5-Jul-17 9:56am    
You still need to look into your file to see why you receive that error message.
Dave Kreskowiak 5-Jul-17 10:58am    
What do you mean by "you created the .zip file manually"? How, exactly? It sounds like whatever you did did not create a valid .ZIP file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900