Using System;
Using System.Collections.Generic;
Using System.Text;
Third-party DLLs
Using Icsharpcode.sharpziplib;
Using ICSharpCode.SharpZipLib.Checksums;
Using ICSharpCode.SharpZipLib.Zip;
Using System.IO;
Using Log4net;
Using Log4net. Config;
Using System.Text.RegularExpressions;
Namespace TEST.BLL
{
public class Testzipfile
{
protected static readonly ILog logger = Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
<summary>
How to encrypt a compressed package
</summary>
<param name= "Strfile" ></param>
<param name= "Strzip" ></param>
<param name= "Spassword" ></param>
public void ZipFile (String strfile, String strzip, String spassword)
{
if (strfile[strfile.length-1]! = Path.directoryseparatorchar)
Strfile + = Path.directoryseparatorchar;
Zipoutputstream s = new Zipoutputstream (File.create (strzip));
if (Spassword! = "")
{
S.password = Spassword; Zip compressed file password
}
S.setlevel (6);
Zip (strfile, S, strfile);
S.finish ();
S.close ();
}
<summary>
Compressed folder
</summary>
<param name= "Strfile" ></param>
<param name= "Strzip" ></param>
<param name= "Spassword" ></param>
public void ZipFile (String strfile, String strzip)
{
if (strfile[strfile.length-1]! = Path.directoryseparatorchar)
Strfile + = Path.directoryseparatorchar;
Zipoutputstream s = new Zipoutputstream (File.create (strzip));
S.setlevel (6);
Zip (strfile, S, strfile);
S.finish ();
S.close ();
}
private void Zip (String strfile, Zipoutputstream s, String staticfile)
{
if (strfile[strfile.length-1]! = Path.directoryseparatorchar) strfile + = Path.directoryseparatorchar;
Crc32 CRC = New Crc32 ();
string[] filenames = directory.getfilesystementries (strfile);
foreach (string file in filenames)
{
if (directory.exists (file))
{
Zip (file, S, staticfile);
}
else//Otherwise compress files directly
{
Open compressed file
FileStream fs = File.openread (File);
byte[] buffer = new BYTE[FS. Length];
Fs. Read (buffer, 0, buffer. Length);
String tempfile = file. Substring (staticfile.lastindexof ("\ \") + 1);
ZipEntry entry = new ZipEntry (tempfile);
Entry. DateTime = DateTime.Now;
Entry. Size = fs. Length;
Fs. Close ();
Crc. Reset ();
Crc. Update (buffer);
Entry. CRC = CRC. Value;
S.putnextentry (entry);
S.write (buffer, 0, buffer.) Length);
}
}
}
}
}
C # Compressed folder