GZipStream help RRS feed

  • Question

  • User1119594843 posted


    I want to create a .zip extension using GZipStream class. I had done with single file using GZipStream, but i want to add more files inside the single zip file.

    Please, i don't to use any 3rd party dll's.

    thank you Smile

    Tuesday, September 27, 2011 5:34 AM

All replies

  • Tuesday, September 27, 2011 5:46 AM
  • User1119594843 posted

    sorry this will create a different zip files right. This is the one i already saw, but scenario is i want to compress all files into one single zip file.

    for eg:

    i have 10 files(.jpg, .doc, .png) in a folder.

    now i want to create compression of all 10 files into backup.2011.zip

    can you help me

    Tuesday, September 27, 2011 6:44 AM
  • User377791177 posted

    this tweaked version worked for me. it has compression only...

    using System;
    using System.IO;
    using System.IO.Compression;
    using System.Linq;
    namespace zip
        public class Program
            public static void Main()
                // Path to directory of files to compress and decompress.
                string dirpath = @"D:\TESTS";
                DirectoryInfo di = new DirectoryInfo(dirpath);
                using (FileStream outFile = File.Create("Compressed.gz"))
                    using (GZipStream Compress =
                                    new GZipStream(outFile,
                        di.GetFiles("*", SearchOption.TopDirectoryOnly).ToList().ForEach(fi =>
                            using (FileStream inFile = fi.OpenRead())
                                // Prevent compressing hidden and 
                                // already compressed files.
                                if ((File.GetAttributes(fi.FullName)
                                    & FileAttributes.Hidden)
                                    != FileAttributes.Hidden & fi.Extension != ".gz")
                                    // Create the compressed file.
                                        Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                                            fi.Name, fi.Length.ToString(), outFile.Length.ToString());
                                       // outFile.Close();

    Tuesday, September 27, 2011 7:58 AM
  • User1119594843 posted


    sorry i didn't mention my version, the code which you have send is i think 4.0 but i'm working on 3.5



    Tuesday, September 27, 2011 8:18 AM
  • Tuesday, September 27, 2011 8:36 AM
  • User1119594843 posted

    one more issue i got Frown

    my zip filename is compressed.zip

    when i open in winrar inside the zip only one file called compressed without extension, but i have 3 files in my collection.

    my requirement is: I want to create a handler where i will pass 'n' no. of files to download instead of downloading one by one i want to compress to one file.

    asp.net (3.5)

    Tuesday, September 27, 2011 8:55 AM