|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Send files to Compressed (zipped) folderI 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, 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."); } -- Show quoteWilliam Stacey [MVP] "Amelyan" <bamelyan at wi.rr.com> wrote in message news:eQu$1QgQGHA.1416@TK2MSFTNGP12.phx.gbl... |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, | | | 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, Show quote "William Stacey [MVP]" <william.sta***@gmail.com> wrote in message news:ei%23Lc5mQGHA.1160@TK2MSFTNGP09.phx.gbl... > 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 > news:eQu$1QgQGHA.1416@TK2MSFTNGP12.phx.gbl... > |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, > | > | > | > > |
|||||||||||||||||||||||