static void Main(string[] args) { global = cGlobal.INSTANCE; bool result = false; ConsoleTraceListener ctl_trace = new ConsoleTraceListener(); Trace.Listeners.Add(ctl_trace); global.ts_TypeTrace.Level = TraceLevel.Error; sw_watch = new Stopwatch(); try { global.readArguments(args); Trace.AutoFlush = true; sw_watch.Start(); switch (global.ACTION) { case "COMP": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DESTINATION == "") throw new ToDException("The option -o is missing !"); if (global.MODE == -1) throw new ToDException("The option -m is missing !"); using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) using (MemoryStream ms_out = new MemoryStream()) { Compression.compFile(fs_in, ms_out); global.writeFileToDisk(ms_out, global.DESTINATION); } break; case "DECOMP": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DESTINATION == "") throw new ToDException("The option -o is missing !"); result = false; using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) using (MemoryStream ms_out = new MemoryStream()) { Compression.decompFile(fs_in, ms_out); if (global.RECURSIVE) result = global.processFile(ms_out, global.DESTINATION, "", global.DESTINATION); } break; case "EXTRACT": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DIR_OUT == "") throw new ToDException("The option -d is missing !"); extractFromIso(); break; case "INSERT": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DESTINATION == "") throw new ToDException("The option -o is missing !"); insertToIso(); break; case "PACK": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); if (global.DESTINATION == "") throw new ToDException("The option -o is missing !"); if (global.MODE == -1) throw new ToDException("The option -m is missing !"); Archive.packFile(); break; case "UNPACK": if (global.SOURCE == "") throw new ToDException("The option -i is missing !"); using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) { Archive.unpackFile(fs_in, global.DIR_OUT, 1); } break; default: throw new ToDException(string.Format("Unknow action {0} !", global.ACTION)); } } catch (ToDException ex) { Trace.WriteLine(ex.Message + Environment.NewLine, "ERROR"); usage(); } catch (Exception ex) { Trace.WriteLine(ex.Message + Environment.NewLine, "ERROR"); Trace.WriteLine(ex.StackTrace); } finally { sw_watch.Stop(); Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose, string.Format("Terminated. Execution time : {0}", sw_watch.Elapsed)); } }
//---------------------------------------------------------------------------------------- public static void unpackFile(Stream as_in, string as_pathname, int mode) { global = cGlobal.INSTANCE; List<cGlobal.st_index> index = new List<cGlobal.st_index>(); byte[] b_file; string s_ext; int size; try { index = readIndex(as_in, mode); for (int i = 0; i < index.Count; i++) { if (i == index.Count - 1) size = (int)(as_in.Length - index[i].pos); else size = (int)(index[i + 1].pos - index[i].pos); b_file = new byte[size]; string s_name = string.Format("{0:0000}", index[i].id); s_ext = global.getTypeOfFile(as_in, (int)index[i].pos, size); as_in.Seek(index[i].pos, SeekOrigin.Begin); using (MemoryStream ms_file = new MemoryStream()) { ms_file.CopyFrom(as_in, size); ms_file.Position = 0; if (global.RECURSIVE) global.processFile(ms_file, as_pathname, s_name, as_pathname + s_name + s_ext); else global.writeFileToDisk(ms_file, as_pathname + s_name + s_ext); } b_file = null; } } catch (Exception ex) { throw ex; } }
//---------------------------------------------------------------------------------------- public static void packFile() { global = cGlobal.INSTANCE; List<cGlobal.st_index> index_orig = new List<cGlobal.st_index>(); List<cGlobal.st_index> index_dest = new List<cGlobal.st_index>(); MemoryStream ms_dest = new MemoryStream(); cGlobal.st_index elem; byte[] b_file; string s_pathname; string s_name; int size; Trace.WriteLine(string.Format("Inserting file {0}", global.SOURCE)); Trace.Indent(); try { using (BinaryReader br_file = new BinaryReader(File.Open(global.SOURCE, FileMode.Open, FileAccess.Read))) { index_orig = readIndex(br_file, global.MODE); ms_dest.Position = global.MODE == 1 ? index_orig.Count * 4 : index_orig.Count * 4 + 4; for (int i = 0; i < index_orig.Count; i++) { if (i == index_orig.Count - 1) size = (int)(br_file.BaseStream.Length - index_orig[i].pos); else size = (int)(index_orig[i + 1].pos - index_orig[i].pos); s_name = string.Format("{0:0000}{1}", index_orig[i].id, global.getTypeOfFile(br_file, (int)index_orig[i].pos, size)); s_pathname = Path.GetDirectoryName(global.SOURCE) + "/" + Path.GetFileNameWithoutExtension(global.SOURCE) + "/" + s_name; elem.id = i; elem.pos = (UInt32)ms_dest.Position; if (File.Exists(s_pathname)) { Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose, string.Format("Inserting file {0}", s_name)); using (BinaryReader br = new BinaryReader(File.Open(s_pathname, FileMode.Open, FileAccess.Read))) { b_file = br.ReadBytes((int)br.BaseStream.Length); elem.size = (UInt32)br.BaseStream.Length; } } else { Trace.WriteLineIf(global.ts_TypeTrace.TraceVerbose, string.Format("Copying file {0}{1}", s_name)); br_file.BaseStream.Seek(index_orig[i].pos, SeekOrigin.Begin); b_file = new byte[size]; br_file.Read(b_file, 0, size); elem.size = (UInt32)size; } index_dest.Add(elem); ms_dest.Write(b_file, 0, size); global.padding(ms_dest, 4); b_file = null; } } ms_dest.Position = 0; if (global.MODE == 2) { byte[] b = new byte[4]; b = BitConverter.GetBytes(index_dest.Count); Array.Reverse(BitConverter.GetBytes(index_dest.Count)); ms_dest.Write(b, 0, 4); } writeIndex(ms_dest, index_dest); global.writeFileToDisk(ms_dest, global.DESTINATION); } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }
public static void unpackBDat(MemoryStream ams_file, List<cGlobal.st_index> al_index, string as_path) { global = cGlobal.INSTANCE; byte[] b_file; string s_ext; Trace.Indent(); try { foreach (cGlobal.st_index file in al_index) { b_file = new byte[file.size]; string s_name = string.Format("{0:0000}", file.id); s_ext = global.getTypeOfFile(ams_file, (int)file.pos, (int)file.size); using (MemoryStream ms_file = new MemoryStream()) { ams_file.CopyTo(ms_file, file.size, file.pos); ms_file.Position = 0; if (global.RECURSIVE) global.processFile(ms_file, as_path, s_name, as_path + s_name + s_ext); else global.writeFileToDisk(ms_file, as_path + s_name + s_ext); } b_file = null; } } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }
//---------------------------------------------------------------------------------------- public static void decompFile() { global = cGlobal.INSTANCE; Trace.WriteLine("Decompressing"); Trace.Indent(); try { using (FileStream fs_in = new FileStream(global.SOURCE, FileMode.Open)) using (MemoryStream ms_out = new MemoryStream()) { bool b_result = TOLib.decomp(fs_in, ms_out); if (!b_result) throw new Exception(); global.writeFileToDisk(ms_out, global.DESTINATION); } } catch (Exception ex) { throw ex; } finally { Trace.Unindent(); } }