How to Unzip a Zip File on server in Asp.Net

Introduction

Today I will explain you about how to store unzip files of zip format in server directories

Step 1

Download ICSharpCode.SharpZipLib dll from this link

Step 2



I assumes that you have implemented the upload control with the zip validation on web pages and added the references of above mentioned dll file in your asp.net web site project.

if ((Path.GetExtension(fileupload1.FileName).ToLower() == ".zip"))
                    {
                        string _str = "";
                        _str = fileupload1.FileName;

                        string directoryName = _str;
}


 Step 3

And after that implement the below Code for unzipping the zip file on server

using (ZipInputStream s = new ZipInputStream(File.OpenRead(Server.MapPath(" Your Folder Mapping"))
                                {
                                    ZipEntry theEntry;

                                    while ((theEntry = s.GetNextEntry()) != null)
                                    {
                                        string directoryName2 = Path.GetDirectoryName(theEntry.Name);
                                        string fileName;
                                        //string directoryName = Path.GetDirectoryName("zip2");
                                        //string fileName = Path.GetFileName(theEntry.Name);

                                        //theEntry = s.GetNextEntry();
                                        // string directoryName3 = Path.GetDirectoryName(theEntry.Name);
                                        // create directory cintaining folder if necessary

                                       if (directoryName2.Length > 0)
                                        {
                                            Directory.CreateDirectory(Server.MapPath("Your Folder Mapping");

                                        }
                                        fileName = Path.GetFileName(Server.MapPath("Your Folder Mapping);



                                        if (fileName != String.Empty)
                                        {
                                            using (FileStream streamWriter = File.Create(

                                            Server.MapPath("Your Folder Mapping))
                                            {

                                                int size = 500000;
                                                byte[] data = new byte[500000];
                                                while (true)
                                                {
                                                    size = s.Read(data, 0, data.Length);
                                                    if (size > 0)
                                                    {
                                                        streamWriter.Write(data, 0, size);
                                                    }
                                                    else
                                                    {
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }




Step 4

Give Read,Write permission for folder structure with proper user rights

Step 5

Check the uploaded Zip file extracted folder on mapped directory of server.

Happy Programming.








Comments