#region Copyright & License Information /* * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, * see LICENSE. */ #endregion using System.Collections.Generic; using System.IO; using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.Zip; using SZipFile = ICSharpCode.SharpZipLib.Zip.ZipFile; namespace OpenRA.FileFormats { public static class PackageWriter { public static void CreateMix(string filename, List contents) { // Construct a list of entries for the file header uint dataSize = 0; var items = new List(); foreach (var file in contents) { uint length = (uint)new FileInfo(file).Length; uint hash = PackageEntry.HashFilename(Path.GetFileName(file)); items.Add(new PackageEntry(hash, dataSize, length)); dataSize += length; } using (var s = File.Create(filename)) using (var writer = new BinaryWriter(s)) { // Write file header writer.Write((ushort)items.Count); writer.Write(dataSize); foreach (var item in items) item.Write(writer); writer.Flush(); // Copy file data foreach (var file in contents) s.Write(File.ReadAllBytes(file)); } } class StaticMemoryDataSource : IStaticDataSource { byte[] data; public StaticMemoryDataSource (byte[] data) { this.data = data; } public Stream GetSource() { return new MemoryStream(data); } } public static void CreateZip(string zipFile, Dictionary contents) { var z = SZipFile.Create(zipFile); z.BeginUpdate(); foreach (var kvp in contents) { z.Add(new StaticMemoryDataSource(kvp.Value), kvp.Key); } z.CommitUpdate(); z.Close(); } } }