public byte[] CreateBinaryData(bool header)
 {
     using (MemoryStream stream = new MemoryStream())
         using (CustomBinaryWriter writer = new CustomBinaryWriter(stream, Encoding.ASCII))
         {
             WriteBinaryToStream(stream, writer, header);
             return(stream.ToArray());
         }
 }
Beispiel #2
0
        public Mutex GetStream(out FileStream stream, out CustomBinaryReader reader, out CustomBinaryWriter writer)
        {
            mutex.WaitOne();

            stream = this.fileStream;
            reader = this.customBinaryReader;
            writer = this.customBinaryWriter;

            return(mutex);
        }
Beispiel #3
0
        public void Initialize(string filename, bool readOnly, bool logging = false)
        {
            Filename = filename;
            Logging  = logging;

            mutex = new Mutex();

            if (readOnly)
            {
                ReadOnly   = true;
                fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            else
            {
                try
                {
                    ReadOnly   = false;
                    fileStream = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                }
                catch (IOException)
                {
                    ReadOnly   = true;
                    fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                }
            }

            try
            {
                mutex.WaitOne();

                customBinaryReader = new CustomBinaryReader(fileStream);
                customBinaryWriter = ReadOnly ? null : new CustomBinaryWriter(fileStream);

                var offset_central_directory_end = FindEndCentralDirectoryOffset(fileStream, customBinaryReader);
                central_directory_end = ReadPK(fileStream, customBinaryReader, offset_central_directory_end) as CentralDirectoryEnd;

                var offset_central_directory_locator_offset = offset_central_directory_end - 0x14;
                central_directory_locator_offset = ReadPK(fileStream, customBinaryReader, offset_central_directory_locator_offset) as CentralDirectoryLocatorOffset;

                var offset_central_directory_locator = central_directory_locator_offset.DirectoryLocatorOffset;
                central_directory_locator = ReadPK(fileStream, customBinaryReader, offset_central_directory_locator) as CentralDirectoryLocator;

                PopulateDirectory(fileStream, customBinaryReader, central_directory_locator);
            }
            finally
            {
                mutex.ReleaseMutex();
            }

            if (Logging)
            {
                Console.WriteLine($"Finished loading {filename}");
            }
        }
Beispiel #4
0
        public void WriteBinaryToStream(Stream stream, CustomBinaryWriter writer, bool header)
        {
            if (header)
            {
                writer.Write((byte)0x50);
                writer.Write((byte)0x4B);
                writer.Write((byte)0x06);
                writer.Write((byte)0x07);
            }

            writer.Write(unknownA);
            writer.Write(DirectoryLocatorOffset);
            writer.Write(unknownB);
        }
Beispiel #5
0
        public void WriteBinaryToStream(Stream stream, CustomBinaryWriter writer, bool header)
        {
            var start = stream.Position;

            if (header)
            {
                writer.Write((byte)0x50);
                writer.Write((byte)0x4B);
                writer.Write((byte)0x03);
                writer.Write((byte)0x14);
            }

            var extra_data = Extra.CreateBinaryData();

            ExtraLength = (short)extra_data.Length;

            writer.Write(Version);
            writer.Write(Flags);
            writer.Write((short)CompressionMode);
            writer.Write(ModificationTime);
            writer.Write(ModificationDate);
            writer.Write(CRC32);
            writer.Write(CompressedSize);
            writer.Write(UncompressedSize);
            writer.Write(FilenameLength);

            var extra_length_position = stream.Position;

            writer.Write(ExtraLength);

            writer.WriteString(Filename, false);
            var extra_data_start_position = stream.Position;

            writer.Write(extra_data);

            // fixup the extra data to include the length of the padding, otherwise
            // the stream will only read part way through the padding into a bunch
            // of zeroed bytes
            var end = stream.Position;
            var padding_remaining = 0x1000 - (end - start);

            stream.Position = extra_length_position;
            ExtraLength    += (short)padding_remaining;
            writer.Write((short)ExtraLength);

            stream.Position = extra_data_start_position + ExtraLength;
        }
Beispiel #6
0
        public byte[] CreateCentralDirectoryChunk()
        {
            using (MemoryStream cd_stream = new MemoryStream())
                using (CustomBinaryWriter cd_writer = new CustomBinaryWriter(cd_stream))
                {
                    for (var i = 0; i < Files.Count; i++)
                    {
                        var file = Files[i];

                        byte[] bytes = file.centralDirectory.CreateBinaryData(true);

                        cd_writer.Write(bytes);
                    }

                    return(cd_stream.ToArray());
                }
        }
        public void WriteBinaryToStream(Stream stream, CustomBinaryWriter writer, bool header)
        {
            if (header)
            {
                writer.Write((byte)0x50);
                writer.Write((byte)0x4B);
                writer.Write((byte)0x05);
                writer.Write((byte)0x06);
            }

            writer.Write(DiskNumber);
            writer.Write(DiskNumberWithCD);
            writer.Write(DiskEntries);
            writer.Write(TotalEntries);
            writer.Write(CentralDirectorySize);
            writer.Write(OffsetOfCdStartingDisk);
            writer.Write(CommentLength);
            writer.WriteString(Comment, false);
        }
Beispiel #8
0
            public byte[] CreateBinaryData()
            {
                using (MemoryStream stream = new MemoryStream())
                    using (CustomBinaryWriter writer = new CustomBinaryWriter(stream, Encoding.ASCII))
                    {
                        writer.Write(_UnknownID);
                        writer.Write(StructureSizeAfterHeader);
                        writer.Write(UncompressedFileLength);
                        writer.Write(_UnknownD);
                        writer.Write(CompressedFileLength);
                        writer.Write(_UnknownF);

                        var data_offset_ldword = (int)(DataOffset & 0xFFFFFFFF);
                        var data_offset_hdword = (int)((DataOffset >> 32) & 0xFFFFFFFF);

                        writer.Write(data_offset_ldword);
                        writer.Write(data_offset_hdword);

                        return(stream.ToArray());
                    }
            }
        public void WriteBinaryToStream(Stream stream, CustomBinaryWriter writer, bool header)
        {
            if (header)
            {
                writer.Write((byte)0x50);
                writer.Write((byte)0x4B);
                writer.Write((byte)0x06);
                writer.Write((byte)0x06);
            }

            writer.Write(unknownA);
            writer.Write(unknownB);
            writer.Write(unknownC);
            writer.Write(unknownD);
            writer.Write(unknownE);
            writer.Write(unknownF);
            writer.Write(DontentDictionaryCount);
            writer.Write(unknownG);
            writer.Write(ContentDictionaryCount2);
            writer.Write(unknownH);
            writer.Write(ContentDictionarySize);
            writer.Write(unknownI);
            writer.Write(ContentDirectoryOffset);
        }
Beispiel #10
0
        public void WriteContentDirectoryChunk(FileStream stream, CustomBinaryReader reader, CustomBinaryWriter writer)
        {
            stream.Position = central_directory_locator.ContentDirectoryOffset;

            var cd_chunk = CreateCentralDirectoryChunk();

            writer.Write(cd_chunk);

            central_directory_locator.ContentDictionarySize   = cd_chunk.Length;
            central_directory_locator.DontentDictionaryCount  = Files.Count;
            central_directory_locator.ContentDictionaryCount2 = Files.Count;

            central_directory_locator_offset.DirectoryLocatorOffset = stream.Position;
            byte[] locator_bytes        = this.central_directory_locator.CreateBinaryData(true);
            byte[] locator_offset_bytes = this.central_directory_locator_offset.CreateBinaryData(true);
            byte[] cd_end_bytes         = this.central_directory_end.CreateBinaryData(true);

            writer.Write(locator_bytes);
            writer.Write(locator_offset_bytes);
            writer.Write(cd_end_bytes);
        }