C # Compressed folder

Source: Internet
Author: User
Tags crc32 log4net

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

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.