Compressing a folder in c# ??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eternal
    New Member
    • Dec 2008
    • 4

    #1

    Compressing a folder in c# ??

    Hello All,

    I am trying to compress a folder with different kinds of files and sub-folder programmaticall y. Googleing all the c# forums and news groups
    (almost for half a day ) lead me to ICSharpCode SharpZipLib project. But I dont want to use this and I am trying to find out any other way to do this. Can anyone shed some light on this. I thought it must be easy to compress a folder in c#. Thank you.
  • SvenV
    New Member
    • Oct 2008
    • 50

    #2
    .NET framework has two classes that you can use for this
    DeflateStream class (without CRC): DeflateStream Class (System.IO.Comp ression)
    GZipStream (with CRC): GZipStream Class (System.IO.Comp ression)

    Comment

    • Eternal
      New Member
      • Dec 2008
      • 4

      #3
      Thanks SvenV. Can you give me some example for compressing a folder.

      Comment

      • nukefusion
        Recognized Expert New Member
        • Mar 2008
        • 221

        #4
        Originally posted by Eternal
        Thanks SvenV. Can you give me some example for compressing a folder.
        Did you follow the links that SvenV provided? There is sample code there for compressing files including a link to a full blown application sample, also with source code that is capable of compressing multiple files.

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          Originally posted by Eternal
          Hello All,

          I am trying to compress a folder with different kinds of files and sub-folder programmaticall y. Googleing all the c# forums and news groups
          (almost for half a day ) lead me to ICSharpCode SharpZipLib project. But I dont want to use this and I am trying to find out any other way to do this. Can anyone shed some light on this. I thought it must be easy to compress a folder in c#. Thank you.
          You could also use WMI to do so... Win32_Directory class
          System.Manageme nt.
          Code:
          string filePath = @"C:\Zp1\Zp";
                      try
                      {
                          ManagementObject dir =
                              new ManagementObject("root\\CIMV2",  "Win32_Directory.Name='"+filePath+"'", null);
          ManagementBaseObject op = dir.InvokeMethod("Compress", null, null);
          
          Console.WriteLine(op["ReturnValue"].Tostring);
          //0 for success
          Win32_Directory

          Comment

          • vekipeki
            Recognized Expert New Member
            • Nov 2007
            • 229

            #6
            There is a good open-source library which offers better compression compared to System.IO.Compr ession classes:

            .NET Zip Library #ziplib (SharpZipLib)

            There are several compression types (Zip, GZip, BZip2, Tar), with adjustable compression settings.

            Comment

            • Eternal
              New Member
              • Dec 2008
              • 4

              #7
              Thank you all for the help.

              Comment

              Working...