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 dynamically inside of the .zip as well?
This code on codeproject could be used in a asp.net app to achieve what you need:
I have used a free component from chilkat for this: http://www.chilkatsoft.com/zip-dotnet.asp. Does pretty much everything I have needed however I am not sure about building the file structure dynamically.
You don't have to use an external library anymore. System.IO.Packaging has classes that can be used to drop content into a zip file. Its not simple, however. Here's a blog post with an example (its at the end; dig for it).
DotNetZip is nice for this. Working example
You can write the zip directly to the Response.OutputStream. The code looks like this:
Response.Clear();
Response.BufferOutput = false; // for large files...
System.Web.HttpContext c= System.Web.HttpContext.Current;
String ReadmeText= "Hello!\n\nThis is a README..." + DateTime.Now.ToString("G");
string archiveName= String.Format("archive-{0}.zip",
DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + archiveName);
using (ZipFile zip = new ZipFile())
{
// filesToInclude is an IEnumerable<String>, like String[] or List<String>
zip.AddFiles(filesToInclude, "files");
// Add a file from a string
zip.AddEntry("Readme.txt", "", ReadmeText);
zip.Save(Response.OutputStream);
}
// Response.End(); // no! See http://stackoverflow.com/questions/1087777
Response.Close();
DotNetZip is free.
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();