Esempio n. 1
0
        private static CompressionHeader ReadCompressionHeader(BinaryReader reader)
        {
            var magicInt = BitConverter.ToUInt32(Encoding.ASCII.GetBytes(Constants.Magic.SECOND_FILE_HEADER_MAGIC), 0);

            if (reader.ReadUInt32() != magicInt)
            {
                throw new Exception();
            }

            var result = new CompressionHeader();

            var entryCount = reader.ReadInt32();

            for (int i = 0; i < entryCount; i++)
            {
                result.DataChunkInfos.Add(new DataChunkInfo
                {
                    Offset           = reader.ReadInt32(),
                    CompressedSize   = reader.ReadInt32(),
                    DecompressedSize = reader.ReadInt32()
                });
            }

            result.MaxEntries          = (result.DataChunkInfos[0].Offset - 33) / 12;
            reader.BaseStream.Position = result.DataChunkInfos[0].Offset;

            return(result);
        }
Esempio n. 2
0
        static byte[] UnpackRecord(byte[] ab, out CompressionHeader coh)
        {
            coh.fCompressed    = (ab[1] == 1);
            coh.cbUncompressed = (ushort)((ab[2] << 8) + ab[3]);
            coh.cbCompressed   = (ushort)((ab[4] << 8) + ab[5]);

            byte[] abT = new byte[ab.Length - 6];
            Array.Copy(ab, 6, abT, 0, abT.Length);
            if (coh.fCompressed)
            {
                ab = Compressor.DecompressChunk(abT);
            }
            else
            {
                ab = abT;
            }

            return(ab);
        }
Esempio n. 3
0
        /// <summary>
        /// Serialize a CompressionHeaderStruct
        /// </summary>
        /// 
        /// <param name="Header">The initialized CompressionHeaderStruct</param>
        /// 
        /// <returns>The struct as a stream of bytes</returns>
        public static MemoryStream SerializeHeader(CompressionHeader Header)
        {
            try
            {
                MemoryStream stream = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(stream);

                // write compression format
                writer.Write(Header.Format);
                // write file count
                writer.Write(Header.FileCount);
                // write name array length
                writer.Write(Header.FileNames.Length);
                // write positions aray
                byte[] temp = Convert(Header.FileSizes);
                writer.Write(temp);
                // write file names array
                writer.Write(Header.FileNames);
                stream.Seek(0, SeekOrigin.Begin);

                return stream;
            }
            catch
            {
                return new MemoryStream();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Read a compression header from a file path
        /// </summary>
        /// 
        /// <param name="InputFile">The full path to the input file</param>
        /// 
        /// <returns>An initialized CompressionHeaderStruct</returns>
        public static CompressionHeader DeSerializeHeader(string InputFile)
        {
            CompressionHeader Header = new CompressionHeader();
            if (!File.Exists(InputFile)) return Header;

            using (BinaryReader reader = new BinaryReader(new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                // compression format
                Header.Format = reader.ReadInt32();
                // get file count
                int fileCount = reader.ReadInt32();
                Header.FileCount = fileCount;
                // file name array length
                int nameLen = reader.ReadInt32();
                Header.NameSize = nameLen;
                // get start positions in the file
                int btCount = fileCount * 8;
                byte[] temp = reader.ReadBytes(btCount);
                Header.FileSizes = Convert(temp);
                // get file name array
                Header.FileNames = reader.ReadChars(nameLen);
            }

            return Header;
        }
Esempio n. 5
0
        /// <summary>
        /// Read a compression header from a stream
        /// </summary>
        /// 
        /// <param name="InputStream">The data stream</param>
        /// 
        /// <returns>An initialized CompressionHeaderStruct</returns>
        public static CompressionHeader DeSerializeHeader(Stream InputStream)
        {
            InputStream.Seek(0, SeekOrigin.Begin);
            CompressionHeader Header = new CompressionHeader();
            if (InputStream.Length < MIN_HEADER) return Header;

            BinaryReader reader = new BinaryReader(InputStream);

            // compression format
            Header.Format = reader.ReadInt32();
            // get file count
            int fileCount = reader.ReadInt32();
            Header.FileCount = fileCount;
            // file name array length
            int nameLen = reader.ReadInt32();
            Header.NameSize = nameLen;
            // get start positions in the file
            int btCount = fileCount * 8;
            byte[] temp = reader.ReadBytes(btCount);
            Header.FileSizes = Convert(temp);
            // get file name array
            Header.FileNames = reader.ReadChars(nameLen);
            InputStream.Seek(0, SeekOrigin.Begin);

            return Header;
        }
Esempio n. 6
0
        /// <summary>
        /// Create a Compression header from a folder path
        /// </summary>
        /// 
        /// <param name="InputPath">The file to compress</param>
        /// <param name="Format">The compression format</param>
        /// <param name="FolderOption">The scope of the folder compression; top level or all sub-folders</param>
        /// 
        /// <returns>An initialized CompressionHeader structure</returns>
        public static CompressionHeader CreateFolderHeader(string InputPath, CompressionFormats Format = CompressionFormats.Deflate, SearchOption FolderOption = SearchOption.AllDirectories)
        {
            if (!Directory.Exists(InputPath)) return new CompressionHeader();
            CompressionHeader header = new CompressionHeader();
            char[] names = GetNameArray(InputPath, FolderOption);

            header.Format = (int)Format;
            header.FileCount = GetFileCount(InputPath, FolderOption);
            header.NameSize = names.Length;
            header.FileSizes = GetFileSizes(InputPath, FolderOption);
            header.FileNames = names;

            return header;
        }
Esempio n. 7
0
        /// <summary>
        /// Create a Compression header from a file path
        /// </summary>
        /// 
        /// <param name="InputFile">The file to compress</param>
        /// <param name="Format">The compression format</param>
        /// 
        /// <returns>An initialized CompressionHeader structure</returns>
        public static CompressionHeader CreateFileHeader(string InputFile, CompressionFormats Format = CompressionFormats.Deflate)
        {
            if (!File.Exists(InputFile)) return new CompressionHeader();
            CompressionHeader header = new CompressionHeader();
            char[] name = Path.GetFileName(InputFile).ToCharArray();

            header.Format = (int)Format;
            header.FileCount = 1;
            header.NameSize = name.Length;
            header.FileSizes = new Int64[1];
            header.FileSizes[0] = GetFileSize(InputFile);
            header.FileNames = name;

            return header;
        }
Esempio n. 8
0
        /// <summary>
        /// Create an empty Compression header
        /// </summary>
        /// 
        /// <param name="Format">The compression format</param>
        /// 
        /// <returns>An initialized CompressionHeader structure</returns>
        public static CompressionHeader CreateFileHeader(CompressionFormats Format = CompressionFormats.Deflate)
        {
            CompressionHeader header = new CompressionHeader();

            header.Format = (int)Format;
            header.FileCount = 0;
            header.NameSize = 0;
            header.FileSizes = new Int64[0];
            header.FileSizes[0] = 1;
            header.FileNames = new char[0];

            return header;
        }
Esempio n. 9
0
        static byte[] UnpackRecord(byte[] ab, out CompressionHeader coh)
        {
            coh.fCompressed = (ab[1] == 1);
            coh.cbUncompressed = (ushort)((ab[2] << 8) + ab[3]);
            coh.cbCompressed = (ushort)((ab[4] << 8) + ab[5]);

            byte[] abT = new byte[ab.Length - 6];
            Array.Copy(ab, 6, abT, 0, abT.Length);
            if (coh.fCompressed) {
                ab = Compressor.DecompressChunk(abT);
            } else {
                ab = abT;
            }

            return ab;
        }