By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,067 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

Compressing a folder in c# ??

P: 4
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.
Dec 30 '08 #1
Share this Question
Share on Google+
6 Replies


P: 50
.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)
Dec 30 '08 #2

P: 4
Thanks SvenV. Can you give me some example for compressing a folder.
Dec 30 '08 #3

nukefusion
Expert 100+
P: 221
@Eternal
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.
Dec 30 '08 #4

Expert 100+
P: 750
PRR
@Eternal
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
Dec 30 '08 #5

vekipeki
Expert 100+
P: 229
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.
Dec 31 '08 #6

P: 4
Thank you all for the help.
Dec 31 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.