示例#1
0
        public static Stream GetFileAsStream(string sarc_name_full, string relative_path)
        {
            SarcFileLocation f_location = GetFileLocation(sarc_name_full, relative_path);
            MemoryStream     mem        = new MemoryStream();
            Stream           stream     = new FileStream(sarc_name_full, FileMode.Open);

            stream.Seek(f_location.file_offset, SeekOrigin.Begin);

            byte[]       buffer = new byte[4096];
            BinaryReader reader = new BinaryReader(stream);
            BinaryWriter writer = new BinaryWriter(mem);
            int          nb     = 0;
            // записываем часть файла в mem
            long nbytes = f_location.file_length;

            while (nbytes > 0)
            {
                int portion = buffer.Length < nbytes ? buffer.Length : (int)nbytes;
                nb = reader.Read(buffer, 0, portion);
                writer.Write(buffer, 0, nb);
                nbytes -= nb;
            }
            reader.Close(); stream.Close();
            mem.Seek(0, SeekOrigin.Begin);
            return(mem);
        }
示例#2
0
 public static SarcFileLocation GetFileLocation(string sarc_full_name, string relative_path)
 {
     if (SarcCatalog == null || SarcCatalog.Count > 100000)
     {
         SarcCatalog = new Dictionary <string, SarcFileLocation>();
     }
     if (!SarcCatalog.ContainsKey(relative_path))
     {
         Stream stream = new FileStream(sarc_full_name, FileMode.Open, FileAccess.Read);
         //stream.Seek(prologLength, SeekOrigin.Begin);
         long offset = 0L;
         for (int i = 0; i < 16; i++)
         {
             int b = stream.ReadByte(); if (b != 0)
             {
                 offset = offset * 10 + (b - '0');
             }
         }
         byte[]       catalog_bytes = new byte[offset - 16];
         var          v             = stream.Read(catalog_bytes, 0, (int)(offset - 16));
         MemoryStream mem           = new MemoryStream(catalog_bytes);
         //Encoding etable = Encoding.UTF8;
         //XElement cat = XElement.Parse(etable.GetString(catalog_bytes));
         System.Xml.XmlReader xreader = new System.Xml.XmlTextReader(mem);
         //XElement catalog = XElement.Load(new StreamReader(stream, System.Text.Encoding.UTF8));
         XElement catalog = XElement.Load(xreader);
         stream.Close();
         foreach (XElement xfile in catalog.Elements("file"))
         {
             string           r_path = xfile.Element("path").Value;
             SarcFileLocation sfl    = new SarcFileLocation()
             {
                 relative_path  = r_path,
                 sarc_full_name = sarc_full_name,
                 file_offset    = Int64.Parse(xfile.Element("start").Value) + offset,
                 file_length    = Int64.Parse(xfile.Element("length").Value)
             };
             SarcCatalog.Add(r_path, sfl);
         }
     }
     return(SarcCatalog[relative_path]);
 }
示例#3
0
 public static SarcFileLocation GetFileLocation(string sarc_full_name, string relative_path)
 {
     if (SarcCatalog == null || SarcCatalog.Count > 100000) SarcCatalog = new Dictionary<string, SarcFileLocation>();
     if (!SarcCatalog.ContainsKey(relative_path))
     {
         Stream stream = new FileStream(sarc_full_name, FileMode.Open, FileAccess.Read);
         //stream.Seek(prologLength, SeekOrigin.Begin);
         long offset = 0L;
         for (int i = 0; i < 16; i++) { int b = stream.ReadByte(); if (b != 0) offset = offset * 10 + (b - '0'); }
         byte[] catalog_bytes = new byte[offset - 16];
         var v = stream.Read(catalog_bytes, 0, (int)(offset - 16));
         MemoryStream mem = new MemoryStream(catalog_bytes);
         //Encoding etable = Encoding.UTF8;
         //XElement cat = XElement.Parse(etable.GetString(catalog_bytes));
         System.Xml.XmlReader xreader = new System.Xml.XmlTextReader(mem);
         //XElement catalog = XElement.Load(new StreamReader(stream, System.Text.Encoding.UTF8));
         XElement catalog = XElement.Load(xreader);
         stream.Close();
         foreach (XElement xfile in catalog.Elements("file"))
         {
             string r_path = xfile.Element("path").Value;
             SarcFileLocation sfl = new SarcFileLocation()
             {
                 relative_path = r_path,
                 sarc_full_name = sarc_full_name,
                 file_offset = Int64.Parse(xfile.Element("start").Value) + offset,
                 file_length = Int64.Parse(xfile.Element("length").Value)
             };
             SarcCatalog.Add(r_path, sfl);
         }
     }
     return SarcCatalog[relative_path];
 }