0

I have a file in a current directory, which name is firstName. I want to create an archive with this file (name of file have to stay). The archive name have to be an archiveName. But when I use my code I get an archive with the rigth name but the name of file in it was changed. How can I fix it?

    public static void Compress(string firstName, string secondName)
    {
        FileInfo fileToCompress = new FileInfo(firstName);
        using (FileStream originalFileStream = fileToCompress.OpenRead())
        {
            if ((File.GetAttributes(fileToCompress.FullName) &
                FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
            {
                using (FileStream compressedFileStream = File.Create(secondName))
                {
                    using (GZipStream compressionStream = new GZipStream(compressedFileStream,
                        CompressionMode.Compress))
                    {
                        originalFileStream.CopyTo(compressionStream);

                    }
                }
                FileInfo info = new FileInfo(secondName);
                Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                fileToCompress.Name, fileToCompress.Length.ToString(), info.Length.ToString());
            }
        }
    }
Beras Mark
  • 53
  • 1
  • 9

1 Answers1

2

GZipStream isn't a ZIP file; it's straight-up stream compression.
It has no notion of files or names; it just compresses bytes.

You're looking for the ZipArchive class.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964