コード例 #1
0
        public T ReadCompressSharpLZ4(string file)
        {
            int sz = 0;

            using (FileStream fs = new FileStream(file, FileMode.Open))
            {
                using (BinaryReader binaryReader = new BinaryReader(fs))
                {
                    int length = binaryReader.ReadInt32();       //read the length first
                    //if (length < 0) { length = 0; }
                    byte[] bytesUncompressed = new byte[length]; //you can convert this back to the object using a MemoryStream ;)

                    using (var stream = new LZ4.LZ4Stream(fs, LZ4.LZ4StreamMode.Decompress, true, length))
                    {
                        try
                        {
                            while ((sz = stream.Read(bytesUncompressed, 0, bytesUncompressed.Length)) > 0)
                            {
                                // ...
                            }
                        }
                        catch (System.IO.EndOfStreamException ex) { }
                        catch (System.NotSupportedException ex) { }

                        var serializer = new DataContractSerializer(typeof(T));
                        using (var ms = new MemoryStream(bytesUncompressed))
                        {
                            //if (length == 0) { return default(T); }
                            return((T)serializer.ReadObject(ms));
                        }
                    }
                }
            }
        }
コード例 #2
0
        public static void Decode(string encoded, string decoded, int chunk)
        {
            using (var source = new RefStream(File.OpenRead(encoded), RefStreamMode.Decompress))
                using (var target = File.Create(decoded))
                {
                    var buffer = new byte[chunk];
                    while (true)
                    {
                        var read = source.Read(buffer, 0, buffer.Length);
                        if (read == 0)
                        {
                            break;
                        }

                        target.Write(buffer, 0, read);
                    }
                }
        }