public static bool TryCreateWotMod(string targetPath, string rootDirectory) { using (var zip = ICSharpCode.SharpZipLib.Zip.ZipFile.Create(targetPath)) { var root = new DirectoryInfo(rootDirectory); rootDirectory = root.FullName; zip.BeginUpdate(); //TODO refactor this crap - prefix path param? string prefix = $"{root.Name}"; if (root.Name != "res") { zip.AddDirectory("res"); zip.AddDirectory($"res/{root.Name}"); prefix = $"res/{root.Name}"; } TraverseDirectory(root, i => { var relativePath = $"{prefix}/{MakeRelativePath(rootDirectory, i.FullName)}"; zip.AddDirectory(relativePath); }, i => { var relativePath = $"{prefix}/{MakeRelativePath(rootDirectory, i.FullName)}"; var dataSource = new ICSharpCode.SharpZipLib.Zip.StaticDiskDataSource(i.FullName); zip.Add(dataSource, relativePath, CompressionMethod.Stored); }); zip.CommitUpdate(); } return(true); }
// -------------------- Download -------------------- public FileResult GetAlbumInArchive(long idAlbum) { var album = AlbumRepository.Get(idAlbum); var zipName = string.Format("Martin S. - {0}.zip", album.Name); var zipPath = Path.Combine(Server.MapPath("~/Content/Song"), zipName); if (FileStandard.Exists(zipPath)) { FileStandard.Delete(zipPath); } var zipFile = ZipFile.Create(zipPath); zipFile.BeginUpdate(); var count = 1; // Add song in archive foreach (var song in album.Songs) { var globalMp3Path = Server.MapPath("~/" + FileHelper.PathToSong(album.Name, song.Mp3FileName)); var dataSource = new StaticDiskDataSource(globalMp3Path); var cyrillicName = song.Name; var romanName = cyrillicName.FromCyrillicToRomanAlphabet(); var songFileName = string.Format("{0}. Martin S. - {1}.mp3", count++, romanName); zipFile.Add(dataSource, songFileName); } // Add readme in archive var readmePath = Server.MapPath("~/" + FileHelper.PathToSong(album.Name, "readme.txt")); FileStandard.WriteAllText(readmePath, album.ReadmeInArchive); var readmeDataSource = new StaticDiskDataSource(readmePath); zipFile.Add(readmeDataSource, Path.GetFileName(readmePath)); // Add cover in archive var coverPath = Server.MapPath("~/" + FileHelper.PathToCoverForAlbum(album.CoverFileName)); var coverDataSource = new StaticDiskDataSource(coverPath); zipFile.Add(coverDataSource, Path.GetFileName(coverPath).FromCyrillicToRomanAlphabet()); zipFile.CommitUpdate(); zipFile.Close(); var fileBytes = FileStandard.ReadAllBytes(zipPath); var fileName = Path.GetFileName(zipPath); return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Zip, fileName); }