"Go" C # Packaging folder into zip format

Source: Internet
Author: User
Tags crc32 zip extension

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.