예제 #1
0
        public override void Close()
        {
            // Data has changed.
            if (!RecompressJustWhenModified || !UncompressedOriginalData.SequenceEqual(((MemoryStream)this.ParentStream).ToArray()))
            {
                int Version = RecompressVersion;
                if (Version == -1)
                {
                    Version = TalesCompression.DetectVersion(this.CompressedStream.Slice());
                }
                var Compression      = TalesCompression.CreateFromVersion(Version);
                var RecompressedData = Compression.EncodeFile(ParentStream).ToArray();
                if (RecompressedData.Length > this.CompressedStream.Length)
                {
                    throw (new Exception(String.Format("Compressed file is bigger than original Updated: {0} > Previous: {1}", RecompressedData.Length, CompressedStream.Length)));
                }
                var CompressedStream2 = this.CompressedStream.Slice();
                CompressedStream2
                .WriteBytes(RecompressedData)
                .WriteByteRepeatedTo((byte)0x00, CompressedStream2.Length);
                ;
            }
            else
            {
                //Console.WriteLine("Unchanged!");
            }

            base.Close();
        }
예제 #2
0
 public DecompressRecompressStream(Stream CompressedStream, int RecompressVersion = -1, bool RecompressJustWhenModified = true)
     : base(TalesCompression.DecompressStream(CompressedStream.Slice()))
 {
     this.CompressedStream           = CompressedStream;
     this.RecompressVersion          = RecompressVersion;
     this.RecompressJustWhenModified = RecompressJustWhenModified;
     UncompressedOriginalData        = ((MemoryStream)this.ParentStream).ToArray();
 }
예제 #3
0
        static public void CreateCompression15File(String FileName, Stream OutputStream, int FallbackCompression = 15)
        {
            /*
             * if (pState->block_type == LZX_BLOCKTYPE_UNCOMPRESSED)
             * {
             *      if (pState->block_length & 1) inpos++; // realign bitstream to word
             *      INIT_BITSTREAM;
             * }
             */

            //throw (new NotImplementedException("Can't compress using LZX. Please use compressión 03 instead."));
            Stream LZXStream;

            try
            {
                LZXStream = CompressToLZXStream(FileName, FallbackCompression);
            }
            catch (InvalidDataException)
            {
                if (FallbackCompression == 15)
                {
                    LZXStream = CompressToLZXStreamRAW(FileName);
                }
                else
                {
                    OutputStream.WriteBytes(TalesCompression.CreateFromVersion(FallbackCompression).EncodeBytes(File.ReadAllBytes(FileName)));
                    return;
                }
            }

            uint UncompressedSize = (uint)(new FileInfo(FileName)).Length;
            uint CompressedSize   = (uint)LZXStream.Length;
            var  HeaderStruct     = new TalesCompression15_Lzx.HeaderStruct()
            {
                Magic               = TalesCompression15_Lzx.Signature,
                Magic1              = 0x20000,
                Magic2              = 0x80000,
                Magic3              = 0x00000,
                Magic4              = 0x00000,
                UncompressedSize    = UncompressedSize,
                UncompressedSize2   = UncompressedSize,
                CompressedSize      = CompressedSize,
                CompressedSize2     = CompressedSize,
                CompressedSizePlus4 = CompressedSize + 4,
            };

            OutputStream.WriteStruct(HeaderStruct);
            LZXStream.CopyTo(OutputStream);
            OutputStream.Flush();

            //Console.WriteLine(OutputStream.Position);
            //Console.WriteLine(0x34 + CompressedSize);

            if (OutputStream.Position != 0x34 + CompressedSize)
            {
                throw (new Exception("(OutputStream.Position != 0x34 + CompressedSize)"));
            }
        }