Zip Files

Hi,

I have the requirement to create several Excel files, add the Excel files to a new zip file and return the zip file to the user.

I have been following https://help.syncfusion.com/file-formats/xlsio/faq#how-to-zip-files-using-the-syncfusioncompressionzip-namespace trying to accomplish my goal.  The problem I am facing is that "ZipArchive" doesn't have the "AddFile" method as described in the doc.  I tried using "AddItem" method but I don't know how to get the attributes.  "File.GetAttributes()" doesn't exist.

Could you give me a sample in ASP.Net Core that creates multiple excel files and then adds the excel files to a new zip file?  Below is some code I have been working on.

In my view I have the following javascript:

        function runConRpt(Id) {
            Id = parseInt(Id);
            if (Id > 1) {
                var popupobj = $("#customWaiting").data("ejWaitingPopup");
                popupobj.show();

                $.ajax({
                      type: "POST",
                      url: "/MyController/ConRpt",
                      data: {
                        "conRptId": Id},
                      success: function (data) {
                         var popupobj = $("#customWaiting").data("ejWaitingPopup");
                         popupobj.hide();
                         if (data.fileName != "") {
                             window.location.rel='nofollow' href = "@Url.RouteUrl(new { Controller = "MyController", Action = "DownloadConRpt" })/?file=" + data.fileName;
                         }
                      }
                 });
            }
        }

In my controller, I have:

        public ActionResult ConRpt(int ConRptId)
        {
            ExcelEngine excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;
            application.DefaultVersion = ExcelVersion.Excel2016;

            // create new workbook
            IWorkbook workbook = application.Workbooks.Create();

           //TODO: actually create the contact reports.  I am only creating blank excel docs to see if I can add them to a zip file

            // save excel docs to server
            string fullPath = _hostingEnvironment.WebRootPath + @"\XlsIO\MyController\Download\test1.xlsx";
            FileStream outputStream = new FileStream(fullPath, FileMode.Create);
            workbook.SaveAs(outputStream);

            string fullPath2 = _hostingEnvironment.WebRootPath + @"\XlsIO\MyController\Download\test2.xlsx";
            FileStream outputStream2 = new FileStream(fullPath2, FileMode.Create);
            workbook.SaveAs(outputStream);

            workbook.Close();
            outputStream.Close();
            outputStream2.Close();
            excelEngine.Dispose();

            // now add the excel files to zip file
            ZipArchive zipArchive = new ZipArchive();
            zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best;
                       
            //FileAttributes attribute = File.GetAttributes(fullPath);  //File.GetAttributes is not a method

            //zipArchive.addFile(); // AddFile() is not a method
            //zipArchive.AddItem(fullPath, outputStream, false, )  // don't know how to properly use this method
            //zipArchive.Save(@"path_and_filename");  // the Save method that I have doesn't take the same parameters as the syncfusion documentation

           //TODO: Delete the excel files

            try
            {
                return Json(new { fileName = "zipfile.zip", errorMessage = "" });
            }
            catch
            {
                return View();
            }           
        }

        public ActionResult DownloadConRpt(string file)
        {
            string fullPath = _hostingEnvironment.WebRootPath + @"\XlsIO\MyController\Download\" + file;
            byte[] fileByteArray = System.IO.File.ReadAllBytes(fullPath);
            System.IO.File.Delete(fullPath);
            return File(fileByteArray, "Application/msexcel", "test1.xlsx");
        }


3 Replies

PK Prakash Kumar D Syncfusion Team October 22, 2018 12:10 PM UTC

Hi Chris, 
 
Thank you for contacting Syncfusion support. 
 
The ZipArchive.AddFile(string fileName) method is not available in ASP.NET Core platform. However, your requirement can be achieved by using the following code snippet. 
 
Code snippet: 
ZipArchive zipArchive = new ZipArchive(); 
zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best; 
 
//Add the file you want to zip. 
zipArchive.AddItem("Test1.xlsx", outputStream, false, Syncfusion.Compression.FileAttributes.Normal); 
zipArchive.AddItem("Test2.xlsx", outputStream2, false, Syncfusion.Compression.FileAttributes.Normal); 
 
MemoryStream stream = new MemoryStream(); 
zipArchive.Save(stream, false); 
 
 
We have prepared a simple sample using above code snippet which can be downloaded from the following link. 
 
 
 
Regards, 
Prakash Kumar 



CH Chris October 22, 2018 03:48 PM UTC

Thank you, I got it working.

FYI, the documentation I used to zip files under ASP.Net Core is for MVC. 

If you follow these steps it will give you instructions for MVC not ASP.Net Core:
- Go to syncfusion website
- Click "Learning & Support" and choose "Documentation"
- Then click ASP.Net Core under Essential JS 1
- Expand XlsIO on the left hand side and click on "FAQ"
- Then on the right hand side click on "How to zip files using the Syncfusion.Compression.Zip" namespace.

Thanks,
Chris


PK Prakash Kumar D Syncfusion Team October 23, 2018 09:36 AM UTC

Hi Chris, 
 
Thank you for helping us to defining this Document issue. We will consider this and update the same in our 2018 volume 3 service pack 1 release (16.3 SP-1) which will be rolled out at the last week of October 2018 tentatively. 
 
Regards, 
Prakash Kumar 


Loader.
Up arrow icon