c# - DotNetZip - Create a Zip from a list of items -


i have .net c# application request information database , store records in list structure.

public class record {     public string name { get; set; }    public string surname { get; set; } }  list<record> lst = new list<record>(); 

i iterate on list , adding each record zip file. not want create txt file containing these records (a record line) , once file saved on disk, create zip file file, mean, not want create intermediate file on disk in order create zip file that.

how can using dotnetzip?

the zipfile can take stream.

zipfile.addentry(string entryname, stream stream) 

you want create memorystream , add stream file.

for example:

using (var stream = new memorystream()) {     using (var sw = new streamwriter(stream)) {         foreach (var record in lst) {             sw.writeline(record.surname + "," + record.name);         }         sw.flush();         stream.position = 0;          using (zipfile zipfile = new zipfile()) {             zipfile.addentry("records.txt", stream);             zipfile.save("archive.zip");         }     } } 

Comments