annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (ArmyBuilderFileExtractor.cs) is a part of the IBBoard.ArmyBuilder.API project and is copyright 2009 IBBoard
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // 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.
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 //
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.IO;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 //using ICSharpCode.SharpZipLib.BZip2;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 //using ICSharpCode.SharpZipLib.GZip;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 using IBBoard.ArmyBuilder.API;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 namespace IBBoard.ArmyBuilder.API.Extractors
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 public class ArmyBuilderFileExtractor
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 public static byte[] GetFileEntryContent(FileTableEntry entry)
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 //BZip2InputStream zipped = null;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 //GZipInputStream zipped = null;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 InflaterInputStream zipped = null;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 byte[] byteArr = new byte[entry.FileSize];
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 try
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 byte [] compressed = GetFileEntryCompressedContent(entry);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 MemoryStream ms = new MemoryStream(compressed, 0, compressed.Length);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 ms.Seek(0, SeekOrigin.Begin);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 //zipped = new BZip2InputStream(ms);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 //zipped = new GZipInputStream(ms);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 zipped = new InflaterInputStream(ms);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 int size = 0;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 int offset = 0;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 do
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 size = zipped.Read(byteArr, offset, byteArr.Length);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 offset+= size;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 while (size > 0);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 finally
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 if (zipped != null)
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 zipped.Close();
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 return byteArr;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 public static byte[] GetFileEntryCompressedContent(FileTableEntry entry)
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 FileStream stream = entry.GetParentFileStream();
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 stream.Seek(entry.Location, SeekOrigin.Begin);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 byte[] bytes = new byte[entry.CompressedSize];
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 try
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 stream.Read(bytes, 0, entry.CompressedSize);
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 finally
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 {
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 stream.Close();
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 return bytes;
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 }
1a5205612b72 Add initial work on ArmyBuilder API project\nno-open-ticket
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 }