简体   繁体   中英

Trying to dynamically create a zip file causes the zip file to be corrupted

So i wrote this program for my company that dynamically makes a zip folder in their downloads. This code worked perfectly, however, when my server was updated to windows 10, when i attempt to unzip the file, I get this error.

"Cannot complete the compressed folder"

        string[] ProductNumberAmount = prodNumber.ToString().Split(' ');
        int amountOf = ProductNumberAmount.Count();
        Response.ClearHeaders();
        Response.ClearContent();

        using (ZipFile zip = new ZipFile())
        {
            Response.ClearHeaders();
            Response.ClearContent();
            Response.Clear();

            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "attachment; filename=" + ProductNumberAmount[0] + "_" + DateTime.Now.ToString("yyyyMMdd") + ".zip");


            for (int i = 0; i < amountOf;)
            {
                string productNumberList = ProductNumberAmount[i];
                productNumberList = productNumberList.Replace("\r\n", string.Empty);
                string s = "example text";


                File.WriteAllText(@"product_detail_" + productNumberList + ".inc", s);

                zip.AddFile(@"product_detail_" + productNumberList + ".inc");
                i++;
            }


            zip.Save(Response.OutputStream);
        }
        Response.Flush();
        HttpContext.Current.Response.End();

    }

When viewing the zipped folder that was created in notepad, instead of the normal hex text that you would see, there is html text

Again, this exact code worked perfectly before the update so does this have something to do with the IIS server properties being changed. I have been working on this for a couple days and still have not had any luck with anything

Thank you all for your answers! It was an easier fix then anticipated. I set a breakpoint at

"Response.AddHeader("content-disposition", "attachment; filename=" + ProductNumberAmount[0] + "_" + DateTime.Now.ToString("yyyyMMdd") + ".zip");"

and it threw an exception showing that there was folder that needed permission set. Once i have the user "IIS_IUSRS" full permissions in the named folder, the program worked as it originally was intended.

The reason that it probably needed full permissions was because the zipped folder would go directly into the users downloaded folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM