Creating Zip file from stream and downloading it

前端 未结 8 1103
深忆病人
深忆病人 2020-12-02 22:20

I have a DataTable that i want to convert it to xml and then zip it, using DotNetZip. finally user can download it via Asp.Net webpage. My code in below

            


        
8条回答
  •  渐次进展
    2020-12-02 22:57

    Creating a zip file from stream and downloading it. Below is the code.

    FileStream stream=File.OpenRead(@"D:\FileDownLoad\DeskTop\1.txt");
    MemoryStream MS=new MemoryStream();
    
    ZipOutputStream zipOutputStream = new ZipOutputStream(MS);
    zipOutputStream.SetLevel(9);
    ZipEntry entry = new ZipEntry("1.txt");
    zipOutputStream.PutNextEntry(entry);
    
    byte[] buffer = new byte[stream.Length];
    int byteRead = 0;
    
    while ((byteRead = stream.Read(buffer, 0, buffer.Length)) > 0) 
        zipOutputStream.Write(buffer, 0, byteRead);
    
        zipOutputStream.IsStreamOwner = false;
        stream.Close();
        zipOutputStream.Close();
        MS.Position = 0;
    
        Response.ContentType = "application/application/octet-stream";
        Response.AppendHeader("content-disposition", "attachment; filename=\"Download.zip\"");
        Response.BinaryWrite(MS.ToArray());
    

提交回复
热议问题