Unzip .gz file using c#

29,196

Solution 1

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

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);
                    }
                }
            }
        }
    }
}

Solution 2

.Net has GZipStream

The example listed in the API...

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);
                }
            }
        }
    }
Share:
29,196

Related videos on Youtube

Kuriyama Mirai
Author by

Kuriyama Mirai

Junior Software developer

Updated on July 09, 2022

Comments

  • Kuriyama Mirai
    Kuriyama Mirai almost 2 years

    How to unzip .gz file and save files in a specific folder using c#?

    This is the first time I encounter a .gz file. I've search in how to unzip it yet It didn't work for me. It didn't unzip .gz file in a specific folder. I don't want to used any third party application.

    Can anyone gave me a sample code on how to unzip it. Then save file in a folder. Thanks.

    • nos
      nos almost 10 years
      The documentation contains an example: msdn.microsoft.com/en-us/library/…
    • Kuriyama Mirai
      Kuriyama Mirai almost 10 years
      It unzip the file yet it didn't include the file extension
    • nos
      nos almost 10 years
      Which file extension are you talking about ? The example program both compresses and decompreses files in a directory, surely you can adapt to what you need to do.
    • Kuriyama Mirai
      Kuriyama Mirai almost 10 years
      My .gz contains excel file. but when i unzip it. it only returns file with no extension.
    • Kuriyama Mirai
      Kuriyama Mirai almost 10 years
      ex. i have file.gz that contains file.xlxs. When i unzip it, it only shows file with no extension name
    • Moez Rebai
      Moez Rebai almost 10 years
      GZip only compresses one file - without knowing the name. Therefore if you compress the file file.xlxs you should name it file.xlxs.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...
    • nos
      nos almost 10 years
      @KuriyamaMirai And you unzipped this using what ? The code in the documentation I posted ? In any case, read the comment of moez. The .gz extension is just removed from the file name. It's up to you, the programmer/user to preserve the original filename.
    • Moez Rebai
      Moez Rebai almost 10 years
      @Kuriyama Mirai did you get it or not
    • psubsee2003
      psubsee2003 over 7 years
      Possible duplicate of Unzipping a .gz file using C#
  • Kuriyama Mirai
    Kuriyama Mirai almost 10 years
    I've used this code before yet it didn't work for me. I dont know why but It didn't unzip my gz file. It only remove the gz extension name returning file with no extension.
  • Kuriyama Mirai
    Kuriyama Mirai almost 10 years
    I've used this code before yet it didn't work for me. I dont know why but It didn't unzip my gz file. It only remove the gz extension name returning file with no extension.
  • Moez Rebai
    Moez Rebai almost 10 years
    GZip only compresses 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...
  • Steven Liekens
    Steven Liekens almost 10 years
    @KuriyamaMirai look at the line that sets newFileName
  • Moez Rebai
    Moez Rebai almost 10 years
    I guess it will work when you add the .gz extension at the end of yout file name did u try it
  • Kuriyama Mirai
    Kuriyama Mirai almost 10 years
    Now I know. I'll try it out. Thank you so much. That answers my question.
  • NetMage
    NetMage almost 5 years
    What does this have to do with .gz files?