views:

49

answers:

1

I am using C# to programatically compress an xml file. Compression works fine, but when I gunzip the file from the command line, the extension has been dropped. Why would this be?

The destination file coming in has the gz extension while the source file has an xml extension.

Here is my compression code:

            using (FileStream input = File.OpenRead(filename))
            {
                using (var raw = File.Create(destFilename))
                {
                    using (Stream gzipStream = new GZipStream(raw, CompressionMode.Compress))
                    {
                        byte[] buffer = new byte[4096];
                        int n;
                        while ((n = input.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            gzipStream.Write(buffer, 0, n);
                        }
                    }
                }
            }

This also happens when I use a 3rd party library (SharpLibZip) to compress the file.

How do I keep the extension in the compressed zip file?

+3  A: 

You should probably name your compressed files filename.xml.gz - the gz extension intentionally gets removed, and the source extension is not stored anywhere inside the archive, IIRC

corvuscorax
It is also obvious when looking at the code. They're just compressing a stream of bytes, not a file (including metadata) per se. So there is no way the final archive *can* have the file name.
Joey
This is the standard convention for GZip. It works differently than, e.g., Zip, which includes file metadata.
Stephen Cleary