1

I am trying to save a zip file with a web api which is throwing exception :

Access to the path 'c:\windows\system32\inetsrv\DotNetZip-uxoebj5p.tmp' is 
denied.   at System.IO.__Error.WinIOError(Int32 errorCode, String 
maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, 
Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, 
FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean 
bFromProxy, Boolean useLongPath, Boolean checkHost)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy at System.IO.FileStream..ctor(String path, FileMode mode) at Ionic.Zip.SharedUtilities.CreateAndOpenUniqueTempFile(String dir, Stream& fs, String& filename) at Ionic.Zip.ZipFile.get_WriteStream() at Ionic.Zip.ZipFile.Save() at Archnies.Archnies.DownloadFile(String url)

Code :

            Logger.LogMessage("Downloading File From URL " + url);
            // Construct HTTP request to get the file
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.CookieContainer = new System.Net.CookieContainer();

            for (int i = 0; i <= driver.Manage().Cookies.AllCookies.Count - 1; i++)
            {
                System.Net.Cookie ck = new System.Net.Cookie(driver.Manage().Cookies.AllCookies[i].Name, driver.Manage().Cookies.AllCookies[i].Value, driver.Manage().Cookies.AllCookies[i].Path, driver.Manage().Cookies.AllCookies[i].Domain);
                httpRequest.CookieContainer.Add(ck);
            }
            String userAgent = (String)((IJavaScriptExecutor)driver).ExecuteScript("return navigator.userAgent;");
            httpRequest.Accept = "text/html, application/xhtml+xml, */*";
            httpRequest.UserAgent = userAgent;// "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";

            //HttpStatusCode responseStatus;
            // Get back the HTTP response for web server
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            Stream httpResponseStream = httpResponse.GetResponseStream();

            //  Define buffer and buffer size
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int bytesRead = 0;

            // Read from response and write to file
            string userProfile = Configurations.AppDataPath; //Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            byte[] b = null;

            using (MemoryStream ms = new MemoryStream())
            {
                int count = 0;
                do
                {
                    byte[] buf = new byte[1024];
                    //count = stream.Read(buf, 0, 1024);
                    count = httpResponseStream.Read(buf, 0, 1024);
                    ms.Write(buf, 0, count);
                } while (httpResponseStream.CanRead && count > 0);//while (stream.CanRead && count > 0);
                b = ms.ToArray();
            }
            string fileName = Path.Combine(userProfile, String.Format("archaniesReport{0}.zip", new Random().Next(512365412)));
            Logger.LogMessage("File: " + fileName);
            ZipFile zip = new ZipFile();
            zip.AddEntry(fileName, b);

            zip.Save(Path.GetFileName(fileName));
            zip.Dispose();
            using (FileStream fileStream = File.Create(fileName))
            {
                while ((bytesRead = httpResponseStream.Read(buffer, 0, bufferSize)) != 0)
                {
                    fileStream.Write(buffer, 0, bytesRead);
                }
            }
            File.Copy(Path.GetFileName(fileName), fileName, true);
            return true;

I also tried

 zip.TempFileFolder = Configurations.AppDataPath;

but that doesn't seem to fix the problem.

Kindly , let me know

1 Answers1

0

You can download the file to temporary location, which you get using Path.GetTempPath() (MSDN). After the download is complete, you can move file by File.Copy(...) to user's folder and delete file from temp by File.Delete(...).

Using this approach you won't get acces denied exception.

Pavel Pája Halbich
  • 1,529
  • 2
  • 17
  • 22
  • I am trying to save the file in AppData of the IIS Directory, which it has complete permission , I believe the dotnetzip libarary is installed on windows directory, which IIS Server has not access to and thus it is throwing exception What can be the solution? – Saurabh Singh1 Jul 25 '17 at 11:35
  • So `Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)` is correct solution? – Pavel Pája Halbich Jul 25 '17 at 11:40