Send files to Compressed (zipped) folder

A

Amelyan

I have a bunch of files I need to put into Compressed (zipped) Folder,
MyFiles.zip file, similarly to what Windows XP does when you send files to
compressed zipped folder. What is the simplest way to compress these files
into a .zip?

I found System.IO.Compression, but I am not sure how to use it to do what I
need to do. Does anyone have an example of how to send a bunch of files
into compressed (zipped) folder?

Thanks,
 
W

William Stacey [MVP]

You could do something like:



private void button21_Click_1(object sender, EventArgs e)

{

// Note: Add a ref to the Com dll 'Microsoft Shell Controls And
Automation' in your project.

// Creates a new zip container and adds directory to it.



string zipFile = @"c:\myzip.zip";

if (!System.IO.File.Exists(zipFile))

System.IO.File.Delete(zipFile);



using(FileStream fs = new FileStream(zipFile, FileMode.Create,
FileAccess.Write))

{

byte[] emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

fs.Write(emptyzip, 0, emptyzip.Length);

}



//Copy a folder and all its contents into zip file.

Shell32.ShellClass sc = new Shell32.ShellClass();

Shell32.Folder SrcFlder = sc.NameSpace(@"C:\temp\sampleapp");

Shell32.Folder DestFlder = sc.NameSpace(zipFile);

Shell32.FolderItems items = SrcFlder.Items();

DestFlder.CopyHere(items, 20);



Console.WriteLine("Done.");

}


--
William Stacey [MVP]

"Amelyan" <bamelyan at wi.rr.com> wrote in message
|I have a bunch of files I need to put into Compressed (zipped) Folder,
| MyFiles.zip file, similarly to what Windows XP does when you send files to
| compressed zipped folder. What is the simplest way to compress these
files
| into a .zip?
|
| I found System.IO.Compression, but I am not sure how to use it to do what
I
| need to do. Does anyone have an example of how to send a bunch of files
| into compressed (zipped) folder?
|
| Thanks,
|
|
|
 
A

Amelyan

Hi William,

I created a new project and added your code to main function. I also added
shell32.dll from c:\windows\System32 folder. I provided c:\temp as my
source folder that contains a bunch of files and folders.

However, when I run the program, it creates a myzip.zip that is 24bytes in
size, and when I open it, it is blank. Is there anything that I missed?

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top