11

I am using following code to zip a file and it works fine but when I decompress with WinRar I get the original file name without the extension, any clue why if filename is myReport.xls when I decompress I get only myReport ?

using (var fs = new FileStream(fileName, FileMode.Open))
{
    byte[] input = new byte[fs.Length];
    fs.Read(input, 0, input.Length);
    fs.Close();

    using (var fsOutput = new FileStream(zipName, FileMode.Create, FileAccess.Write))
    using(var zip = new GZipStream(fsOutput, CompressionMode.Compress))
    {
        zip.Write(input, 0, input.Length);
        zip.Close();
        fsOutput.Close();
    }
}
2
  • 1
    AFAIK GZip has no idea about files or folders... OTOH ZIP is very different from that since it has all this including file attributes etc. built-in... perhaps you want to use a Zip library ?
    – Yahia
    Oct 14, 2011 at 11:05
  • IOW, GZip is a compression format, whereas ZIP is a compressed archive format. (TAR would be an example of an archive format.)
    – Marc L.
    Oct 14, 2011 at 18:15

2 Answers 2

29

GZip compresses only one file - without knowing the name. Therefore if you compress the file myReport.xls you should name it myReport.xls.gz. On decompression the last file extension will be removed so you end up with the original filename.

That its the way how it is used in Unix/Linux for ages...

1
  • 1
    Thanks, this was the issue, now solved by simply appending the extension instead of replacing it with .zip. Oct 14, 2011 at 11:35
2

Very weird indeed. A brief search came up with the following:

http://dotnetzip.codeplex.com/discussions/268293

Which says that GZipStream has no way of knowing the name of the stream that is being written, and suggests you set the FileName property directly.

Hope that helps.

2
  • no it does not work because the zip object has no Name or FileName property to assign. Tried with .NET 3.5 and .NET 4 but no differences... :( Oct 14, 2011 at 11:09
  • @Davide Piras But if you use DotNetZip it has a method AddFile - this takes the filename and stores it... GZip is something different from ZIP although the names are rather similar...
    – Yahia
    Oct 14, 2011 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.