none
GZipStream Failed to Unzip Gzip File, Is it a BUG? RRS feed

  • Question

  • Hi, I have a Gzip file, which can be unzip by 7zip. But when I try to unzip it by C# GZipStream, it failed. 

    GZipStream can only extract frist 11 bytes, it's "OutputWindows" property is 11. 

    And I've tried SharpZipLib and DotNetZip, and got the same result.

    Anyone know the reason? Is it a GZipStream Bug? And how can i unzip the file?

    Here's my code:

            public void Decompress(string strPath)
            {
                DateTime current;
                string dstFile = "";
    
                FileStream fsIn = null;
                FileStream fsOut = null;
                GZipStream gzip = null;
                const int bufferSize = 4096;
                byte[] buffer = new byte[bufferSize];
                int count = 0;
    
                try
                {
                    current = DateTime.Now;
                    dstFile = "z:\\" + current.Day.ToString() + current.Month.ToString() + current.Year.ToString() + current.Hour.ToString() + current.Minute.ToString() + current.Second.ToString() + ".txt";
                    fsIn = new FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
                    gzip = new GZipStream(fsIn, CompressionMode.Decompress, true);
                    while (true)
                    {
                        count = gzip.Read(buffer, 0, bufferSize);
                        if (count != 0)
                        {
                            fsOut.Write(buffer, 0, count);
                        }
                        if (count != bufferSize)
                        {
                            // have reached the end
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    // handle or display the error 
                    System.Diagnostics.Debug.Assert(false, ex.ToString());
                }
                finally
                {
                    if (gzip != null)
                    {
                        gzip.Close();
                        gzip = null;
                    }
                    if (fsOut != null)
                    {
                        fsOut.Close();
                        fsOut = null;
                    }
                    if (fsIn != null)
                    {
                        fsIn.Close();
                        fsIn = null;
                    }
                }
            } 
    

    Here's my Gzip file:

    • Moved by Bob Beauchemin Friday, July 26, 2013 5:13 AM Moved to a more relevent forum
    Friday, July 26, 2013 2:29 AM

Answers

  • Hi Carly,

    The following example shows how to use the GZipStream class to compress and decompress a directory of files.

    using System;
    using System.IO;
    using System.IO.Compression;
    namespace zip
    {
        public class Program
        {
            public static void Main()
            {
                string directoryPath = @"c:\users\public\reports";
                DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
                foreach (FileInfo fileToCompress in directorySelected.GetFiles())
                {
                    Compress(fileToCompress);
                }
                foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
                {
                    Decompress(fileToDecompress);
                }
            }
            public static void Compress(FileInfo fileToCompress)
            {
                using (FileStream originalFileStream = fileToCompress.OpenRead())
                {
                    if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                    {
                        using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                        {
                            using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                            {
                                originalFileStream.CopyTo(compressionStream);
                                Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                                    fileToCompress.Name, fileToCompress.Length.ToString(), compressedFileStream.Length.ToString());
                            }
                        }
                    }
                }
            }
            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);
                        }
                    }
                }
            }
        }
    }

    By the way,you can check samples from http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx.

    Have a nice day!

    buernuo


    ALL FOR ONE AND ONE FOR ALL.

    Friday, July 26, 2013 8:02 AM
  • Also try this modified loop:

    while (true)
    {
        count = gzip.Read(buffer, 0, bufferSize);
        if (count == 0) break;
        fsOut.Write(buffer, 0, count);
    }

    Saturday, July 27, 2013 7:25 AM

All replies

  • Hi Carly,

    The following example shows how to use the GZipStream class to compress and decompress a directory of files.

    using System;
    using System.IO;
    using System.IO.Compression;
    namespace zip
    {
        public class Program
        {
            public static void Main()
            {
                string directoryPath = @"c:\users\public\reports";
                DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
                foreach (FileInfo fileToCompress in directorySelected.GetFiles())
                {
                    Compress(fileToCompress);
                }
                foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
                {
                    Decompress(fileToDecompress);
                }
            }
            public static void Compress(FileInfo fileToCompress)
            {
                using (FileStream originalFileStream = fileToCompress.OpenRead())
                {
                    if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                    {
                        using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                        {
                            using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                            {
                                originalFileStream.CopyTo(compressionStream);
                                Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                                    fileToCompress.Name, fileToCompress.Length.ToString(), compressedFileStream.Length.ToString());
                            }
                        }
                    }
                }
            }
            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);
                        }
                    }
                }
            }
        }
    }

    By the way,you can check samples from http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx.

    Have a nice day!

    buernuo


    ALL FOR ONE AND ONE FOR ALL.

    Friday, July 26, 2013 8:02 AM
  • Also try this modified loop:

    while (true)
    {
        count = gzip.Read(buffer, 0, bufferSize);
        if (count == 0) break;
        fsOut.Write(buffer, 0, count);
    }

    Saturday, July 27, 2013 7:25 AM