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;
}