Mail Folder in C #

What is an example (simple code) how to fasten a folder in C #?




Update:

I do not see the ICSharpCode namespace. I downloaded ICSharpCode.SharpZipLib.dll , but I do not know where to copy this dll file. What do I need to do to see this namespace?

And you have a link to this example MSDN for the compression folder, because I read all the MSDN, but did not find anything.




OK, but I need the following information.

Where can I copy ICSharpCode.SharpZipLib.dll to see this namespace in Visual Studio?

+46
c # folder compression zip archive
May 25 '09 at 7:00 a.m.
source share
9 answers

This answer is modified using .NET 4.5. Creating a zip file is incredibly easy . No third-party libraries are required.

 string startPath = @"c:\example\start"; string zipPath = @"c:\example\result.zip"; string extractPath = @"c:\example\extract"; ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath); 
+56
Jul 30 2018-12-14T00:
source share

In the DotNetZip help file http://dotnetzip.codeplex.com/releases/

 using (ZipFile zip = new ZipFile()) { zip.UseUnicodeAsNecessary= true; // utf-8 zip.AddDirectory(@"MyDocuments\ProjectX"); zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G") ; zip.Save(pathToSaveZipFile); } 
+43
May 25 '09 at 7:29 a.m.
source share

Nothing is done for you in BCL, but for .NET there are two large libraries that support functionality.

I used both options and I can say that they are very complete and have well-developed APIs, so this is mainly a matter of personal preference.

I'm not sure that they explicitly support the addition of Folders, not just individual files to zip files, but to create quite something that recursively repeats through a directory and its subdirectories using DirectoryInfo and FileInfo .

+20
May 25 '09 at 7:03 a.m.
source share

The System.IO.Packaging namespace has the ZipPackage class, which is built into .NET 3, 3.5, and 4.0.

http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx

Here is a usage example. http://www.codeproject.com/KB/files/ZipUnZipTool.aspx?display=Print

+5
Oct 21 2018-10-21
source share

In .NET 4.5, ZipFile.CreateFromDirectory (startPath, zipPath); the method does not cover the scenario in which you want to pin several files and subfolders without placing them in a folder. This is true if you want to unzip files directly to the current folder.

This code worked for me:

 public static class FileExtensions { public static IEnumerable<FileSystemInfo> AllFilesAndFolders(this DirectoryInfo dir) { foreach (var f in dir.GetFiles()) yield return f; foreach (var d in dir.GetDirectories()) { yield return d; foreach (var o in AllFilesAndFolders(d)) yield return o; } } } void Test() { DirectoryInfo from = new DirectoryInfo(@"C:\Test"); using (FileStream zipToOpen = new FileStream(@"Test.zip", FileMode.Create)) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create)) { foreach (FileInfo file in from.AllFilesAndFolders().Where(o => o is FileInfo).Cast<FileInfo>()) { var relPath = file.FullName.Substring(from.FullName.Length+1); ZipArchiveEntry readmeEntry = archive.CreateEntryFromFile(file.FullName, relPath); } } } } 

Folders do not need to be "created" in the zip archive. The second entryName parameter in CreateEntryFromFile must be a relative path, and when unpacking the zip file, relative path directories will be detected and created.

+4
Feb 29 '16 at 17:53 on
source share

There is an article about MSDN here that has an example application for unpacking and unpacking files and folders exclusively in C #. I have successfully used some of these classes for a long time. The code is licensed under Microsoft Permissive if you need to know something like this.

EDIT: Thanks to Cheeso for keeping me a little behind. The MSDN example that I pointed to actually uses DotNetZip and is really very full-featured these days. Based on my experience with the previous version of this, I would happily recommend it.

SharpZipLib is also a fairly mature library and is highly regarded by people, and is available under the GPL license. It really depends on your zipping needs and how you view the license terms for each of them.

Rich

+3
May 25 '09 at 7:24 a.m.
source share

"Where should I copy ICSharpCode.SharpZipLib.dll to see that namespace in Visual Studio?"

You need to add the dll file as a link in your project. Right-click the Links link in Solution Explorer → Add Link → Browse, and then select dll.

Finally, you will need to add it as a using statement to any files that you want to use it in.

0
Mar 25
source share

The following code uses a third - party ZIP component from Rebex :

 // add content of the local directory C:\Data\ // to the root directory in the ZIP archive // (ZIP archive C:\archive.zip doesn't have to exist) Rebex.IO.Compression.ZipArchive.Add(@"C:\archive.zip", @"C:\Data\*", ""); 

Or if you want to add more folders without having to open and close the archive several times:

 using Rebex.IO.Compression; ... // open the ZIP archive from an existing file ZipArchive zip = new ZipArchive(@"C:\archive.zip", ArchiveOpenMode.OpenOrCreate); // add first folder zip.Add(@"c:\first\folder\*","\first\folder"); // add second folder zip.Add(@"c:\second\folder\*","\second\folder"); // close the archive zip.Close(ArchiveSaveAction.Auto); 

You can download the mail component here .

Using the free LGPL SharpZipLib license is a common alternative.

Disclaimer: I work for Rebex

0
Jul 15 '10 at 10:52
source share

ComponentPro ZIP can help you achieve this goal. The following code snippet compresses files and directories in a folder. You can also use the wilcard mask.

 using ComponentPro.Compression; using ComponentPro.IO; ... // Create a new instance. Zip zip = new Zip(); // Create a new zip file. zip.Create("test.zip"); zip.Add(@"D:\Temp\Abc"); // Add entire D:\Temp\Abc folder to the archive. // Add all files and subdirectories from 'c:\test' to the archive. zip.AddFiles(@"c:\test"); // Add all files and subdirectories from 'c:\my folder' to the archive. zip.AddFiles(@"c:\my folder", ""); // Add all files and subdirectories from 'c:\my folder' to '22' folder within the archive. zip.AddFiles(@"c:\my folder2", "22"); // Add all .dat files from 'c:\my folder' to '22' folder within the archive. zip.AddFiles(@"c:\my folder2", "22", "*.dat"); // Or simply use this to add all .dat files from 'c:\my folder' to '22' folder within the archive. zip.AddFiles(@"c:\my folder2\*.dat", "22"); // Add *.dat and *.exe files from 'c:\my folder' to '22' folder within the archive. zip.AddFiles(@"c:\my folder2\*.dat;*.exe", "22"); TransferOptions opt = new TransferOptions(); // Donot add empty directories. opt.CreateEmptyDirectories = false; zip.AddFiles(@"c:\abc", "/", opt); // Close the zip file. zip.Close(); 

http://www.componentpro.com/doc/zip has more examples

0
Dec 18 '14 at 17:21
source share



All Articles