tags:

views:

1401

answers:

6

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?

+1  A: 

This code on codeproject could be used in a asp.net app to achieve what you need:

http://www.codeproject.com/KB/recipes/ZipStorer.aspx

Richard
+4  A: 

You could take a look at SharpZipLib. And here's a sample.

Darin Dimitrov
beat me to it 15 seconds...
JoshBerke
+1  A: 

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.

Macros
We used this component in our company too. Based on errors in the component some of our extremly stressed services caused an EngineException. After an Microsoft Support Ticket we decided to switch to SharpZLib. This was 2 years ago. I do not know how good the component is today?
michl86
We've never had any problems with it however is used in export services which generally run at the most every hour, but typically daily. The encryption is useful as well
Macros
+5  A: 

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).

Will
+1 for the extremely useful link that the author is keeping up to date.
Jeff Sternal
+3  A: 

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.

Cheeso
A: 

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();
Jan Šotola