By: Nick Gravelyn
http://nick.gravelyn.com/code
nickgravelyn@gmail.com
Latest Installed Version: 1.3
EasyZip Homepage
Welcome to EasyZip, the easiest way to manage your XNA Game Studio 2.0 game's growing content sizes. EasyZip is an incredibly simple solution to automatically add all your content pipeline files into a convenient ZIP file and read these assets from the ZIP file at runtime. This process can reduce your built content sizes by up to 70%!
Since you already have EasyZip installed, you have most of the work out of the way. The next step to take advantage of the library is to either create a new XNA Game Studio 2.0 project or find an existing XNA Game Studio 2.0 project. After the project has been created, make sure it is not open in Visual Studio. Attempting to update a project that is open can lead to errors in the update process.
Once a project has been created or located, we can run the EasyZip Project Utility to modify the .csproj and .contentproj files that make up your game project. You can launch the EasyZip Project Utility from the Start Menu (All Programs -> EasyZip -> Project Utility). Once open, browse to the game project (.csproj) or type in the path.
Next choose the options you wish. You can have the Project Utiltiy create backup projects for you if you would like. This can be useful in case problems occur later. You can then select whether or not you want the ZIP file to maintain the folder structure. If this box is checked, any folders you create in your content project will be brought over to the ZIP file. If this box is unchecked, all your content will be placed in the ZIP file with no folders at all. You are always able to run the utility against your project again later to change this option.
Once you are presented with a message box informing you the update process was successful, you are ready to begin programming with EasyZip. The content project has all the instructions it needs to create the ZIP file and the game project has the proper references added to it.
Content loaded with EasyZip uses a derived ContentManager called ZipContentManager. This means the process of loading content with EasyZip is almost identical to using the standard ContentManager.
using EasyZip;Next declare a new ZipContentManager in your class:
ZipContentManager zipContent;Next instantiate the ZipContentManager. The constructor is similart to the normal ContentManager constructor in that it requires an IServiceProvider instance (usually Game.Services). The constructor also requires a path to the ZIP file. The ZIP file created will always be next to your game executable and will have the same name as the content subproject. So by default the name is Content.ZIP:
zipContent = new ZipContentManager(Services, "Content.zip");Lastly we just load content like we would any other ContentManager:
SpriteFont font = zipContent.Load<SpriteFont>("Arial");
Here is an entire Game class filled in to demonstrate the EasyZip capabilities:
using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using EasyZip; namespace WindowsGame1 { public class Game1 : Microsoft.Xna.Framework.Game { ZipContentManager zipContent; GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont font; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; zipContent = new ZipContentManager(Services, "Content.zip"); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = zipContent.Load<SpriteFont>(@"Arial"); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(SpriteBlendMode.AlphaBlend); spriteBatch.DrawString( font, "Hello EasyZip!", new Vector2(100), Color.White); spriteBatch.End(); base.Draw(gameTime); } } }
This section details the API of the ZipContentManager class that are not a part of the default ContentManager.
ZipContentManager zipContent = new ZipContentManager( Services, "Content.zip");
ZipContentManager zipContent = new ZipContentManager( Services, "Content.zip", false);
string[] files = zipContent.GetAssetNames(); foreach (string file in files) { Console.WriteLine(file); }
string[] files = zipContent.GetAssetNamesFromDirectory("Textures"); List<Texture2D> textures = new List<Texture2D>(); foreach (string file in files) { textures.Add(zipContent.Load<Texture2D>(file)); }
Stream stream = zipContent.GetFileStream("somefile.txt"); using (StreamReader reader = new StreamReader(stream)) { Console.WriteLine(reader.ReadLine()); }