/// ----------------------------------------------------------------------------- /// <summary> /// Serializes all Files /// </summary> /// <param name="objportal">Portal to serialize</param> /// <param name="folderPath">The folder containing the files</param> /// <remarks> /// The serialization uses the xml attributes defined in FileInfo class. /// </remarks> /// <history> /// [cnurse] 11/08/2004 Created /// [cnurse] 05/20/2004 Extracted adding of file to zip to new FileSystemUtils method /// </history> /// ----------------------------------------------------------------------------- private void SerializeFiles(XmlWriter writer, PortalInfo objportal, string folderPath, ref ZipOutputStream zipFile) { var folderManager = FolderManager.Instance; var objFolder = folderManager.GetFolder(objportal.PortalID, folderPath); writer.WriteStartElement("files"); foreach (FileInfo objFile in folderManager.GetFiles(objFolder)) { //verify that the file exists on the file system var filePath = objportal.HomeDirectoryMapPath + folderPath + objFile.FileName; if (File.Exists(filePath)) { writer.WriteStartElement("file"); writer.WriteElementString("contenttype", objFile.ContentType); writer.WriteElementString("extension", objFile.Extension); writer.WriteElementString("filename", objFile.FileName); writer.WriteElementString("height", objFile.Height.ToString()); writer.WriteElementString("size", objFile.Size.ToString()); writer.WriteElementString("width", objFile.Width.ToString()); writer.WriteEndElement(); FileSystemUtils.AddToZip(ref zipFile, filePath, objFile.FileName, folderPath); } } writer.WriteEndElement(); }
private void AddFilesToZip(ZipOutputStream stream, IDictionary <string, InstallFile> files, string basePath) { foreach (InstallFile packageFile in files.Values) { string filepath; if (string.IsNullOrEmpty(basePath)) { filepath = Path.Combine(Globals.ApplicationMapPath, packageFile.FullName); } else { filepath = Path.Combine(Path.Combine(Globals.ApplicationMapPath, basePath), packageFile.FullName.Replace(basePath + "\\", "")); } if (File.Exists(filepath)) { string packageFilePath = packageFile.Path; if (!string.IsNullOrEmpty(basePath)) { packageFilePath = packageFilePath.Replace(basePath + "\\", ""); } FileSystemUtils.AddToZip(ref stream, filepath, packageFile.Name, packageFilePath); Log.AddInfo(string.Format(Util.WRITER_SavedFile, packageFile.FullName)); } } }
/// <summary> /// Serializes all Files /// </summary> /// <param name="xmlTemplate">Reference to XmlDocument context</param> /// <param name="nodeFiles">Node to add the serialized objects</param> /// <param name="objportal">Portal to serialize</param> /// <param name="folderPath">The folder containing the files</param> /// <param name="zipFile"></param> /// <remarks> /// The serialization uses the xml attributes defined in FileInfo class. /// </remarks> /// <history> /// [cnurse] 11/08/2004 Created /// [cnurse] 05/20/2004 Extracted adding of file to zip to new FileSystemUtils method /// </history> public void SerializeFiles(XmlDocument xmlTemplate, XmlNode nodeFiles, PortalInfo objportal, string folderPath, ref ZipOutputStream zipFile) { FolderController objFolders = new FolderController(); FolderInfo objFolder = objFolders.GetFolder(objportal.PortalID, folderPath); ArrayList arrFiles = FileSystemUtils.GetFilesByFolder(objportal.PortalID, objFolder.FolderID); XmlSerializer xser = new XmlSerializer(typeof(FileInfo)); foreach (FileInfo objFile in arrFiles) { // verify that the file exists on the file system string filePath = objportal.HomeDirectoryMapPath + folderPath + objFile.FileName; if (File.Exists(filePath)) { StringWriter sw = new StringWriter(); xser.Serialize(sw, objFile); //Add node to template XmlDocument xmlFile = new XmlDocument(); xmlFile.LoadXml(sw.GetStringBuilder().ToString()); XmlNode nodeFile = xmlFile.SelectSingleNode("file"); nodeFile.Attributes.Remove(nodeFile.Attributes["xmlns:xsd"]); nodeFile.Attributes.Remove(nodeFile.Attributes["xmlns:xsi"]); nodeFiles.AppendChild(xmlTemplate.ImportNode(nodeFile, true)); FileSystemUtils.AddToZip(ref zipFile, filePath, objFile.FileName, folderPath); } } }
private void CreateZipFile(string zipFileName, string Folder) { //string basePath = Server.MapPath(OpenContentUtils.GetSiteTemplateFolder(PortalSettings)); //string packageFilePath = Folder.Replace(basePath, ""); //zipFileName = basePath + zipFileName + ".zip"; int CompressionLevel = 9; var zipFile = new System.IO.FileInfo(zipFileName); //string ZipFileShortName = zipFile.Name; FileStream strmZipFile = null; //Log.StartJob(Util.WRITER_CreatingPackage); try { //Log.AddInfo(string.Format(Util.WRITER_CreateArchive, ZipFileShortName)); strmZipFile = File.Create(zipFileName); ZipOutputStream strmZipStream = null; try { strmZipStream = new ZipOutputStream(strmZipFile); strmZipStream.SetLevel(CompressionLevel); foreach (var item in Directory.GetFiles(Folder)) { FileSystemUtils.AddToZip(ref strmZipStream, Path.GetFullPath(item), Path.GetFileName(item), ""); } } catch (Exception ex) { DotNetNuke.Services.Exceptions.Exceptions.LogException(ex); //Log.AddFailure(string.Format(Util.WRITER_SaveFileError, ex)); } finally { if (strmZipStream != null) { strmZipStream.Finish(); strmZipStream.Close(); } } //Log.EndJob(Util.WRITER_CreatedPackage); WriteFileToHttpContext(zipFileName, ContentDisposition.Attachment); } catch (Exception ex) { DotNetNuke.Services.Exceptions.Exceptions.LogException(ex); //Log.AddFailure(string.Format(Util.WRITER_SaveFileError, ex)); } finally { if (strmZipFile != null) { strmZipFile.Close(); } } }
public void AddToZip_Should_Able_To_Add_Multiple_Files() { // Action this.DeleteZippedFiles(); var zipFilePath = Path.Combine(Globals.ApplicationMapPath, $"Test{Guid.NewGuid().ToString().Substring(0, 8)}.zip"); var files = Directory.GetFiles(Globals.ApplicationMapPath, "*.*", SearchOption.TopDirectoryOnly); using (var stream = File.Create(zipFilePath)) { var zipStream = new ZipOutputStream(stream); zipStream.SetLevel(9); foreach (var file in files) { var fileName = Path.GetFileName(file); FileSystemUtils.AddToZip(ref zipStream, file, fileName, string.Empty); } zipStream.Finish(); zipStream.Close(); } // Assert var destPath = Path.Combine(Globals.ApplicationMapPath, Path.GetFileNameWithoutExtension(zipFilePath)); if (!Directory.Exists(destPath)) { Directory.CreateDirectory(destPath); } try { using (var stream = File.OpenRead(zipFilePath)) { var zipStream = new ZipInputStream(stream); FileSystemUtils.UnzipResources(zipStream, destPath); zipStream.Close(); } var unZippedFiles = Directory.GetFiles(destPath, "*.*", SearchOption.TopDirectoryOnly); Assert.AreEqual(files.Length, unZippedFiles.Length); } finally { this.DeleteZippedFiles(); this.DeleteUnzippedFolder(destPath); } }
private void AddSourceFiles(DirectoryInfo folder, string fileType, ref ZipOutputStream resourcesFile) { //Get the Source Files in the folder FileInfo[] sourceFiles = folder.GetFiles(fileType); foreach (FileInfo sourceFile in sourceFiles) { string filePath = sourceFile.FullName; string fileName = sourceFile.Name; string folderName = folder.FullName.Replace(Folder, ""); if (folderName != "") { folderName += "\\"; } if (folderName.StartsWith("\\")) { folderName = folderName.Substring(1); } FileSystemUtils.AddToZip(ref resourcesFile, filePath, fileName, folderName); } }
private string CreateZipFile() { int CompressionLevel = 9; string ZipFileShortName = _Name; string ZipFileName = _ZipFile; if (ZipFileName == "") { ZipFileName = ZipFileShortName + ".zip"; } ZipFileName = Globals.HostMapPath + ZipFileName; FileStream strmZipFile = null; try { ProgressLog.AddInfo(string.Format(Localization.GetString("LOG.PAWriter.CreateArchive"), ZipFileShortName)); strmZipFile = File.Create(ZipFileName); ZipOutputStream strmZipStream = null; try { strmZipStream = new ZipOutputStream(strmZipFile); strmZipStream.SetLevel(CompressionLevel); foreach (PaFileInfo PaFile in _Files) { if (File.Exists(PaFile.FullName)) { FileSystemUtils.AddToZip(ref strmZipStream, PaFile.FullName, PaFile.Name, ""); ProgressLog.AddInfo(string.Format(Localization.GetString("LOG.PAWriter.SavedFile"), PaFile.Name)); } } } catch (Exception ex) { Exceptions.LogException(ex); ProgressLog.AddFailure(string.Format(Localization.GetString("LOG.PAWriter.ERROR.SavingFile"), ex)); } finally { if (strmZipStream != null) { strmZipStream.Finish(); strmZipStream.Close(); } } } catch (Exception ex) { Exceptions.LogException(ex); ProgressLog.AddFailure(string.Format(Localization.GetString("LOG.PAWriter.ERROR.SavingFile"), ex)); } finally { if (strmZipFile != null) { strmZipFile.Close(); } } return(ZipFileName); }