How to create and fill a ZIP file using ASP.NET?

后端 未结 9 1832
暖寄归人
暖寄归人 2020-12-14 17:29

Need to dynamically package some files into a .zip to create a SCORM package, anyone know how this can be done using code? Is it possible to build the folder structure dynam

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 17:42

    Creating ZIP file "on the fly" would be done using our Rebex ZIP component.

    The following sample describes it fully, including creating a subfolder:

    // prepare MemoryStream to create ZIP archive within
    using (MemoryStream ms = new MemoryStream())
    {
        // create new ZIP archive within prepared MemoryStream
        using (ZipArchive zip = new ZipArchive(ms))
        {            
             // add some files to ZIP archive
             zip.Add(@"c:\temp\testfile.txt");
             zip.Add(@"c:\temp\innerfile.txt", @"\subfolder");
    
             // clear response stream and set the response header and content type
             Response.Clear();
             Response.ContentType = "application/zip";
             Response.AddHeader("content-disposition", "filename=sample.zip");
    
             // write content of the MemoryStream (created ZIP archive) to the response stream
             ms.WriteTo(Response.OutputStream);
        }
    }
    
    // close the current HTTP response and stop executing this page
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    

提交回复
热议问题