locked
Zip two folder in asp.net RRS feed

  • Question

  • User-1752241410 posted

    Hi,   

            I want to zip two different folders simultaneously.

    Expecting for your replies.

     

    Thanks in advance... 


     

    Friday, April 25, 2008 4:09 AM

Answers

  • User2022958948 posted

    Hi,

    As far as I know, you can achieve it by using ICSharpCode.SharpZipLib.Zip.

    This dll file can be available at: http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx You can choose the edition you need after download.

    The below codes can zip a file int zip format.

        using ICSharpCode.SharpZipLib.Zip;
        using System.IO;
        using System.Threading;
     
        ZipOutputStream zos = null;
        protected void Button1_Click(object sender, EventArgs e)
        {
            string[] PathCellction = new string[2];
            PathCellction[0] = "E:\\folder1";
            PathCellction[1] = "c:\\folder2";  // Don't use c:\\folder2\\ or c:/folder2.
            StartZip(PathCellction, "filename"); 
    
        }
        protected void StartZip(string[] strPathCellection, 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);
    
            addZipEntry(strPathCellection);
    
            zos.Finish();
            zos.Close();
            Response.Clear();
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }
        protected void addZipEntry(string[] PathStrCellection)
        {
            for (int i = 0; i < PathStrCellection.Length; i++)
            {
                string strPath = PathStrCellection[i].ToString();
                addZipEntry(strPath, strPath.LastIndexOf("\\") + 1);
            }
    
        }
        protected void addZipEntry(string PathStr, int strBaseIndex)
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName, strBaseIndex);
            }
            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.Remove(0, strBaseIndex);
    
                ZipEntry entry = new ZipEntry(strEntryName);
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }
     
    In the above codes, you can zip two different folders.
    Hope it helps.
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 29, 2008 2:29 AM

All replies

  • User-1752241410 posted

     Still no answers ...... [:(]

    Friday, April 25, 2008 6:24 AM
  • User-1491146390 posted

    Hi,

    check this link   http://forums.asp.net/p/1143608/1846020.aspx

    Friday, April 25, 2008 8:36 AM
  • User-128063356 posted

    Hi,

    Check these. 

     

    http://www.icsharpcode.net/OpenSource/SharpZipLib/
    http://www.codeproject.com/KB/cs/Zip_UnZip.aspx
    http://www.example-code.com/csharp/zip.asp

     

    Saturday, April 26, 2008 6:30 AM
  • User2022958948 posted

    Hi,

    As far as I know, you can achieve it by using ICSharpCode.SharpZipLib.Zip.

    This dll file can be available at: http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx You can choose the edition you need after download.

    The below codes can zip a file int zip format.

        using ICSharpCode.SharpZipLib.Zip;
        using System.IO;
        using System.Threading;
     
        ZipOutputStream zos = null;
        protected void Button1_Click(object sender, EventArgs e)
        {
            string[] PathCellction = new string[2];
            PathCellction[0] = "E:\\folder1";
            PathCellction[1] = "c:\\folder2";  // Don't use c:\\folder2\\ or c:/folder2.
            StartZip(PathCellction, "filename"); 
    
        }
        protected void StartZip(string[] strPathCellection, 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);
    
            addZipEntry(strPathCellection);
    
            zos.Finish();
            zos.Close();
            Response.Clear();
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }
        protected void addZipEntry(string[] PathStrCellection)
        {
            for (int i = 0; i < PathStrCellection.Length; i++)
            {
                string strPath = PathStrCellection[i].ToString();
                addZipEntry(strPath, strPath.LastIndexOf("\\") + 1);
            }
    
        }
        protected void addZipEntry(string PathStr, int strBaseIndex)
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName, strBaseIndex);
            }
            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.Remove(0, strBaseIndex);
    
                ZipEntry entry = new ZipEntry(strEntryName);
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }
     
    In the above codes, you can zip two different folders.
    Hope it helps.
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 29, 2008 2:29 AM