asp.net implementation of folder and file compression, and implementation of the download

Source: Internet
Author: User
Tags foreach zip

Steps

1) First quote 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 to compress and download the code
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: Huahai Training (qqview.com) All levels of accounts and passwords";

Dlzipdir (UserPath, "Huahai Training (qqview.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.