public void ProcessTracks(Track[] tracks) { foreach (var track in tracks) { //download the invalid track file var trackFileName = string.Format(@"{0}\{1}{2}", m_stagingDirectory, track.TrackName.Trim(), Path.GetExtension(track.TrackUrl)).Replace("+", " "); using (WebClient client = new WebClient()) { Console.WriteLine(string.Format("Downloading {0}", track.TrackUrl)); client.DownloadFile(track.TrackUrl, trackFileName); } bool slot8 = false; using (ZipArchive archive = ZipFile.OpenRead(trackFileName)) { string databaseExt = ".dx9.database"; string levelExt = ".dx9.level"; string packageExt = ".dx9.package"; string sceneExt = ".dx9.scene"; foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.Name.EndsWith(databaseExt, StringComparison.OrdinalIgnoreCase) || entry.Name.EndsWith(levelExt, StringComparison.OrdinalIgnoreCase) || entry.Name.EndsWith(packageExt, StringComparison.OrdinalIgnoreCase) || entry.Name.EndsWith(sceneExt, StringComparison.OrdinalIgnoreCase)) { int slot = TrackValidator.GetSlot(entry.Name); slot8 = slot == 8; break; } } } if (slot8) { Console.WriteLine(string.Format("Slot 8 detected. Moving {0} to zip procesing folder.", Path.GetFileName(trackFileName))); string fullFileName = string.Format("{0}/{1}", m_destinationFolder, Path.GetFileName(trackFileName)); if (File.Exists(fullFileName)) { File.Delete(fullFileName); } File.Move(trackFileName, fullFileName); } } }
private Track[] SplitTracks(Track originalTrack, string fileName) { List <Track> newTracks = new List <Track>(); using (ZipArchive archive = ZipFile.OpenRead(fileName)) { string databaseExt = ".dx9.database"; string levelExt = ".dx9.level"; string packageExt = ".dx9.package"; string sceneExt = ".dx9.scene"; Dictionary <string, List <ZipArchiveEntry> > trackBuckets = new Dictionary <string, List <ZipArchiveEntry> >(); foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.Name.EndsWith(databaseExt, StringComparison.OrdinalIgnoreCase) || entry.Name.EndsWith(levelExt, StringComparison.OrdinalIgnoreCase) || entry.Name.EndsWith(packageExt, StringComparison.OrdinalIgnoreCase) || entry.Name.EndsWith(sceneExt, StringComparison.OrdinalIgnoreCase)) { string type = TrackValidator.GetTrackType(entry.Name); int slot = TrackValidator.GetSlot(entry.Name); string key = string.Format("{0}:{1}", type, slot); if (trackBuckets.ContainsKey(key) == false) { trackBuckets[key] = new List <ZipArchiveEntry>(); } trackBuckets[key].Add(entry); } } foreach (var bucket in trackBuckets) { const int trackFileCount = 4; if (bucket.Value.Count == trackFileCount) { const int keyValueCount = 2; var keyValues = bucket.Key.Split(":"); if (keyValues.Length == keyValueCount) { string trackName = string.Format("{0} ({1} S_{2})", originalTrack.TrackName, keyValues[0], keyValues[1]); string path = string.Format(@"{0}\{1}.zip", m_destinationFolder, trackName); Console.WriteLine("Creating new track ({0}) from {1}.", trackName, originalTrack.TrackName); //Write track to file. using (Stream zipStream = new FileStream(path, FileMode.Create)) { using (ZipArchive zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true)) { foreach (var entry in bucket.Value) { var destFile = zipArchive.CreateEntry(entry.Name); using (var destStream = destFile.Open()) using (var srcStream = entry.Open()) { var task = srcStream.CopyToAsync(destStream); task.Wait(); } } } } var newTrack = new Track { TrackName = trackName, SourceTrackUrl = originalTrack.SourceTrackUrl, SourceThumbnailUrl = originalTrack.SourceThumbnailUrl, TrackUrl = originalTrack.TrackUrl, ThumbnailUrl = originalTrack.ThumbnailUrl, Author = originalTrack.Author, CreationTime = originalTrack.CreationTime }; newTracks.Add(newTrack); } else { Console.Error.WriteLine(string.Format("Unexpected key values: {0}, for track {1}", bucket.Key, fileName)); } } else { Console.Error.WriteLine(string.Format("Unexpected track file counts in bucket {0}, for track {1}", bucket.Key, fileName)); } } } return(newTracks.ToArray()); }