locked
Extract the contents of zip file in the app_data folder RRS feed

  • Question

  • User751840860 posted

    in my mvc application, user uploads the .zip file into the app_data folder ( using the file upload control using httppostedfilebase) and then, I want to extract 

    the same into the app_data folder and then read the contents of the files.

    as a first step, i i tried to extract the files, but thats not happening.

    in my case app_data is the folder, the user will upload into.

    then, once the postedfile.saveas() is called, i wanna  extract the content into the same folder.( this is not happening) 

    i tried with below code, but it throws an exception, when entry.fullname is called, so, currently i am not using this ziparchiventry class

    foreach(ZipArchiveEntry entry in z.entries) //z is a zip file open in ZipArchiveMode.Read
    {
            entry.ExtractToFile(entry.FullName);
    }
    [HttpPost]
            public ActionResult Index(HttpPostedFileBase postedFiles)
            {
                string pathforsaving = string.Empty;
    
                try
                {
                    pathforsaving = Server.MapPath(@"~/App_Data/");
    
                    if (postedFiles != null)
                    {
                        string sfileName = Path.GetFileName(postedFiles.FileName);
                        string uploadFilePathandname = Path.Combine(pathforsaving,sfileName);
    
                        postedFiles.SaveAs(uploadFilePathandname);
    string Zipfilecandidate = pathforsaving + sfileName;
    string extracttoPath = pathforsaving;
    
    ///calal this unzip-extract method
    UnzipReadPostedfile(postedFiles,Zipfilecandidate, extracttoPath);
    
    }
    
    .....
    ....
    }
    
    private void UnzipReadPostedfile(HttpPostedFileBase postedFiles, string mZipfileCandidate, string extractToPathAppdata)
            {
                try
                {
                    //string startPath = @"c:\example\start";
                    //string zipPath = @"c:\example\result.zip";
                    //string extractPath = @"c:\example\extract";
    
                    string startPath = @extractToPathAppdata;
                    string zipFilename = mZipfileCandidate;
                    string extractPath = @"c:\example\extract";
    
                    //System.IO.Compression.ZipFile.CreateFromDirectory(startPath, zipFilename);
                    System.IO.Compression.ZipFile.ExtractToDirectory(zipFilename, zipFilename);
                }
                catch(Exception expo)
                {
                    string errmessage1 = expo.Message + "__" + expo.StackTrace;
                }
    
    
     


                   

    Tuesday, May 14, 2019 12:29 PM

All replies

  • User-821857111 posted

    but it throws an exception
    Can you provide details of the exception?

    Tuesday, May 14, 2019 3:12 PM
  • User753101303 posted

    Hi,

    More likely a path or permission issue but exceptions are precisely intended to tell you what is the wrong thing that happens rather than having to guess.

    Else we would have just a single GuessWhatHappenedException ;-)

    Friday, July 5, 2019 11:35 AM