download UploadZipFile.aspx.cs
Language: C#
LOC: 63
Project Info
SharePoint Zip(SharepointZip)
Server: CodePlex1
Type: svn
....ListActions.DownloadAsZip\
   ...ns.DownloadAsZip.csproj
   ...loadAsZip.csproj.vspscc
   DownloadZipFile.aspx
   DownloadZipFile.aspx.cs
   DownloadZipFileResult.aspx
   ...adZipFileResult.aspx.cs
   DownloadZipFileStatus.aspx
   ...adZipFileStatus.aspx.cs
   DownloadZipFolder.aspx
   DownloadZipFolder.aspx.cs
   elements.xml
   feature.xml
   manifest.xml
   RingBuffer.cs
   UploadZipFile.aspx
   UploadZipFile.aspx.cs
   Utilities.cs
   WSP.ddf
   ZipBase.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace BlackBlade.ListActions.DownloadAsZip
{
    public class UploadZipFile : Microsoft.SharePoint.WebPartPages.WebPartPage
    {
        protected FileUpload FileUpload1;
        protected CheckBox chkKeepOriginalZip;
        protected CheckBox chkCreateFolderForExtractedFiles;
        protected Button btnOk;
        protected Button btnCancel;
        protected Label lblMessage;

        private string strSourceUrl;

        protected override void OnInit(EventArgs e)
        {
            this.Load += new EventHandler(UploadZipFile_Load);
            this.btnOk.Click += new EventHandler(btnOk_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);

            base.OnInit(e);
        }

        void UploadZipFile_Load(object sender, EventArgs e)
        {
            //get the source Url
            this.strSourceUrl = this.Request["Source"];
        }

        void btnOk_Click(object sender, EventArgs e)
        {
            //check to see if there was an uploaded file
            if (!this.FileUpload1.HasFile)
            {
                //display a messge to the user
                this.lblMessage.Text = "You have not uploaded a file. Please select a Zip file to upload before pressing Ok.";
                this.lblMessage.Visible = true; 

                return;
            }

            string tempFileName = Utilities.GetBaseWorkingPath() + "\\" + Guid.NewGuid() + "\\" + this.FileUpload1.FileName;

            //ensure that the temporary directory exists
            System.IO.FileInfo fi = new System.IO.FileInfo(tempFileName);
            if (!fi.Directory.Exists)
                fi.Directory.Create();

            //save the zip file to the new location
            this.FileUpload1.SaveAs(tempFileName);

            //set the root SharePoint folder to start uploading files into
            SPWeb temp = SPContext.Current.Web;

            //The page will be passed a Source Url parameter. This may have the RootFolder parameter 
            // enbeded inside the Url. If the RootFolder parameter is present, use it to find the 
            // sub folder in the document library into which the files need to be uplaoded. If the 
            // RootFolder parameter is not present, then upload the files into the document libray's
            // root folder
            SPFolder uploadFolder;
            if(this.strSourceUrl.Contains("RootFolder="))
            {
                System.Text.RegularExpressions.Regex regExRootFolder = new System.Text.RegularExpressions.Regex("RootFolder=([^&#]*)");
                uploadFolder = temp.GetFolder(regExRootFolder.Match(this.strSourceUrl).Groups[1].Value);
            }
            else 
                uploadFolder = temp.Lists[new Guid(this.Request["ListID"])].RootFolder;

            //create a new zubfolder in SharePoint based on the name of the zip file, if requested
            // by the user
            if (this.chkCreateFolderForExtractedFiles.Checked)
                uploadFolder = uploadFolder.SubFolders.Add(uploadFolder.Url + "/" + fi.Name.Remove(fi.Name.Length - fi.Extension.Length));

            //upload the uncompressed files into SharePoint
            ZipBase zip = new ZipBase();
            zip.UnzipDiskFile(uploadFolder, tempFileName, fi.Directory.FullName);

            //if the option to keep the zip file was selected, add the zip file to the document library
            if (this.chkKeepOriginalZip.Checked)
                uploadFolder.Files.Add(uploadFolder.Url + "/" + fi.Name, fi.OpenRead(), true);

            //finally, redirect to the source page
            this.Response.Redirect(this.strSourceUrl);
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            this.Response.Redirect(this.strSourceUrl);
        }
    }
}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us