25

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.

Kuriyama Mirai
  • 867
  • 5
  • 17
  • 37
  • 1
    The documentation contains an example: http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx – nos Jun 10 '14 at 10:16
  • It unzip the file yet it didn't include the file extension – Kuriyama Mirai Jun 10 '14 at 10:21
  • 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. – nos Jun 10 '14 at 10:22
  • My .gz contains excel file. but when i unzip it. it only returns file with no extension. – Kuriyama Mirai Jun 10 '14 at 10:32
  • ex. i have file.gz that contains file.xlxs. When i unzip it, it only shows file with no extension name – Kuriyama Mirai Jun 10 '14 at 10:33
  • 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... – MRebai Jun 10 '14 at 10:46
  • @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. – nos Jun 10 '14 at 11:01
  • 1
    @Kuriyama Mirai did you get it or not – MRebai Jun 10 '14 at 11:24
  • Possible duplicate of [Unzipping a .gz file using C#](http://stackoverflow.com/questions/1348198/unzipping-a-gz-file-using-c-sharp) – psubsee2003 Jan 06 '17 at 15:39

3 Answers3

50

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);
                    }
                }
            }
        }
    }
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
MRebai
  • 5,344
  • 3
  • 33
  • 52
  • 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 Jun 10 '14 at 10:36
  • 1
    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... – MRebai Jun 10 '14 at 10:44
  • @KuriyamaMirai look at the line that sets `newFileName` – Steven Liekens Jun 10 '14 at 10:46
  • I guess it will work when you add the .gz extension at the end of yout file name did u try it – MRebai Jun 10 '14 at 10:51
  • Now I know. I'll try it out. Thank you so much. That answers my question. – Kuriyama Mirai Jun 11 '14 at 01:28
10

.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);
                }
            }
        }
    }
Matthew Trout
  • 709
  • 5
  • 20
  • 1
    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 Jun 10 '14 at 10:35
-2

The following link shows two example for zip and unzip to the files in C#. You can use this sample.

Sample(using 7-zip):

var tmp = new SevenZipCompressor();
tmp.ScanOnlyWritable = true;
tmp.CompressFilesEncrypted(outputFilePath, password, filePaths);

Sample(using ZipArchive):

ZipArchive zip = ZipFile.Open(filePath, ZipArchiveMode.Create);
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
zip.Dispose();

For more information:

http://csharpexamples.com/zip-and-unzip-files-programmatically-in-c/

turgay
  • 438
  • 4
  • 7