Monday, 28 July 2014

Detectings Files before extract using Iconic zip



I am using Iconic.zip as a way of extracting files on a server but its for a theme pack. And I want to be able to check if a file exists in the zip file before unziping is this possible with the libary.


I am using the following code which works to upload and extract the files.



if (fileUploadZipFiles.HasFile)
{
string uploadedZipFile = Path.GetFileName(fileUploadZipFiles.PostedFile.FileName);
string zipFileLocation = Server.MapPath("~/Themes/" + uploadedZipFile);
fileUploadZipFiles.SaveAs(zipFileLocation);

ZipFile zipFileToExtract = ZipFile.Read(zipFileLocation);
zipFileToExtract.ExtractAll(Server.MapPath("~/Themes/" + txtFolderName.Text.ToString()), ExtractExistingFileAction.DoNotOverwrite);
gridviewExtractedFiles.DataSource = zipFileToExtract.Entries;
gridviewExtractedFiles.DataBind();
lblMessage.Text = "Zip file extracted successfully and containes following files";
}


The second part of this question is that I am using master pages for my themes how would i go about setting the active theme to the one the user wants to make acitve. I am sure I have to have my own base page.


I found the anser to my first part of question was to use the following.



if (fileUploadZipFiles.HasFile)
{
string uploadedZipFile = Path.GetFileName(fileUploadZipFiles.PostedFile.FileName);
string zipFileLocation = Server.MapPath("~/Themes/" + uploadedZipFile);
fileUploadZipFiles.SaveAs(zipFileLocation);

ZipFile zipFileToExtract = ZipFile.Read(zipFileLocation);
var result = zipFileToExtract.Any(entry => entry.FileName.EndsWith("site.master"));
if (result == true)
{

zipFileToExtract.ExtractAll(Server.MapPath("~/Themes/" + txtFolderName.Text.ToString()), ExtractExistingFileAction.DoNotOverwrite);
gridviewExtractedFiles.DataSource = zipFileToExtract.Entries;
gridviewExtractedFiles.DataBind();
lblMessage.Text = "Zip file extracted successfully and containes following files";
}
else
lblMessage.Text = "Not a vlaid theme file.";

}


As suggested here is the answer i used linq to check the existence of the file.



protected void btnExtractZipFiles_Click(object sender, EventArgs e)
{
if (txtFolderName.Text == "")
{
lblMessage.Text = "A Folder Name must be specified";
}else

if (fileUploadZipFiles.HasFile)
{

string uploadedZipFile = Path.GetFileName(fileUploadZipFiles.PostedFile.FileName);
string zipFileLocation = Server.MapPath("~/Themes/" + uploadedZipFile);
fileUploadZipFiles.SaveAs(zipFileLocation);

ZipFile zipFileToExtract = ZipFile.Read(zipFileLocation);
var result = zipFileToExtract.Any(entry => entry.FileName.EndsWith("default.Master"));
if (result == true)
{

zipFileToExtract.ExtractAll(Server.MapPath("~/Themes/" + txtFolderName.Text.ToString()), ExtractExistingFileAction.DoNotOverwrite);
gridviewExtractedFiles.DataSource = zipFileToExtract.Entries;
gridviewExtractedFiles.DataBind();
lblMessage.Text = "Zip file extracted successfully and containes following files";
SetConfiguration(); //parse the configuration file
}
else
lblMessage.Text = "Not a vlaid theme file.";

}

}

No comments:

Post a Comment