Asp.net uses code to Compress folders or files on the server

Source: Internet
Author: User

Implementation Process

1) first reference ICSharpCode. SharpZipLib. dll;

2). cs file Introduction

Using System. IO;
Using System. Text;

Using ICSharpCode. SharpZipLib. Checksums;
Using ICSharpCode. SharpZipLib. Zip;
Using ICSharpCode. SharpZipLib. GZip;

3) Code

# Region the following code is compressed and downloaded
ZipOutputStream zos = null;
String strBaseDir = "";

Void dlZipDir (string strPath, string strFileName)
{
MemoryStream MS = null;
Response. ContentType = "application/octet-stream ";
StrFileName = HttpUtility. UrlEncode (strFileName). Replace ('+ ','');
Response. AddHeader ("Content-Disposition", "attachment; filename =" + strFileName + ". zip ");
MS = new MemoryStream ();
Zos = new ZipOutputStream (MS );
StrBaseDir = strPath + "";
AddZipEntry (strBaseDir );
Zos. Finish ();
Zos. Close ();
Response. Clear ();
Response. BinaryWrite (ms. ToArray ());
Response. End ();
}

Void addZipEntry (string PathStr)
{
DirectoryInfo di = new DirectoryInfo (PathStr );
Foreach (DirectoryInfo item in di. GetDirectories ())
{
AddZipEntry (item. FullName );
}
Foreach (FileInfo item in di. GetFiles ())
{
FileStream fs = File. OpenRead (item. FullName );
Byte [] buffer = new byte [fs. Length];
Fs. Read (buffer, 0, buffer. Length );
String strEntryName = item. FullName. Replace (strBaseDir ,"");
ZipEntry entry = new ZipEntry (strEntryName );
Zos. PutNextEntry (entry );
Zos. Write (buffer, 0, buffer. Length );
Fs. Close ();
}
}

PRotected void Button2_Click (object sender, EventArgs e)
{
String userPath = "D: accounts and passwords at all levels of hohisoft.com ";

DlZipDir (userPath, "hohisoft.com service account ");
}

# Endregion

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.