view Extractors/ArmyBuilderFileExtractor.cs @ 5:ec77b60e5369 default tip

Re #121: Migrate to AGPL license * Update all Army Builder API files to AGPL license * Include AGPL license and remove GPL/LGPL documents
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Aug 2009 10:51:59 +0000
parents d2f7826147eb
children
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 and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
// 

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

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

				int size = 0;
				int pos = 0;

				do
				{
					size = zipped.Read(byteArr, pos, byteArr.Length);
					pos+=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;
		}
	}
}