Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Can you dynamically define a folder structure while streaming from ASP.NET?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #20396 |

    I’m attempting to zip and stream files from asp.net. I found the following post which works great:

    http://xceed.com/CS/forums/permalink/13036/13034/ShowThread.aspx#13034

    However during this process I would like to change the folder structure.
    My goal is to minimize disk and memory usage. 

    Here is an example:

    Given this DataSource:
    <code>

    var fileList = new Dictionary<string, string>

                                   {

                                       {@”C:\Data\Files\1.txt”,@”C:\Examples\1.txt”},

                                       {@”C:\Data\Files\2.txt”,@”C:\Folder\2.txt”},

                                       {@”C:\Data\Files\3.txt”,@”C:\Yesterday\3.txt”}

                                   };

     

    <code> 

    And assuming my physical files

    “C:\Data\Files\1.txt”
    “C:\Data\Files\2.txt”
    “C:\Data\Files\3.txt” 

    When the user finishes downloading the zip, I would like the folder structure to be:

    “C:\Examples\1.txt”
    “C:\Folder\2.txt”
    “C:\Yesterday\3.txt”

    How do I accomplish this?

    Imported from legacy forums. Posted by Justin (had 818 views)

    User (Old forums)
    Member
    Post count: 23064

    I figured it out. The issue was the syntax for my destination pathing. 

    Here is the corrected code in full (changes called out in bold)

     <code>

    protected void btnDownload_Click(object sender, EventArgs e)
    {
    var fileList = new Dictionary<string, string>
    {

    {@”C:\Data\Files\1.txt”, @”Examples\1.txt”}
    , {@”C:\Data\Files\2.txt”, @”Folder\2.txt”}
    , {@”C:\Data\Files\3.txt”, @”Yesterday\3.txt”}
    };

    Response.Clear();
    Response.ClearHeaders();
    Response.Expires = -1;
    Response.Buffer = false;
    Response.ContentType = “application/x-zip-compressed”;
    Response.AddHeader(“Content-Disposition”, “attachment;filename=Files.zip”);

    using (var zipWriter = new ZipWriter(Response.OutputStream))
    {
    foreach (var fileToCompress in fileList)
    {
    if (File.Exists(fileToCompress.Key) == false) continue;

    var fileInfo = new FileInfo(fileToCompress.Key);
    var zipItemLocalHeader = new ZipItemLocalHeader(fileToCompress.Value
    , CompressionMethod.Deflated
    , CompressionLevel.Normal); zipWriter.WriteItemLocalHeader(zipItemLocalHeader);

    var buffer = new byte[1023];
    using (var fileStream = fileInfo.OpenRead())
    {
    int read;
    do
    {
    read = fileStream.Read(buffer, 0, buffer.Length); zipWriter.WriteItemData(buffer, 0, read); Response.Flush();
    } while (read != 0 && Response.IsClientConnected);
    }
    }

    zipWriter.CloseZipFile();
    Response.Flush();
    Response.End();

    }

    }

     

    </code> 

    Imported from legacy forums. Posted by Justin (had 870 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.