public MemoryStream ExportApp(bool includeContentGroups = false, bool resetAppGuid = false)
{
// generate the XML
var xmlExport = GenerateExportXml(includeContentGroups, resetAppGuid);
#region Copy needed files to temporary directory
var messages = new List<ExportImportMessage>();
var randomShortFolderName = Guid.NewGuid().ToString().Substring(0, 4);
var temporaryDirectoryPath = HttpContext.Current.Server.MapPath(Path.Combine(Settings.TemporaryDirectory, randomShortFolderName));
if (!Directory.Exists(temporaryDirectoryPath))
Directory.CreateDirectory(temporaryDirectoryPath);
AddInstructionsToPackageFolder(temporaryDirectoryPath);
var tempDirectory = new DirectoryInfo(temporaryDirectoryPath);
var appDirectory = tempDirectory.CreateSubdirectory("Apps/" + _app.Folder + "/");
var sexyDirectory = appDirectory.CreateSubdirectory(_zipFolderForAppStuff);
var portalFilesDirectory = appDirectory.CreateSubdirectory(_zipFolderForPortalFiles);
// Copy app folder
if (Directory.Exists(_app.PhysicalPath))
{
FileManager.CopyAllFiles(sexyDirectory.FullName, false, messages);
}
// Copy PortalFiles
foreach (var file in xmlExport.ReferencedFiles)
{
var portalFilePath = Path.Combine(portalFilesDirectory.FullName, Path.GetDirectoryName(file.RelativePath.Replace('/','\\')));
if (!Directory.Exists(portalFilePath))
Directory.CreateDirectory(portalFilePath);
if (File.Exists(file.PhysicalPath))
{
var fullPath = Path.Combine(portalFilesDirectory.FullName, file.RelativePath.Replace('/', '\\'));
try
{
File.Copy(file.PhysicalPath, fullPath);
}
catch (Exception e)
{
throw new Exception("Error on " + fullPath + " (" + fullPath.Length + ")", e);
}
}
}
#endregion
// Save export xml
string xml = xmlExport.GenerateNiceXml();
File.AppendAllText(Path.Combine(appDirectory.FullName, _AppXmlFileName), xml);
// Zip directory and return as stream
var stream = new MemoryStream();
var zipStream = new ZipOutputStream(stream);
zipStream.SetLevel(6);
ZipFolder(tempDirectory.FullName + "\\", tempDirectory.FullName + "\\", zipStream);
zipStream.Finish();
tempDirectory.Delete(true);
return stream;
}