コード例 #1
0
 public static string UnzipFile(string fileName, string DestFolder, PortalSettings settings)
 {
     ZipInputStream objZipInputStream = null;
     string strMessage = "";
     try
     {
         int FolderPortalId = GetFolderPortalId(settings);
         bool isHost = settings.ActiveTab.ParentId == settings.SuperTabId;
         PortalController objPortalController = new PortalController();
         CommonLibrary.Services.FileSystem.FolderController objFolderController = new CommonLibrary.Services.FileSystem.FolderController();
         CommonLibrary.Services.FileSystem.FileController objFileController = new CommonLibrary.Services.FileSystem.FileController();
         string sourceFolderName = Globals.GetSubFolderPath(fileName, FolderPortalId);
         string sourceFileName = GetFileName(fileName);
         CommonLibrary.Services.FileSystem.FolderInfo folder = objFolderController.GetFolder(FolderPortalId, sourceFolderName, false);
         CommonLibrary.Services.FileSystem.FileInfo file = objFileController.GetFile(sourceFileName, FolderPortalId, folder.FolderID);
         int storageLocation = folder.StorageLocation;
         ZipEntry objZipEntry;
         string strFileName = "";
         string strExtension;
         try
         {
             objZipInputStream = new ZipInputStream(GetFileStream(file));
         }
         catch (Exception ex)
         {
             return ex.Message;
         }
         ArrayList sortedFolders = new ArrayList();
         objZipEntry = objZipInputStream.GetNextEntry();
         while (objZipEntry != null)
         {
             if (objZipEntry.IsDirectory)
             {
                 try
                 {
                     sortedFolders.Add(objZipEntry.Name.ToString());
                 }
                 catch (Exception ex)
                 {
                     objZipInputStream.Close();
                     return ex.Message;
                 }
             }
             objZipEntry = objZipInputStream.GetNextEntry();
         }
         sortedFolders.Sort();
         foreach (string s in sortedFolders)
         {
             try
             {
                 AddFolder(settings, DestFolder, s.ToString(), storageLocation);
             }
             catch (Exception ex)
             {
                 return ex.Message;
             }
         }
         objZipInputStream = new ZipInputStream(GetFileStream(file));
         objZipEntry = objZipInputStream.GetNextEntry();
         while (objZipEntry != null)
         {
             if (!objZipEntry.IsDirectory)
             {
                 if (objPortalController.HasSpaceAvailable(FolderPortalId, objZipEntry.Size))
                 {
                     strFileName = Path.GetFileName(objZipEntry.Name);
                     if (!String.IsNullOrEmpty(strFileName))
                     {
                         strExtension = Path.GetExtension(strFileName).Replace(".", "");
                         if (("," + Host.FileExtensions.ToLower()).IndexOf("," + strExtension.ToLower()) != 0 || isHost)
                         {
                             try
                             {
                                 string folderPath = System.IO.Path.GetDirectoryName(DestFolder + objZipEntry.Name.Replace("/", "\\"));
                                 DirectoryInfo Dinfo = new DirectoryInfo(folderPath);
                                 if (!Dinfo.Exists)
                                 {
                                     AddFolder(settings, DestFolder, objZipEntry.Name.Substring(0, objZipEntry.Name.Replace("/", "\\").LastIndexOf("\\")));
                                 }
                                 string zipEntryFileName = DestFolder + objZipEntry.Name.Replace("/", "\\");
                                 strMessage += AddFile(FolderPortalId, objZipInputStream, zipEntryFileName, "", objZipEntry.Size, Globals.GetSubFolderPath(zipEntryFileName, settings.PortalId), false, false);
                             }
                             catch (Exception ex)
                             {
                                 if (objZipInputStream != null)
                                 {
                                     objZipInputStream.Close();
                                 }
                                 return ex.Message;
                             }
                         }
                         else
                         {
                             strMessage += "<br>" + string.Format(Localization.GetString("RestrictedFileType"), strFileName, Host.FileExtensions.Replace(",", ", *."));
                         }
                     }
                 }
                 else
                 {
                     strMessage += "<br>" + string.Format(Localization.GetString("DiskSpaceExceeded"), strFileName);
                 }
             }
             objZipEntry = objZipInputStream.GetNextEntry();
         }
     }
     finally
     {
         if (objZipInputStream != null)
         {
             objZipInputStream.Close();
             objZipInputStream.Dispose();
         }
     }
     return strMessage;
 }
コード例 #2
0
        private static string CreateFile(string RootPath, string FileName, long ContentLength, string ContentType, Stream InputStream, string NewFileName, bool Unzip)
        {
            // Obtain PortalSettings from Current Context
            PortalSettings settings = PortalController.GetCurrentPortalSettings();
            int PortalId = GetFolderPortalId(settings);
            bool isHost = (bool)(settings.ActiveTab.ParentId == settings.SuperTabId ? true : false);

            PortalController objPortalController = new PortalController();
            string strMessage = "";
            string strFileName = FileName;
            if (NewFileName != Null.NullString) strFileName = NewFileName;
            strFileName = RootPath + Path.GetFileName(strFileName);
            string strExtension = Path.GetExtension(strFileName).Replace(".", "");
            string strFolderpath = Globals.GetSubFolderPath(strFileName, PortalId);

            CommonLibrary.Services.FileSystem.FolderController objFolders = new CommonLibrary.Services.FileSystem.FolderController();
            CommonLibrary.Services.FileSystem.FolderInfo objFolder = objFolders.GetFolder(PortalId, strFolderpath, false);

            if (FolderPermissionController.CanAdminFolder(objFolder))
            {
                if (objPortalController.HasSpaceAvailable(PortalId, ContentLength))
                {
                    if (("," + Host.FileExtensions.ToUpper()).IndexOf("," + strExtension.ToUpper()) != -1 || isHost)
                    {
                        //Save Uploaded file to server
                        try
                        {
                            strMessage += AddFile(PortalId, InputStream, strFileName, ContentType, ContentLength, strFolderpath, true, true);

                            //Optionally Unzip File?
                            if (Path.GetExtension(strFileName).ToLower() == ".zip" & Unzip == true)
                            {
                                strMessage += UnzipFile(strFileName, RootPath, settings);
                            }
                        }
                        catch (Exception exc)
                        {
                            // save error - can happen if the security settings are incorrect on the disk
                            strMessage += "<br>" + string.Format(Localization.GetString("SaveFileError"), strFileName);
                            exc.ToString();
                        }
                    }
                    else
                    {
                        // restricted file type
                        strMessage += "<br>" + string.Format(Localization.GetString("RestrictedFileType"), strFileName, Host.FileExtensions.Replace(",", ", *."));
                    }
                }
                else
                {
                    // file too large
                    strMessage += "<br>" + string.Format(Localization.GetString("DiskSpaceExceeded"), strFileName);
                }
            }
            else
            {
                // insufficient folder permission in the application
                strMessage += "<br>" + string.Format(Localization.GetString("InsufficientFolderPermission"), strFolderpath);
            }

            return strMessage;
        }