Top Ad unit 728 × 90

random

How to write data and zip the file using ionic.Zip in asp.net & C#.

  private string WriteResponseInZip()
        {


DataSet ResultDataSet= new DataSet();

DataTable customers = myDataset.Tables.Add("Customers");

customers.Columns.Add("Name");
customers.Columns.Add("Age");

customers.Rows.Add("Chris", "25");

            //Prefixing the filename for download data.

            var fileName = "TestFile" + "_" + Guid.NewGuid();
            var srcDirectory = "C:\Test apps\test\final\" + fileName + "\\";
            var destinDirectory = "C:\Test apps\test\final1";
            var srcFile = srcDirectory + fileName + ".txt";  // Put the source file name
            var desFile = destinDirectory + fileName + ".zip";// to contanin destination file path

            //creating a source directory to store the file.
            Directory.CreateDirectory(srcDirectory);

            //Writing response to src txt file
            File.WriteAllText(srcFile, JsonConvert.SerializeObject(ResultDataSet, Formatting.None)); put data in any dataset and pass here ex. ResultDataset

            //Creating Password protected zip file
            using (var zip = new ZipFile())
            {
                zip.AddFile(srcFile, ".");// "." is passed to provide the compressed file at root level.
                zip.Save(desFile);
            }

            //Zompressing the src directory
            //ZipFile.CreateFromDirectory(srcDirectory, desFile);

            //Deleteing Source file/directory as zip file is created
            Directory.Delete(srcDirectory, true);
            return fileName;
        }




1). For performing the above operation. first download the ionic.zip file from Nuget Package mangaer in Solution Explorer. 
and done the above operation , you will get zip folder download with content.

How to write data and zip the file using ionic.Zip in asp.net & C#. Reviewed by Vikas Kumar Singh on November 30, 2017 Rating: 5

No comments:

All Rights Reserved by DotNetBasic.com © 2019

Contact Form

Name

Email *

Message *

© 2018 All rights reserved by DotNetBasic. Powered by Blogger.