user1008784
user1008784

Reputation:

Create a ZIP file without entries touching the disk?

I'm trying to create a program that has the capability of creating a zipped package containing files based on user input.

I don't need any of those files to be written to the hard drive before they're zipped, as that would be unnecessary, so how do I create these files without actually writing them to the hard drive, and then have them zipped?

I'm using DotNetZip.

Upvotes: 4

Views: 1877

Answers (3)

Bobson
Bobson

Reputation: 13706

See the documentation here, specifically the example called "Create a zip using content obtained from a stream":

 using (ZipFile zip = new ZipFile())
  {
    ZipEntry e= zip.AddEntry("Content-From-Stream.bin", "basedirectory", StreamToRead);
    e.Comment = "The content for entry in the zip file was obtained from a stream";
    zip.AddFile("Readme.txt");
    zip.Save(zipFileToCreate);
  }

If your files are not already in a stream format, you'll need to convert them to one. You'll probably want to use a MemoryStream for that.

Upvotes: 4

Spencer Ruport
Spencer Ruport

Reputation: 35117

Writing to the hard disk shouldn't be something avoid because it's unnecessary. That's backwards. If it's not a requirement that the entire zipping process is done in memory then avoid it by writing to the hard disk.

The hard disk is better suited for storing large amounts of data than memory is. If by some chance your zip file ends up being around a gigabyte in size your application could croak or at least cause a system slowdown. If you write directly to the hard drive the zip could be several gigabytes in size without causing an issue.

Upvotes: 0

hometoast
hometoast

Reputation: 11792

I use SharpZipLib, but if DotNetZip can do everything against a basic System.IO.Stream, then yes, just feed it a MemoryStream to write to.

Upvotes: 0

Related Questions