Exemplo n.º 1
0
        /// <summary>
        /// Extracts an entry from the blob file and puts the bytes into memory.
        /// </summary>
        /// <param name="entry">The BlobEntry to extract to memory.</param>
        /// <returns>The bytes of the entry in the blob file.</returns>
        public byte[] ExtractBytes(BlobEntry entry)
        {
            FileStream input;

            byte[] buffer;

            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }

#if DEBUG
            Trace.WriteLine(_filename, "Filename");
#endif

            buffer = new byte[entry.Size];

            input = File.OpenRead(_filename);
            input.Seek((long)entry.Offset, SeekOrigin.Begin);
            input.Read(buffer, 0, buffer.Length);

            return(buffer);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads the blob file information.
        /// </summary>
        private void Load()
        {
            FileStream stream;

            byte[]    buffer;
            BlobEntry entry;
            int       offset;
            int       end;

            buffer = new byte[8];
            stream = File.OpenRead(_filename);
            stream.Read(buffer, 0, 8);

            _version = BitConverter.ToInt32(buffer, 0);
            _count   = BitConverter.ToInt32(buffer, 4);

            offset = 0;

            if (_version == 1)
            {
                buffer = new byte[22 * _count];
                stream.Read(buffer, 0, buffer.Length);
                stream.Close();

                _entries = new List <BlobEntry>(_count);

                for (int i = 0; i < _count; i++)
                {
                    entry          = new BlobEntry();
                    entry.Filename = ASCIIEncoding.ASCII.GetString(buffer, offset, 14); offset += 14;
                    end            = entry.Filename.IndexOf('\0');
                    if (end > 0)
                    {
                        entry.Filename = entry.Filename.Substring(0, end);
                    }
                    entry.Offset = BitConverter.ToInt32(buffer, offset); offset += 4;
                    entry.Size   = BitConverter.ToInt32(buffer, offset); offset += 4;

                    _entries.Add(entry);
                }
            }
            else if (_version == 2)
            {
                buffer = new byte[40 * _count];
                stream.Read(buffer, 0, buffer.Length);
                stream.Close();

                _entries = new List <BlobEntry>(_count);

                for (int i = 0; i < _count; i++)
                {
                    entry          = new BlobEntry();
                    entry.Filename = ASCIIEncoding.ASCII.GetString(buffer, offset, 32); offset += 32;
                    end            = entry.Filename.IndexOf('\0');
                    if (end > 0)
                    {
                        entry.Filename = entry.Filename.Substring(0, end);
                    }
                    entry.Offset = BitConverter.ToInt32(buffer, offset); offset += 4;
                    entry.Size   = BitConverter.ToInt32(buffer, offset); offset += 4;

                    _entries.Add(entry);
                }
            }
            else
            {
                throw new Exception("Unknown file version: " + _version);
            }
        }
Exemplo n.º 3
0
        public void Add(string filename)
        {
            FileStream   originalStream, tempStream;
            BinaryWriter writer;
            int          size;
            int          headerLength;
            int          offset;
            string       tempname;

            byte[]    buffer, data;
            BlobEntry item;
            FileInfo  info;

            info = new FileInfo(filename);

            item          = new BlobEntry();
            item.Filename = Path.GetFileName(filename);
            item.Size     = (int)info.Length;

            _count++;

            tempname = Path.GetTempFileName();

            originalStream = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            tempStream     = new FileStream(tempname, FileMode.Open, FileAccess.Write, FileShare.None);

            writer = new BinaryWriter(tempStream);

            writer.Write(_version);
            writer.Write(_count);

            if (_version == 1)
            {
                size = 14;
            }
            else
            {
                size = 32;
            }

            headerLength = size * _count;

            // Write the header
            offset = headerLength;
            for (int i = 0; i < _entries.Count; i++)
            {
                // Make sure the string is the correct length
                data   = ASCIIEncoding.ASCII.GetBytes(_entries[i].Filename);
                buffer = new byte[size];
                Array.Clear(buffer, 0, buffer.Length);
                Array.Copy(data, 0, buffer, 0, data.Length);

                writer.Write(buffer);             // filename
                _entries[i].Offset = offset;
                writer.Write(_entries[i].Offset); // offset
                offset += _entries[i].Size;
                writer.Write(_entries[i].Size);   // file size
            }

            // Make sure the string is the correct length
            data   = ASCIIEncoding.ASCII.GetBytes(item.Filename);
            buffer = new byte[size];
            Array.Clear(buffer, 0, buffer.Length);
            Array.Copy(data, 0, buffer, 0, data.Length);

            writer.Write(buffer);      // filename
            item.Offset = offset;
            writer.Write(item.Offset); // offset
            offset += item.Size;
            writer.Write(item.Size);   // file size

            // Write the file data
            foreach (BlobEntry entry in _entries)
            {
                originalStream.Seek((long)entry.Offset, SeekOrigin.Begin);
                buffer = new byte[entry.Size];

                originalStream.Read(buffer, 0, buffer.Length);
                writer.Write(buffer);
            }

            buffer = File.ReadAllBytes(filename);
            writer.Write(buffer);

            originalStream.Close();
            tempStream.Close();

            _entries.Add(item);

            File.Delete(_filename);
            File.Move(tempname, _filename);
        }