Original Address C # Package folder into ZIP format (including all files under Folders and subfolders)
C # packaged Zip files can call out-of-the-box third-party DLLs, with less effort, and the DLL is completely free: sharpziplib
After downloading the extract, copy the ICSharpCode.SharpZipLib.dll to the current project directory (if lazy, you can copy directly to the current project
Bin\Debug directory), right-click on the VS Open project reference to add a reference ICSharpCode.SharpZipLib.dll
Then, right-create a new class on the VS Open project, name ZipHelper.cs, empty all code inside the class, copy the following code, paste:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;
Using System.Diagnostics;
Using Icsharpcode.sharpziplib;
Using ICSharpCode.SharpZipLib.Zip;
Using ICSharpCode.SharpZipLib.Checksums;
Using ICSharpCode.SharpZipLib.Core;
Namespace Ziponecode.zipprovider
{
public class Ziphelper
{
<summary>
Compress files
</summary>
<param name= "Sourcefilepath" ></param>
<param name= "Destinationzipfilepath" ></param>
public static void Createzip (String sourcefilepath, String destinationzipfilepath)
{
if (sourcefilepath[sourcefilepath.length-1]! = System.IO.Path.DirectorySeparatorChar)
Sourcefilepath + = System.IO.Path.DirectorySeparatorChar;
Zipoutputstream ZipStream = new Zipoutputstream (File.create (Destinationzipfilepath));
Zipstream.setlevel (6); Compression level 0-9
Createzipfiles (Sourcefilepath, ZipStream);
Zipstream.finish ();
Zipstream.close ();
}
<summary>
Recursive compressed files
</summary>
<param name= "Sourcefilepath" > file or folder path to be compressed </param>
<param name= "ZipStream" > Package result zip file path (similar to D:\WorkSpace\a.zip), full path including file name and. zip extension
</param>
<param name= "Staticfile" ></param>
private static void Createzipfiles (String sourcefilepath, Zipoutputstream zipStream)
{
Crc32 CRC = New Crc32 ();
string[] Filesarray = directory.getfilesystementries (Sourcefilepath);
foreach (string file in Filesarray)
{
if (directory.exists (file))//If the folder is currently a recursive
{
Createzipfiles (file, zipStream);
}
else//If it is a file, start compressing
{
FileStream FileStream = File.openread (File);
byte[] buffer = new Byte[filestream.length];
FileStream.Read (buffer, 0, buffer.) Length);
String tempfile = file. Substring (sourcefilepath.lastindexof ("\ \") + 1);
ZipEntry entry = new ZipEntry (tempfile);
Entry. DateTime = DateTime.Now;
Entry. Size = Filestream.length;
Filestream.close ();
Crc. Reset ();
Crc. Update (buffer);
Entry. CRC = CRC. Value;
Zipstream.putnextentry (entry);
Zipstream.write (buffer, 0, buffer.) Length);
}
}
}
}
}
Using the method, after referencing using ziponecode.zipprovider externally, similar to calling Ziphelper.createzip (@ "D:\Temp\forzip", @ "D:\TEMP2
\forzip.zip ").
"Go" C # Packaging folder into zip format