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();
    }
}
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • 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 '11 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 '11 at 18:15

2 Answers2

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...

Robert
  • 39,162
  • 17
  • 99
  • 152
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.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • 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... :( – Davide Piras Oct 14 '11 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 '11 at 11:12