Using DotNetZip to compress file during upload

Last post 12-18-2008, 8:59 AM by cutechat. 7 replies.
Sort Posts: Previous Next
  •  12-10-2008, 11:55 AM 46762

    Using DotNetZip to compress file during upload

    I am using the uploader fine as it is, but would like to be able to compress the files as they are uploaded. I've seen the forum post with sample code, but haven't figured out how to integrate it with my code.
    I've added to my C# class:
    using Ionic.Utils.Zip;
    My current code block is:

    Uploader uploader = (Uploader)sender;

    uploader.SetAdvancedOption(UploaderAdvancedOption.NoFlash10, "true");

    InsertMsg("File uploaded! " + args.FileName + ", " + args.FileSize + " bytes.");

    string selectedItem = DropDownListTemplateFields.SelectedItem.Text.ToString();

    selectedItem = StringHelper.Replace(selectedItem," ","");

    string folder = Server.MapPath("~/Assets/OrderUploads");

    string thisOrderId = OrderId.ToString();

    string pathURL = "../Assets/OrderUploads/";

    string fileName = thisOrderId + "-" + DateTime.Now.ToString("yyyyMMddHmm") + "-" + selectedItem + "-" + args.FileName;

     

    string mypath = Path.Combine(folder, fileName);

    args.CopyTo(mypath);
     
     
    How can I integrate it? It is for only one file at a time.
    Thanks,
  •  12-12-2008, 4:24 PM 46869 in reply to 46762

    Re: Using DotNetZip to compress file during upload

    Maybe if I re-phrased this, it would help. What is the best way to intercept the file? As it is being uploaded? After it is uploaded and when it is being copied with it's new name to the permanent folder? I don't want to do anything that will keep the upload from being written to the hard drive and make it eat up computer memory and I'm not sure  how this all works. What terminology would I use to refer to the the temporary file within the code to zip it?
    Code:

    using (ZipFile zip = new ZipFile("[What do I put here?"))

    zip.Save(Do I put anything here or leave it blank?);
     
    Thanks
  •  12-12-2008, 11:43 PM 46877 in reply to 46869

    Re: Using DotNetZip to compress file during upload

    Hi jmestep,
     
    Please try this code:
     

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

        protected void uploader1_FileUploaded(object sender, UploaderEventArgs args)
        {
            Ionic.Utils.Zip.ZipFile file = new Ionic.Utils.Zip.ZipFile(Response.OutputStream);
            file.AddFileStream(args.FileName, "", args.OpenStream());
            file.Save("C://test//" + args.FileName + ".zip");
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <CuteWebUI:Uploader ID="uploader1" runat="server" OnFileUploaded="uploader1_FileUploaded">
                </CuteWebUI:Uploader>
            </div>
        </form>
    </body>
    </html>

     
     
    Regards,
     
    Ken
  •  12-13-2008, 8:56 AM 46883 in reply to 46877

    Re: Using DotNetZip to compress file during upload

    Thank you very much. That worked!
  •  12-14-2008, 2:03 PM 46912 in reply to 46883

    Re: Using DotNetZip to compress file during upload

    Well, it kind of worked. I just found that files that are uploaded and zipped can't be downloaded from the webpage or ftp. I checked the file permissions on the files as opposed to the files that weren't zipped and the username for that website doesn't have permissions on the zipped files. Can you tell me what I would need to do to ensure the same permissions as AjaxUploaded applies to files? Here is my code:

    void Uploader_FileUploaded(object sender, UploaderEventArgs args)

    {

    Uploader uploader = (Uploader)sender;

    uploader.SetAdvancedOption(UploaderAdvancedOption.NoFlash10, "true");

    InsertMsg("File uploaded! " + args.FileName + ", " + args.FileSize + " bytes.");

    string selectedItem = DropDownListTemplateFields.SelectedItem.Text.ToString();

    selectedItem = StringHelper.Replace(selectedItem, " ", "");

    string folder = Server.MapPath("~/Assets/OrderUploads");

    string thisOrderId = OrderId.ToString();

    string pathURL = "../Assets/OrderUploads/";

    string fileName = thisOrderId + "-" + DateTime.Now.ToString("yyyyMMddHmm") + "-" + selectedItem + "-" + args.FileName;

    string zipFileName = "";

    string mypath = Path.Combine(folder, fileName);

    string extension = Path.GetExtension(mypath);

    if (extension == ".pdf" || extension == ".jpg" || extension == ".zip")

    {

    args.CopyTo(mypath);

    zipFileName = fileName;

     

    }

    else if(extension == ".mp3" || extension == ".m4a")

    {

    Ionic.Utils.Zip.ZipFile file = new Ionic.Utils.Zip.ZipFile(Response.OutputStream);

    file.AddFileStream(args.FileName, "", args.OpenStream());

    file.ForceNoCompression = true;

    zipFileName = fileName + ".zip";

    file.Save(mypath + ".zip");

     

    }

    else

    {

    Ionic.Utils.Zip.ZipFile file = new Ionic.Utils.Zip.ZipFile(Response.OutputStream);

    file.AddFileStream(args.FileName, "", args.OpenStream());

    zipFileName = fileName + ".zip";

    file.Save(mypath + ".zip");

    }
     
    Thanks
  •  12-16-2008, 12:05 PM 46979 in reply to 46912

    Re: Using DotNetZip to compress file during upload

    Is there some way I can copy the file back into the Uploader after it is zipped? With the current code, the file is losing the permissions that the Uploader creates from the upload of the original file. Everything works fine unless I try to zip the file.
    Thanks
  •  12-16-2008, 6:29 PM 46996 in reply to 46979

    Re: Using DotNetZip to compress file during upload

    I found out what the problem was. The DotNetZip copies the file to a temporary folder that .net doesn't have permissions to so the permissions don't get replicated correctly. Once I changed the temp folder to the one I use for the non-zipped files with AjaxUploader, the files zipped up fine.
  •  12-18-2008, 8:59 AM 47083 in reply to 46996

    Re: Using DotNetZip to compress file during upload

    Hi,
     
    That's great.
    Have you fixed your issue?
     
    Regards,
    Terry
View as RSS news feed in XML