I'm needing to create a zipped document containing files that exist on the server. I am using the Ionic.Zip to do so, and to create a new Package (which is the zip file) I have to have either a path to a physical file or a stream. I am trying to not create an actual file that would be the zip file, instead just create a stream that would exist in memory or something. how can i do this?
3 Answers
You can try the save method in the ZipFile Class. It can save to a stream try this.
using (MemoryStream ms = new MemoryStream())
{
using (Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile())
{
zipFile.AddFiles(filesToBeZipped, false, "NewFolder");//filesTobeZipped is a List<string>
zipFile.Save(ms);
}
}
3 Comments
Krishan
Vignesh Natraj : where is this zip file saving, what is the meaning of this new folder. if you can give answer asap it will be great help to me
Vignesh.N
you can check the method signature for AddFiles,"NewFolder" is the folder structure inside the Compressed Zip file, I am telling the method to put all files inside a folder called NewFolder.
Vignesh.N
and it'll save to the memory stream like you asked.
You'll want to use the .AddEntry method on the ZipFile you've created specifying a name and the byte[] containing the actual file data.
ex.
ZipFile zipFile = new ZipFile();
zipFile.AddEntry(file.FileName, file.FileData);
where file.FileName will be the entry name (in the zip file) and file.FileData is the byte array.