Upload and Compress as a zip file

4 posts, 0 answers
  1. Babak
    Babak avatar
    12 posts
    Member since:
    Jan 2012

    Posted 01 Jul 2013 Link to this post

    Hi .
    I want to compress all my files uploaded via Radasyncupload and save them as a zip file in a specific folder .
    how can I do this . 
    tnx
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 02 Jul 2013 Link to this post

    Hi Babak,

    You can use the ZipFile class to achieve your requirement. Please have a look at the following code I tried which works fine as expected. I am uploading few image files to the Target folder and using the ZipFile class I am creating the .zip file and finally discarding the files uploaded in the Target folder. For this to work you need the .NetFramework 4.5 version installed on your machine and need to refer the System.IO.Compression.FileSystem DLL. If you cant find the reference you can download it from here.

    ASPX:
    <telerik:RadAsyncUpload runat="server" ID="AsyncUpload_Attachment" TargetFolder="~/Images/Img/"
        OnFileUploaded="AsyncUpload_Attachment_FileUploaded" PostbackTriggers="RadButton1"
        InitialFileInputsCount="1" MaxFileSize="10240000" MultipleFileSelection="Automatic" />
    <telerik:RadButton runat="server" ID="RadButton1" Text="Upload and Generate Zip"
        OnClick="RadButton1_Click" />

    C#:
    protected void AsyncUpload_Attachment_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        string targetfolder = AsyncUpload_Attachment.TargetFolder;
        e.File.SaveAs(Path.Combine(Server.MapPath(targetfolder), e.File.FileName));
    }
     
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        string uploadTarget = AsyncUpload_Attachment.TargetFolder;
     
        string startPath = Server.MapPath(uploadTarget); //source path to create zip files.
        string zipPath = Path.Combine(Server.MapPath("~/Images/FinalUpload"), "User1.zip"); //Setting path and filename of .zip to be created.
        ZipFile.CreateFromDirectory(startPath, zipPath); //creating the .zip
        Array.ForEach(Directory.GetFiles(Server.MapPath(uploadTarget)),
                delegate(string f) { File.Delete(f); }); //deleting the uploaded files after creating .zip file
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Babak
    Babak avatar
    12 posts
    Member since:
    Jan 2012

    Posted 02 Jul 2013 Link to this post

    Hi Shinu.
    your code was very helpful . but I have another problem . If multiple user upload files in the same time we have a zip file that contains all the files . what's your plan for this problem ?
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 03 Jul 2013 Link to this post

    Hi Babak,

    One suggestion is at the time of uploading files you can create separate folder for individual users inside the target folder. Suppose the target folder name is InitialUploads, then when a user (say User1) tries to upload files, that can be stored inside a folder named User1 inside the Target folder "InitialUploads" and after creating the .zip you can delete this User1 folder.

    Please have a look at the following sample code based on the mark-up I provided. When the user login some particular identity for ex. Username will be saved in the session and that you can use while uploading files as follows.

    C#:
    protected void AsyncUpload_Attachment_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        string targetfolder = AsyncUpload_Attachment.TargetFolder+Convert.ToString(Session["Username"]);
        if (!System.IO.Directory.Exists(Server.MapPath(targetfolder)))
        {
            System.IO.Directory.CreateDirectory(Server.MapPath(targetfolder)); //Creating user folder if it does not exists in Target folder.
        }
        e.File.SaveAs(Path.Combine(Server.MapPath(targetfolder), e.File.FileName));
    }
     
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        string uploadTarget = AsyncUpload_Attachment.TargetFolder + Convert.ToString(Session["Username"]);
     
        string startPath = Server.MapPath(uploadTarget); //source path to create zip files.
        string zipPath = Path.Combine(Server.MapPath("~/Images/FinalUpload"), Convert.ToString(Session["Username"])+".zip"); //Setting path and filename of .zip to be created.
        ZipFile.CreateFromDirectory(startPath, zipPath); //creating the .zip
        System.IO.Directory.Delete(startPath, true); //deleting the user folder in Target folder after creating .zip file
    }

    Thanks,
    Shinu.
     
Back to Top