using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace System.Fire.Container {
public partial class ContainerList {
///
/// Exports an file from the container to the given path
///
/// The container file index
/// Path to export the file to
/// Returns true if the export was successfull
public bool ExportFile(int FileIndex, string ExportPath) {
return this.ExportFile(this._files[FileIndex], ExportPath);
}
///
/// Exports an file from the container to the given path
///
/// The container file index
/// Path to export the file to
/// Returns true if the export was successfull
public bool ExportFile(ContainerFile File, string ExportPath) {
FileInfo fi = new FileInfo(ExportPath);
FileAttributes fa = new DirectoryInfo(fi.DirectoryName).Attributes;
if ((fa | FileAttributes.ReadOnly) == fa) {
System.Diagnostics.Debug.WriteLine(fi.DirectoryName + " is ReadOnly");
return false;
}
if ((fa | FileAttributes.Hidden) == fa) {
System.Diagnostics.Debug.WriteLine(fi.DirectoryName + " is Hidden");
}
if (!Directory.Exists(fi.DirectoryName))
Directory.CreateDirectory(fi.DirectoryName);
if (System.IO.File.Exists(ExportPath))
System.IO.File.Delete(ExportPath);
FileStream fs = new FileStream(ExportPath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
//byte Group = 0, Container = 0;
//ushort Pos = 0;
long size = 0;
for (int i = 0, c = File.NumChunks; i < c; i++) {
//File.GetChunk(i, ref Group, ref Container, ref Pos);
byte[] b = this.ReadChunk(File.ChunkPos, i);
if (size + b.Length > File.Size) {
bw.Write(b.Take((int)(File.Size - size)).ToArray());
size += File.Size - size;
} else {
bw.Write(b);
size += b.Length;
}
}
bw.Close();
return true;
}
}
}