view Extractors/ArmyBuilderFileExtractor.cs @ 0:1a5205612b72

Add initial work on ArmyBuilder API project\nno-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 19 Apr 2009 11:24:10 +0000
parents
children 1a54f6afafe7
line wrap: on
line source

//  This file (ArmyBuilderFileExtractor.cs) is a part of the IBBoard.ArmyBuilder.API project and is copyright 2009 IBBoard
// 
//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
// 

using System;
using System.IO;
//using ICSharpCode.SharpZipLib.BZip2;
//using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using IBBoard.ArmyBuilder.API;

namespace IBBoard.ArmyBuilder.API.Extractors
{	
	public class ArmyBuilderFileExtractor
	{		
		public static byte[] GetFileEntryContent(FileTableEntry entry)
		{			
			//BZip2InputStream zipped = null;
			//GZipInputStream zipped = null;
			InflaterInputStream zipped = null;
			byte[] byteArr = new byte[entry.FileSize];
			
			try
			{
				byte [] compressed = GetFileEntryCompressedContent(entry);
				MemoryStream ms = new MemoryStream(compressed, 0, compressed.Length);
				ms.Seek(0, SeekOrigin.Begin);
				//zipped = new BZip2InputStream(ms);
				//zipped = new GZipInputStream(ms);
				zipped = new InflaterInputStream(ms);
								
				int size = 0;
				int offset = 0;

				do
				{
					size = zipped.Read(byteArr, offset, byteArr.Length);
					offset+= size;
				} 
				while (size > 0);
			}
			finally
			{
				if (zipped != null)
				{
					zipped.Close();
				}
			}

			return byteArr;
		}

		public static byte[] GetFileEntryCompressedContent(FileTableEntry entry)
		{
			FileStream stream = entry.GetParentFileStream();
			stream.Seek(entry.Location, SeekOrigin.Begin);
			byte[] bytes = new byte[entry.CompressedSize];
			
			try
			{
				stream.Read(bytes, 0, entry.CompressedSize);
			}
			finally
			{
				stream.Close();
			}

			return bytes;
		}
	}
}