Connecting Tech Pros Worldwide Forums | Help | Site Map

Compressing a folder in c# ??

Newbie
 
Join Date: Dec 2008
Posts: 4
#1: Dec 30 '08
Hello All,

I am trying to compress a folder with different kinds of files and sub-folder programmatically. 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.

Member
 
Join Date: Oct 2008
Posts: 50
#2: Dec 30 '08

re: Compressing a folder in c# ??


.NET framework has two classes that you can use for this
DeflateStream class (without CRC): DeflateStream Class (System.IO.Compression)
GZipStream (with CRC): GZipStream Class (System.IO.Compression)
Newbie
 
Join Date: Dec 2008
Posts: 4
#3: Dec 30 '08

re: Compressing a folder in c# ??


Thanks SvenV. Can you give me some example for compressing a folder.
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#4: Dec 30 '08

re: Compressing a folder in c# ??


Quote:

Originally Posted by Eternal View Post

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.
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 702
#5: Dec 30 '08

re: Compressing a folder in c# ??


Quote:

Originally Posted by Eternal View Post

Hello All,

I am trying to compress a folder with different kinds of files and sub-folder programmatically. 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.Management.
Expand|Select|Wrap|Line Numbers
  1. string filePath = @"C:\Zp1\Zp";
  2.             try
  3.             {
  4.                 ManagementObject dir =
  5.                     new ManagementObject("root\\CIMV2",  "Win32_Directory.Name='"+filePath+"'", null);
  6. ManagementBaseObject op = dir.InvokeMethod("Compress", null, null);
  7.  
  8. Console.WriteLine(op["ReturnValue"].Tostring);
  9. //0 for success
  10.  
  11.  
Win32_Directory
vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 231
#6: Dec 31 '08

re: Compressing a folder in c# ??


There is a good open-source library which offers better compression compared to System.IO.Compression classes:

.NET Zip Library #ziplib (SharpZipLib)

There are several compression types (Zip, GZip, BZip2, Tar), with adjustable compression settings.
Newbie
 
Join Date: Dec 2008
Posts: 4
#7: Dec 31 '08

re: Compressing a folder in c# ??


Thank you all for the help.
Reply