Dynamic Zip and Download multiple files

מאת Nu Solutions
בתאריך 11 יולי, 2010

One of the common tasks which comes up time and again for Internet developer, is the need to Download multiple files to the Web User. Typically, it is necessary to Zip the files before download. This common task has no native solution in ASP.NET. This articles looks at how to do this in ASP.NET.

Dynamic Zip and Download multiple files

בא נחלק את זה לשני חלקים:

1) לבנות ZIP ARCHIVE

2) להוריד את הקובץ.

 

1) לבנות ZIP ARCHIVE

אני משתמש בספרית OPENSOURCEש נקרא ICSharpCode.SharpZipLib

ניתן להוריד את הספריה כאן:     http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

 אחרי שהתוכנה יצרה קבצים ושמה אותם בתיקיה, מעבירים את התיקיה + תיקיה ליצירת הארכיון, ושם הארכיון לפונקציה שלמטה.

 

 

usingSystem;

usingSystem.Collections.Generic;

usingICSharpCode.SharpZipLib;

usingICSharpCode.SharpZipLib.Zip;

usingSystem.Web;

 

 publicstaticstringZipDirectory(stringdirToZip, stringzipOutDir, stringzipOutFilename)

{

 

        // Check input directory exists

        if(!System.IO.Directory.Exists(dirToZip))

                 return"";

       

        if(!System.IO.Directory.Exists(zipOutDir))

            return"";

       

        // Zip up the files

        string[] filenames = System.IO.Directory.GetFiles(dirToZip);

        stringzipName = zipOutDir + "\\"+ zipOutFilename + ".zip";

        using(ZipOutputStreams = newZipOutputStream(System.IO.File.Create(zipName)))

        {

         s.SetLevel(9);

         byte[] buffer = newbyte[4096];

         foreach(stringfile infilenames)

         {

             ZipEntryentry = newZipEntry(System.IO.Path.GetFileName(file));

             entry.DateTime = DateTime.Now;

             s.PutNextEntry(entry);

             using(System.IO.FileStreamfs = System.IO.File.OpenRead(file))

             {

                 intsourceBytes;

                 do

                 {

                     sourceBytes = fs.Read(buffer, 0, buffer.Length);

                     s.Write(buffer, 0, sourceBytes);

                 }

                 while(sourceBytes > 0);

             }

         }

 

         s.Finish();

         s.Close();

         returnzipName;

       }

 

 2) הורדת הARCHIVE

 סוף דבר הכל נשמע.  אחרי שנגמור להוריד את הארכיון, רצוי גם למחוק אותה וכאן נמצא בעיה קטנה.  אם נמחוק קבצים שמתחת לFOLDER  של קבצי הWEB, יהיה RESET  לאפלקציה והSESSION VARIABLES הולכים לאיבוד.  התוצאה שהמשתמש מוצא את עצמו LOGGED OUTאו יותר גרוע – APPLICATION ERROR!.  אז קודם כל לוודה שמייצרים את הארכיון בFOLDER מתאים.

וככה עושים את הDOWNLOAD עצמה.

            // Download files

            try

            {

                Response.BufferOutput = false;

                  Response.ContentType = "application/zip";

                Response.AddHeader("Content-Disposition", "attachment;filename=IISInstall_Files.zip");

                  Response.WriteFile(zipName);

                Response.Close();

            }

            finally

            {

                System.IO.Directory.Delete(dirToZip, true);

{        

 

צירפתי את קבצי הCODE.  שימוש מהנה!

 יהודה גבאי

 

מאמרים נוספים...