public override void Build() { using (var zip = new Ionic.Zip.ZipFile()) { zip.AddDirectoryByName("assets"); zip.AddDirectory(ResPath, "assets"); zip.Save(Path + "\\resources.zip"); } }
public void createDirectory(String fullPath) { zipFile.Dispose(); try { if (!fullPath.EndsWith("/") && !fullPath.EndsWith("\\")) { fullPath += '/'; } using (Ionic.Zip.ZipFile ionicZip = new Ionic.Zip.ZipFile(resourceLocation)) { if (!ionicZip.ContainsEntry(fullPath)) { ionicZip.AddDirectoryByName(fullPath); } ionicZip.Save(); } } finally { zipFile = new ZipFile(resourceLocation); } }
protected void ButtonDownload_Click(object sender, EventArgs e) { string[] paths = HiddenFieldDownload.Value.Split('|'); if (paths.Length > 0 && !String.IsNullOrEmpty(paths[0])) { byte[] downloadFile = null; string downloadFileName = String.Empty; if (paths.Length == 1 && ((File.GetAttributes(Request.MapPath(paths[0])) & FileAttributes.Directory) != FileAttributes.Directory)) { //Single path string path = Request.MapPath(paths[0]); downloadFile = File.ReadAllBytes(path); downloadFileName = new FileInfo(path).Name; } else { //Multiple paths List<FileInfo> fileInfos = new List<FileInfo>(); List<DirectoryInfo> emptyDirectoryInfos = new List<DirectoryInfo>(); foreach (string relativePath in paths) { string path = Request.MapPath(relativePath); FileAttributes attr = File.GetAttributes(path); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { DirectoryInfo di = new DirectoryInfo(path); //All the files recursively within a directory FileInfo[] pathFileInfos = di.GetFiles("*", SearchOption.AllDirectories); if (pathFileInfos.Length > 0) { fileInfos.AddRange(pathFileInfos); } else { emptyDirectoryInfos.Add(di); } //All the folders recursively within a directory DirectoryInfo[] pathDirectoryInfos = di.GetDirectories("*", SearchOption.AllDirectories); foreach (DirectoryInfo pathDirectoryInfo in pathDirectoryInfos) { if (pathDirectoryInfo.GetFiles().Length == 0) { emptyDirectoryInfos.Add(pathDirectoryInfo); } } } else { fileInfos.Add(new FileInfo(path)); } } //Needed for constructing the directory hierarchy by the DotNetZip requirements string currentFolder = Request.MapPath(RadFileExplorerAlbum.CurrentFolder); string zipFileName = Path.Combine(Path.GetTempPath(), String.Format("{0}.zip", new Guid())); using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(zipFileName)) { foreach (FileInfo fileInfo in fileInfos) { zip.AddFile(fileInfo.FullName, !fileInfo.Directory.FullName.Equals(currentFolder, StringComparison.InvariantCultureIgnoreCase) ? fileInfo.Directory.FullName.Substring(currentFolder.Length + 1) : String.Empty); } foreach (DirectoryInfo directoryInfo in emptyDirectoryInfos) { zip.AddDirectoryByName( directoryInfo.FullName.Substring(currentFolder.Length + 1)); } zip.TempFileFolder = Path.GetTempPath(); zip.Save(); } downloadFile = File.ReadAllBytes(zipFileName); File.Delete(zipFileName); downloadFileName = "Combined.zip"; } Response.Clear(); Response.AppendHeader("Content-Disposition", String.Format("attachment; filename=\"{0}\"", downloadFileName)); Response.ContentType = String.Format("application/{0}", downloadFileName); Response.BinaryWrite(downloadFile); Response.End(); } }
protected void ButtonDownload_Click(object sender, EventArgs e) { string[] paths = HiddenFieldDownload.Value.Split('|'); if (paths.Length > 0 && !String.IsNullOrEmpty(paths[0])) { byte[] downloadFile = null; string downloadFileName = String.Empty; if (paths.Length == 1 && ((File.GetAttributes(Request.MapPath(paths[0])) & FileAttributes.Directory) != FileAttributes.Directory)) { //Single path string path = Request.MapPath(paths[0]); downloadFile = File.ReadAllBytes(path); downloadFileName = new FileInfo(path).Name; } else { //Multiple paths List <FileInfo> fileInfos = new List <FileInfo>(); List <DirectoryInfo> emptyDirectoryInfos = new List <DirectoryInfo>(); foreach (string relativePath in paths) { string path = Request.MapPath(relativePath); FileAttributes attr = File.GetAttributes(path); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { DirectoryInfo di = new DirectoryInfo(path); //All the files recursively within a directory FileInfo[] pathFileInfos = di.GetFiles("*", SearchOption.AllDirectories); if (pathFileInfos.Length > 0) { fileInfos.AddRange(pathFileInfos); } else { emptyDirectoryInfos.Add(di); } //All the folders recursively within a directory DirectoryInfo[] pathDirectoryInfos = di.GetDirectories("*", SearchOption.AllDirectories); foreach (DirectoryInfo pathDirectoryInfo in pathDirectoryInfos) { if (pathDirectoryInfo.GetFiles().Length == 0) { emptyDirectoryInfos.Add(pathDirectoryInfo); } } } else { fileInfos.Add(new FileInfo(path)); } } //Needed for constructing the directory hierarchy by the DotNetZip requirements string currentFolder = Request.MapPath(RadFileExplorerAlbum.CurrentFolder); string zipFileName = Path.Combine(Path.GetTempPath(), String.Format("{0}.zip", new Guid())); using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(zipFileName)) { foreach (FileInfo fileInfo in fileInfos) { zip.AddFile(fileInfo.FullName, !fileInfo.Directory.FullName.Equals(currentFolder, StringComparison.InvariantCultureIgnoreCase) ? fileInfo.Directory.FullName.Substring(currentFolder.Length + 1) : String.Empty); } foreach (DirectoryInfo directoryInfo in emptyDirectoryInfos) { zip.AddDirectoryByName( directoryInfo.FullName.Substring(currentFolder.Length + 1)); } zip.TempFileFolder = Path.GetTempPath(); zip.Save(); } downloadFile = File.ReadAllBytes(zipFileName); File.Delete(zipFileName); downloadFileName = "Combined.zip"; } Response.Clear(); Response.AppendHeader("Content-Disposition", String.Format("attachment; filename=\"{0}\"", downloadFileName)); Response.ContentType = String.Format("application/{0}", downloadFileName); Response.BinaryWrite(downloadFile); Response.End(); } }