Exemple #1
0
        public void BasicRoundTrip()
        {
            var ms        = new MemoryStream();
            var outStream = new BZip2OutputStream(ms);

            byte[] buf = new byte[10000];
            var    rnd = new Random();

            rnd.NextBytes(buf);

            outStream.Write(buf, 0, buf.Length);
            outStream.Close();
            ms = new MemoryStream(/*ms.GetBuffer()*/ ms.ToArray());
            ms.Seek(0, SeekOrigin.Begin);

            using (BZip2InputStream inStream = new BZip2InputStream(ms))
            {
                byte[] buf2 = new byte[buf.Length]; // How can the Inflater known the length of original data?
                int    pos  = 0;
                while (true)
                {
                    int numRead = inStream.Read(buf2, pos, 4096);
                    if (numRead <= 0)
                    {
                        break;
                    }
                    pos += numRead;
                }

                for (int i = 0; i < buf.Length; ++i)
                {
                    Assert.AreEqual(buf2[i], buf[i]);
                }
            }
        }
Exemple #2
0
        public static byte[] ZipString(string sBuffer)
        {
            MemoryStream      m_msBZip2 = null;
            BZip2OutputStream m_osBZip2 = null;

            byte[] result;
            try {
                m_msBZip2 = new MemoryStream();
                Int32 size = sBuffer.Length;
                // Prepend the compressed data with the length of the uncompressed data (firs 4 bytes)
                //
                using (BinaryWriter writer = new BinaryWriter(m_msBZip2, System.Text.Encoding.ASCII)) {
                    writer.Write(size);

                    m_osBZip2 = new BZip2OutputStream(m_msBZip2);
                    m_osBZip2.Write(Encoding.ASCII.GetBytes(sBuffer), 0, sBuffer.Length);

                    m_osBZip2.Close();
                    result = m_msBZip2.ToArray();
                    m_msBZip2.Close();

                    writer.Close();
                }
            } finally {
                if (m_osBZip2 != null)
                {
                    m_osBZip2.Dispose();
                }
                if (m_msBZip2 != null)
                {
                    m_msBZip2.Dispose();
                }
            }
            return(result);
        }
Exemple #3
0
        public static void CompressToFile(string targetFilePath, MemoryStream streamData)
        {
            // Write compressed data length and compressed data to file
            FileStream fs = null;

            try
            {
                fs = new FileStream(targetFilePath, FileMode.Create);

                // Write compressed data length
                fs.Write(BitConverter.GetBytes(streamData.Length), 0, 4);

                // Compress data
                using (BZip2OutputStream gzs = new BZip2OutputStream(fs, true))
                //ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms))
                {
                    fs = null;

                    gzs.Write(streamData.GetBuffer(), 0, (int)streamData.Length);
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }
        }
    public static string CatGame1(string str, bool isPress = false)
    {
        byte[] buffer4;
        byte[] bytes  = Encoding.UTF8.GetBytes(str);
        byte[] rgbKey = Encoding.UTF8.GetBytes("b5nHjsMrqaeNliSs3jyOzgpD");
        byte[] rgbIV  = Encoding.UTF8.GetBytes("wuD6keVr");
        TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();

        using (MemoryStream stream = new MemoryStream())
        {
            using (CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
            {
                if (isPress)
                {
                    using (BZip2OutputStream stream3 = new BZip2OutputStream(stream2))
                    {
                        stream3.Write(bytes, 0, bytes.Length);
                        stream3.Close();
                    }
                }
                else
                {
                    stream2.Write(bytes, 0, bytes.Length);
                }
                stream2.Close();
            }
            buffer4 = stream.ToArray();
            stream.Close();
        }
        return(Convert.ToBase64String(buffer4));
    }
Exemple #5
0
        //
        // inStream --> compress --> outStream
        //
        public void Compress(Stream inStream, Stream outStream)
        {
            if (inStream == null)
            {
                return;
            }
            if (outStream == null)
            {
                return;
            }

            byte[] buffer = new byte[BUFFER_SIZE];

            using (BZip2OutputStream bz2out = new BZip2OutputStream(outStream))
            {
                while (true)
                {
                    int bytesRead = inStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead <= 0)
                    {
                        break;
                    }

                    bz2out.Write(buffer, 0, bytesRead);
                }
                bz2out.Flush();
            }
        }
Exemple #6
0
        public void BasicRoundTrip()
        {
            var ms        = new MemoryStream();
            var outStream = new BZip2OutputStream(ms);

            var buf = Utils.GetDummyBytes(size: 10000, RandomSeed);

            outStream.Write(buf, offset: 0, buf.Length);
            outStream.Close();
            ms = new MemoryStream(ms.GetBuffer());
            ms.Seek(offset: 0, SeekOrigin.Begin);

            using BZip2InputStream inStream = new BZip2InputStream(ms);
            var buf2 = new byte[buf.Length];
            var pos  = 0;

            while (true)
            {
                var numRead = inStream.Read(buf2, pos, count: 4096);
                if (numRead <= 0)
                {
                    break;
                }
                pos += numRead;
            }

            for (var i = 0; i < buf.Length; ++i)
            {
                Assert.AreEqual(buf2[i], buf[i]);
            }
        }
Exemple #7
0
        override public void AddToStream(Stream stream, EventTracker tracker)
        {
            if (ChildCount > 1)
            {
                throw new Exception("Bzip2 file " + Uri + " has " + ChildCount + " children");
            }

            if (tracker != null)
            {
                tracker.ExpectingAdded(UriFu.UriToEscapedString(this.Uri));
            }

            UnclosableStream unclosable;

            unclosable = new UnclosableStream(stream);

            BZip2OutputStream bz2_out;

            bz2_out = new BZip2OutputStream(unclosable);

            MemoryStream memory;

            memory = new MemoryStream();
            // There should just be one child
            foreach (FileObject file in Children)
            {
                file.AddToStream(memory, tracker);
            }
            bz2_out.Write(memory.ToArray(), 0, (int)memory.Length);
            memory.Close();

            bz2_out.Close();
        }
Exemple #8
0
        //
        //
        //
        public MemoryStream Compress(Stream stream)
        {
            if (stream == null)
            {
                return(null);
            }

            byte[] buffer = new byte[BUFFER_SIZE];

            MemoryStream outStream = new MemoryStream();

            using (BZip2OutputStream bz2Stream = new BZip2OutputStream(outStream))
            {
                while (true)
                {
                    int bytesRead = stream.Read(buffer, 0, buffer.Length);
                    if (bytesRead <= 0)
                    {
                        break;
                    }

                    bz2Stream.Write(buffer, 0, bytesRead);
                }
                bz2Stream.Flush();
            }             // OUCH - doesnt work if you dont close the output stream

            outStream.Position = 0;
            return(outStream);
        }
        /// <summary>
        /// 지정된 데이타를 압축한다.
        /// </summary>
        /// <param name="input">압축할 Data</param>
        /// <returns>압축된 Data</returns>
        public override byte[] Compress(byte[] input)
        {
            if (IsDebugEnabled)
            {
                log.Debug(CompressorTool.SR.CompressStartMsg);
            }

            // check input data
            if (input.IsZeroLength())
            {
                if (IsDebugEnabled)
                {
                    log.Debug(CompressorTool.SR.InvalidInputDataMsg);
                }

                return(CompressorTool.EmptyBytes);
            }

            byte[] output;
            // Compress
            using (var outStream = new MemoryStream(input.Length))
                using (var bz2 = new BZip2OutputStream(outStream, CompressorTool.BUFFER_SIZE)) {
                    bz2.Write(input, 0, input.Length);
                    bz2.Close();
                    output = outStream.ToArray();
                }

            if (IsDebugEnabled)
            {
                log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);
            }

            return(output);
        }
Exemple #10
0
        /// <summary>
        /// Compress the specified sBuffer.
        /// </summary>
        /// <param name="sBuffer">S buffer.</param>
        public static string Compress(string sBuffer)
        {
            byte[] compressed     = null;
            string compressedText = null;


            try {
                byte[] bytesBuffer = Encoding.UTF8.GetBytes(sBuffer);

                using (MemoryStream compressStream = new MemoryStream()) {
                    using (BZip2OutputStream bzipStream = new BZip2OutputStream(compressStream)) {
                        bzipStream.Write(bytesBuffer, 0, bytesBuffer.Length);
                        bzipStream.Flush();
                        bzipStream.Close();

                        compressed = compressStream.ToArray();
                    }
                }
            } catch (Exception ex) {
                ConsoleEx.DebugLog("BZip2 compress error : " + ex.ToString());
            } finally {
                if (compressed != null)
                {
                    compressedText = Convert.ToBase64String(compressed);
                }
            }

            return(compressedText);
        }
    /*
     *  String - CompressString() > DecompressString()
     *  String - ComrpessStringToBytes() > DecompressStringFromBytes()
     *
     *  Bytes - CompressBytes() > DecompressBytes()
     *  Bytes - CompressBytesToString() > DecompressBytesFromString()
     */

    #region CompressBytes

    /// <summary>
    /// Compresses a byte array
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] CompressBytes(this byte[] data)
    {
        // empty if null
        if (data.IsNullOrEmpty())
        {
            return(null);
        }

        using (MemoryStream msBZip2 = new MemoryStream())
        {
            int size = data.Length;

            // Prepend the compressed data with the length of the uncompressed data (firs 4 bytes)
            using (BinaryWriter writer = new BinaryWriter(msBZip2))
            {
                writer.Write(size);

                using (BZip2OutputStream BZip2OutStream = new BZip2OutputStream(msBZip2))
                {
                    BZip2OutStream.Write(data, 0, size);
                }
            }

            // return the compressed data
            return(msBZip2.ToArray());
        }
    }
    public void SaveAs(string name)
    {
        string          Fullname = Application.persistentDataPath + "/" + Game.current.Filename + "-" + name;
        BinaryFormatter bf       = new BinaryFormatter();
        //Save the recording in its own file
        FileStream file;

        file = File.Create(Fullname + ".rcd"); //you can call it anything you want
        bf.Serialize(file, this.Serialize());
        file.Close();
        return;

        //This zipper takes 5 times as long and shrinks the file to 1/3 size
        using (FileStream fs = new FileStream(Fullname + ".zip", FileMode.Create))
        {
            using (MemoryStream objectSerialization = new MemoryStream())
            {
                BinaryFormatter bformatter = new BinaryFormatter();
                bformatter.Serialize(objectSerialization, this.Serialize());

                using (BinaryWriter binaryWriter = new BinaryWriter(fs))
                {
                    binaryWriter.Write(objectSerialization.GetBuffer().Length); //write the length first

                    using (BZip2OutputStream osBZip2 = new BZip2OutputStream(fs))
                    {
                        osBZip2.Write(objectSerialization.GetBuffer(), 0, objectSerialization.GetBuffer().Length); //write the compressed file
                    }
                }
            }
        }
    }
Exemple #13
0
        public void BasicRoundTrip()
        {
            MemoryStream      ms        = new MemoryStream();
            BZip2OutputStream outStream = new BZip2OutputStream(ms);

            byte[]        buf = new byte[10000];
            System.Random rnd = new Random();
            rnd.NextBytes(buf);

            outStream.Write(buf, 0, buf.Length);
            outStream.Close();
            ms = new MemoryStream(ms.GetBuffer());
            ms.Seek(0, SeekOrigin.Begin);

            using (BZip2InputStream inStream = new BZip2InputStream(ms))
            {
                byte[] buf2 = new byte[buf.Length];
                int    pos  = 0;
                while (true)
                {
                    int numRead = inStream.Read(buf2, pos, 4096);
                    if (numRead <= 0)
                    {
                        break;
                    }
                    pos += numRead;
                }

                for (int i = 0; i < buf.Length; ++i)
                {
                    Assert.AreEqual(buf2[i], buf[i]);
                }
            }
        }
    // Token: 0x06003B90 RID: 15248 RVA: 0x00138434 File Offset: 0x00136634
    public static string CatGame1(string str, bool isCompress = false)
    {
        byte[] bytes  = Encoding.UTF8.GetBytes(str);
        byte[] bytes2 = Encoding.UTF8.GetBytes("b************c**********");
        byte[] bytes3 = Encoding.UTF8.GetBytes("a****a**");
        TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();

        byte[] inArray;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, tripleDESCryptoServiceProvider.CreateEncryptor(bytes2, bytes3), CryptoStreamMode.Write))
            {
                if (isCompress)
                {
                    using (BZip2OutputStream bzip2OutputStream = new BZip2OutputStream(cryptoStream))
                    {
                        bzip2OutputStream.Write(bytes, 0, bytes.Length);
                        bzip2OutputStream.Close();
                    }
                }
                else
                {
                    cryptoStream.Write(bytes, 0, bytes.Length);
                }
                cryptoStream.Close();
            }
            inArray = memoryStream.ToArray();
            memoryStream.Close();
        }
        return(Convert.ToBase64String(inArray));
    }
 public virtual byte[] Compress(byte[] decompressedData)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         if (this.Algorithm == CompressionAlgorithm.GZip)
         {
             GZipStream stream2 = new GZipStream(ms, CompressionMode.Compress, true);
             stream2.Write(decompressedData, 0, decompressedData.Length);
             stream2.Close();
         }
         else if (this.Algorithm == CompressionAlgorithm.Deflate)
         {
             DeflateStream stream3 = new DeflateStream(ms, CompressionMode.Compress, true);
             stream3.Write(decompressedData, 0, decompressedData.Length);
             stream3.Close();
         }
         else
         {
             var bzipStream = new BZip2OutputStream(ms);
             bzipStream.Write(decompressedData, 0, decompressedData.Length);
             bzipStream.Close();
         }
         return(ms.ToArray());
     }
 }
Exemple #16
0
    /// <summary>
    /// BZip2压缩
    /// </summary>
    /// <param name="rawData"></param>
    /// <returns></returns>
    public static byte[] BZip2Compress(byte[] rawData)
    {
        MemoryStream      ms    = new MemoryStream();
        BZip2OutputStream BZip2 = new BZip2OutputStream(ms);

        BZip2.Write(rawData, 0, rawData.Length);
        BZip2.Close();
        return(ms.ToArray());
    }
Exemple #17
0
 /// <summary>
 /// 压缩
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public static byte[] Compress(byte[] input)
 {
     using (MemoryStream outputStream = new MemoryStream()) {
         using (BZip2OutputStream zipStream = new BZip2OutputStream(outputStream)) {
             zipStream.Write(input, 0, input.Length);
             zipStream.Close();
         }
         return(outputStream.ToArray());
     }
 }
Exemple #18
0
        /// <summary>
        /// BZip2压缩
        /// </summary>
        /// <param name="data">源字节数组</param>
        /// <param name="level">压缩率,1-9,数字越大压缩率越高</param>
        /// <returns></returns>
        public static byte[] BZip2(byte[] data, int level)
        {
            using var mstream = new MemoryStream();

            using var zipOutStream = new BZip2OutputStream(mstream, level);
            zipOutStream.Write(data, 0, data.Length);
            byte[] result = mstream.ToArray();

            return(result);
        }
Exemple #19
0
 /// <summary>
 /// TODO: description
 /// </summary>
 /// <param name="sourcePath"></param>
 /// <param name="targetPath"></param>
 /// <param name="deltaPath"></param>
 public void Create(string sourcePath, string targetPath, string deltaPath)
 {
     Logger.Info("Creating Fossil patch at {0} from {1} to {2}", deltaPath, sourcePath, targetPath);
     using (FileStream patch = File.Open(deltaPath, FileMode.Create, FileAccess.Write))
         using (BZip2OutputStream zip = new BZip2OutputStream(patch))
         {
             byte[] patchData = Fossil.Delta.Create(File.ReadAllBytes(sourcePath), File.ReadAllBytes(targetPath));
             zip.Write(patchData, 0, patchData.Length);
         }
 }
Exemple #20
0
 /// <summary>
 /// 压缩
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public static string Compress(string input)
 {
     byte[] buffer = Encoding.UTF8.GetBytes(input);
     using (MemoryStream outputStream = new MemoryStream()) {
         using (BZip2OutputStream zipStream = new BZip2OutputStream(outputStream)) {
             zipStream.Write(buffer, 0, buffer.Length);
             zipStream.Close();
         }
         return(Convert.ToBase64String(outputStream.ToArray()));
     }
 }
Exemple #21
0
    public static byte[] ZipStream(byte[] sBuffer)
    {
        MemoryStream m_msBZip2 = null;

        BZip2OutputStream m_osBZip2 = null;

        byte[] result;

        try
        {
            m_msBZip2 = new MemoryStream();

            Int32 size = sBuffer.Length;


            using (BinaryWriter writer = new BinaryWriter(m_msBZip2, System.Text.Encoding.ASCII))
            {
                writer.Write(size);



                m_osBZip2 = new BZip2OutputStream(m_msBZip2);

                m_osBZip2.Write(sBuffer, 0, sBuffer.Length);



                m_osBZip2.Close();

                result = m_msBZip2.ToArray();

                m_msBZip2.Close();



                writer.Close();
            }
        }

        finally
        {
            if (m_osBZip2 != null)
            {
                m_osBZip2.Dispose();
            }

            if (m_msBZip2 != null)
            {
                m_msBZip2.Dispose();
            }
        }

        return(result);
    }
        public static byte[] Compress(byte[] input)
        {
            Contract.Requires(input != null);
            Contract.Ensures(Contract.Result <byte[]>() != null);

            var length = input.Length;
            var ms     = new MemoryStream(length);

            using (var stream = new BZip2OutputStream(ms))
                stream.Write(input, 0, length);

            return(ms.ToArray());
        }
Exemple #23
0
    public static string Compress(string input)
    {
        using (var outputMemoryStream = new MemoryStream())
        {
            using (var zipStream = new BZip2OutputStream(outputMemoryStream))
            {
                var inputBytes = Encoding.UTF8.GetBytes(input);
                zipStream.Write(inputBytes, 0, inputBytes.Length);
            }

            var outputBytes = outputMemoryStream.ToArray();
            return(Convert.ToBase64String(outputBytes));
        }
    }
Exemple #24
0
    public static byte[] CatHomeMain(byte[] data, byte[] home, byte[] info, bool isCompress = false)
    {
        MemoryStream memoryStream = null;
        CryptoStream cryptoStream = null;

        byte[] result;
        try
        {
            var transform = new RijndaelManaged
            {
                Padding   = PaddingMode.PKCS7,
                Mode      = CipherMode.CBC,
                KeySize   = 256,
                BlockSize = 256
            }.CreateEncryptor(home, info);
            memoryStream = new MemoryStream();
            cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
            if (isCompress)
            {
                var bzip2OutputStream = new BZip2OutputStream(cryptoStream, 1);
                bzip2OutputStream.Write(data, 0, data.Length);
                bzip2OutputStream.Close();
            }
            else
            {
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.FlushFinalBlock();
            }

            result = memoryStream.ToArray();
        }
        catch (Exception)
        {
            result = null;
        }
        finally
        {
            if (memoryStream != null)
            {
                memoryStream.Close();
            }
            if (cryptoStream != null)
            {
                cryptoStream.Close();
            }
        }

        return(result);
    }
Exemple #25
0
        public static byte[] ZipCompress(byte[] byte_0)
        {
            MemoryStream stream  = new MemoryStream();
            Stream       stream2 = new BZip2OutputStream(stream);

            try
            {
                stream2.Write(byte_0, 0, byte_0.Length);
            }
            finally
            {
                stream2.Close();
                stream.Close();
            }
            return(stream.ToArray());
        }
Exemple #26
0
        /// <summary>
        /// Function to compress data.
        /// </summary>
        /// <param name="inStream">Input stream.</param>
        /// <param name="outStream">Output stream.</param>
        private void CompressData(Stream inStream, Stream outStream)
        {
            if (_writeBuffer == null)
            {
                _writeBuffer = new byte[81920];
            }

            Debug.Assert(outStream != null, "outStream != null");

            using (var bzStream = new BZip2OutputStream(outStream, _compressionRatio))
            {
                long streamSize = inStream.Length;
                bzStream.IsStreamOwner = false;

                while (streamSize > 0)
                {
                    if (_token.IsCancellationRequested)
                    {
                        return;
                    }

                    int readSize = inStream.Read(_writeBuffer, 0, 81920);

                    if (_token.IsCancellationRequested)
                    {
                        return;
                    }

                    if (readSize > 0)
                    {
                        if (_token.IsCancellationRequested)
                        {
                            return;
                        }

                        bzStream.Write(_writeBuffer, 0, readSize);

                        if (_token.IsCancellationRequested)
                        {
                            return;
                        }
                    }

                    streamSize -= readSize;
                }
            }
        }
    public static byte[] CatHomeMain(byte[] data, byte[] home, byte[] info, bool isPress = false)
    {
        MemoryStream stream  = null;
        CryptoStream stream2 = null;

        byte[] buffer;
        try
        {
            ICryptoTransform transform = new RijndaelManaged
            {
                Padding   = PaddingMode.PKCS7,
                Mode      = CipherMode.CBC,
                KeySize   = 0x100,
                BlockSize = 0x100
            }.CreateEncryptor(home, info);
            stream  = new MemoryStream();
            stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            if (isPress)
            {
                BZip2OutputStream stream3 = new BZip2OutputStream(stream2);
                stream3.Write(data, 0, data.Length);
                stream3.Close();
            }
            else
            {
                stream2.Write(data, 0, data.Length);
                stream2.FlushFinalBlock();
            }
            buffer = stream.ToArray();
        }
        catch (Exception)
        {
            buffer = null;
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
            }
            if (stream2 != null)
            {
                stream2.Close();
            }
        }
        return(buffer);
    }
Exemple #28
0
        static public byte[] BZip2CompressArray(byte[] data)
        {
            MemoryStream ms = new MemoryStream();

            using (BZip2OutputStream bz = new BZip2OutputStream(ms, 9))
            {
                bz.IsStreamOwner = false;
                bz.Write(data, 0, data.Length);
            }
            ms.Position = 0;
            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);
            byte[] gzBuffer = new byte[compressed.Length + 4];
            System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            System.Buffer.BlockCopy(BitConverter.GetBytes(data.Length), 0, gzBuffer, 0, 4);
            return(gzBuffer);
        }
Exemple #29
0
        /// <summary>
        /// Compress <paramref name="instream">input stream</paramref> sending
        /// result to <paramref name="outputstream">output stream</paramref>
        /// </summary>
        public static long Compress(Stream instream, Stream outstream, int blockSize)
        {
            System.IO.Stream bos = outstream;
            System.IO.Stream bis = instream;
            int ch;
            BZip2OutputStream bzos = new BZip2OutputStream(bos, blockSize);

            byte[] buffer = new byte[512];
            while ((ch = bis.Read(buffer, 0, buffer.Length)) != 0)
            {
                bzos.Write(buffer, 0, ch);
            }
            bis.Close();
            bzos.Flush();
            bzos.Close();
            return(bzos.Size);
        }
Exemple #30
0
        void WriteTargetBytes()
        {
            const int Size = 8192;

            byte[] buffer = new byte[Size];

            while (writeTarget_ > 0)
            {
                int thisTime = Size;
                if (thisTime > writeTarget_)
                {
                    thisTime = (int)writeTarget_;
                }

                outStream_.Write(buffer, 0, thisTime);
                writeTarget_ -= thisTime;
            }
        }