Ejemplo n.º 1
0
        public void Save()
        {
            const uint version       = 0;
            var        regionsFolder = _world.Folder + Path.DirectorySeparatorChar + RegionsFolder;

            FolderUtils.CreateRegionFolderIfNotExist(regionsFolder);
            var file = regionsFolder + Path.DirectorySeparatorChar + _position.X + "-" + _position.Y + "-" + _position.Z + ".reg";

            var stream    = new DeflateStream(File.Open(file, FileMode.OpenOrCreate, FileAccess.Write), CompressionMode.Compress);
            var formatter = new BinaryFormatter();

            formatter.Serialize(stream, version);

            ForEachRegionPosition((x, y, z) => {
                var chunk = _savedChunks[x, y, z];
                var save  = chunk != null && !chunk.Natural;
                stream.WriteByte(save ? (byte)1 : (byte)0);
                if (!save)
                {
                    return;
                }
                chunk.Save(stream, formatter);
            });

            stream.Close();
        }
Ejemplo n.º 2
0
        static unsafe byte[] CompressFlipRb3(byte[] data, int height, int stride)
        {
            using var compressStream = new MemoryStream();
            compressStream.Write(FakeDeflateHeader, 0, FakeDeflateHeader.Length);

            using (var compressor = new DeflateStream(compressStream, CompressionLevel.Fastest))
            {
                fixed(byte *srcPtr = data)
                {
                    byte *srcPtrOff = srcPtr;

                    for (int v = 0; v < height; v++)
                    {
                        compressor.WriteByte(0);
                        for (int u = 0; u < stride; u += 3, srcPtrOff += 3)
                        {
                            byte tmp = srcPtrOff[0];
                            srcPtrOff[0] = srcPtrOff[2];
                            srcPtrOff[2] = tmp;
                        }

                        compressor.Write(data, v * stride, stride);
                    }
                }

                compressor.Flush();
                compressor.Close();
            }

            return(compressStream.ToArray());
        }
Ejemplo n.º 3
0
        static void Main()
        {
            // Создание файла и архива.
            // Файл из которого будем делать архив
            FileStream source = File.OpenRead(@"D:\test.txt");
            // Создаем файл "архив"
            FileStream destination = File.Create(@"D:\archive.dfl");

            // Создание компрессора.
            //Предоставляет методы и свойства для сжатия и распаковки потоков с использованием
            //     алгоритма Deflate.
            DeflateStream compressor = new DeflateStream(destination, CompressionMode.Compress);

            // Заполнение архива информацией из файла.
            int theByte = source.ReadByte();

            while (theByte != -1)
            {
                compressor.WriteByte((byte)theByte);
                theByte = source.ReadByte();
            }

            // Удаление компрессора.
            compressor.Close();
        }
Ejemplo n.º 4
0
        //Некоторые архивы имеют заголовки - например ZIP, а Deflate - не имеют таких заголовков
        //по этому я и не могу открыть его через стандартный архиватор.
        static void Main(string[] args)
        {
            //Создание файла
            FileStream fileStream = File.Open(@"D:\test_dfl_1.txt", FileMode.OpenOrCreate);

            //Запись в файл
            StreamWriter writer = new StreamWriter(fileStream);

            writer.WriteLine("Privet, Roman from Deflate");

            //Создание архива
            FileStream archiveStream = File.Create(@"D:\archive_dfl.dfl");

            DeflateStream compressor = new DeflateStream(archiveStream, CompressionMode.Compress);

            // тут лучше применить do while!!!!! смотри пример 003_Zip_Decompression
            int theByte = fileStream.ReadByte();

            while (theByte != -1)
            {
                compressor.WriteByte((byte)theByte);
                theByte = fileStream.ReadByte();
            }

            writer.Close();
            compressor.Close();
        }
Ejemplo n.º 5
0
        static Patch processChunk(byte[] orig, byte[] mod, int chunkStart, int chunkEnd)
        {
            int patchSize = chunkEnd - chunkStart;

            Debug.Assert(patchSize < Patch.MaxPatchSize);
            int matchAreaSize = Patch.MatchAreaSize(patchSize);
            int matchBefore   = (matchAreaSize - patchSize) / 2;
            int matchAfter    = matchAreaSize - (patchSize + matchBefore);

            Debug.Assert(matchBefore <= chunkStart);
            Debug.Assert(chunkEnd + matchAfter <= orig.Length);

            byte[] origBytes  = new byte[matchAreaSize];
            byte[] patchBytes = new byte[patchSize];
            Array.ConstrainedCopy(orig, chunkStart - matchBefore, origBytes, 0, matchAreaSize);
            Array.ConstrainedCopy(mod, chunkStart, patchBytes, 0, patchSize);
            MemoryStream  ms   = new MemoryStream();
            DeflateStream comp = new DeflateStream(ms, CompressionMode.Compress);

            comp.WriteByte(0);
            comp.Flush();
            long base_len = ms.Length;

            comp.Write(origBytes, 0, origBytes.Length);
            comp.Flush();
            return(new Patch(origBytes, patchBytes, matchBefore, Convert.ToInt32(ms.Length - base_len)));
        }
Ejemplo n.º 6
0
 public override void WriteByte(byte value)
 {
     if (CanWrite)
     {
         _DeflateStream.WriteByte(value);
         _Adler32.Update(value);
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Ejemplo n.º 7
0
 public override void WriteByte(byte value)
 {
     if (this.CanWrite == true)
     {
         _deflateStream.WriteByte(value);
         _adler32.Update(value);
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Ejemplo n.º 8
0
        public byte[] Deflate(byte[] data)
        {
            using (MemoryStream output = new MemoryStream())
            {
                using (DeflateStream gzip =
                           new DeflateStream(output, CompressionMode.Compress))
                {
                    foreach (byte bajt in data)
                    {
                        gzip.WriteByte(bajt);
                    }
                }

                return(output.ToArray());
            }
        }
Ejemplo n.º 9
0
        public unsafe void Save(string path)
        {
            using (Stream s = File.Open(path, FileMode.Create, FileAccess.Write)) {
                s.Write(Signature, 0, Signature.Length);

                using (MemoryStream ms = new MemoryStream()) {
                    ms.Write(IPAddress.HostToNetworkOrder(width));
                    ms.Write(IPAddress.HostToNetworkOrder(height));
                    ms.WriteByte(8);     // Bit Depth

                    ms.WriteByte(2 | 4); // Color Type

                    ms.WriteByte(0);     // Compression
                    ms.WriteByte(0);     // Filter
                    ms.WriteByte(0);     // Interlacing

                    WriteChunk(s, "IHDR", ms);
                }

                using (MemoryStream ms = new MemoryStream()) {
                    ms.WriteByte(0x78); // Compression Method and Flags
                    ms.WriteByte(0x9c); // Flags

                    using (Stream compressed = new DeflateStream(ms, CompressionLevel.Optimal, true))
                    {
                        fixed(ColorRgba *ptr = data)
                        {
                            int stride = width * 4;

                            byte[] buffer = new byte[stride];

                            for (var y = 0; y < Height; y++)
                            {
                                Marshal.Copy(new IntPtr((byte *)ptr + y * stride), buffer, 0, stride);

                                compressed.WriteByte(0);             // Filter - None
                                compressed.Write(buffer, 0, stride); // Row
                            }
                        }
                    }

                    WriteChunk(s, "IDAT", ms);
                }

                WriteChunk(s, "IEND", null);
            }
        }
Ejemplo n.º 10
0
        static void Main()
        {
            FileStream source      = File.OpenRead(@"D:\test.txt");
            FileStream destination = File.Create(@"D:\archive.dfl");

            DeflateStream compressor = new DeflateStream(destination, CompressionMode.Compress);

            int theByte = source.ReadByte();

            while (theByte != -1)
            {
                compressor.WriteByte((byte)theByte);
                theByte = source.ReadByte();
            }

            compressor.Close();
        }
Ejemplo n.º 11
0
        /*
         * static byte[] Compress(byte[] data)
         * {
         *  using (var compressStream = new MemoryStream())
         *  using (var compressor = new DeflateStream(compressStream, CompressionLevel.Fastest))
         *  {
         *      compressStream.Write(FakeDeflateHeader, 0, FakeDeflateHeader.Length);
         *      compressor.Write(data, 0, data.Length);
         *      compressor.Close();
         *      return compressStream.ToArray();
         *  }
         * }
         */

        static byte[] Compress(byte[] data, int height, int stride)
        {
            using var compressStream = new MemoryStream();
            compressStream.Write(FakeDeflateHeader, 0, FakeDeflateHeader.Length);

            using (var compressor = new DeflateStream(compressStream, CompressionLevel.Fastest))
            {
                for (int v = 0, offset = 0; v < height; v++, offset += stride)
                {
                    compressor.WriteByte(0);
                    compressor.Write(data, offset, stride);
                }

                compressor.Flush();
                compressor.Close();
            }

            return(compressStream.ToArray());
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Write func for the CWLS-format<para />
        /// For information on the format check the load/read func
        /// </summary>
        public static void write_recents(string file, IEnumerable <string> recents)
        {
            dbg("[CWLS]Writing file...");
            Stream fs = File.Open(file, Create, FileAccess.Write);

            fs.write(cwls_header);
            fs.WriteByte(5);
            dbg("[CWLS]Wrote header.");
            DeflateStream d = new DeflateStream(fs, CompressionLevel.Optimal, false);

            foreach (string r in recents)
            {
                d.write(utf8(r));
                d.WriteByte(11);
                dbg("[CWLS]Wrote \"" + r + "\".");
            }
            d.Close();
            dbg("[CWLS]Finished.");
        }
Ejemplo n.º 13
0
        private void btnZip_Click(object sender, EventArgs e)
        {
            FileInfo   info        = new FileInfo(listBox1.SelectedItem.ToString());
            FileStream source      = File.OpenRead(listBox1.SelectedItem.ToString());
            FileStream destination = File.Create(listBox1.SelectedItem.ToString() + ".dfl");

            using (DeflateStream compressor = new DeflateStream(destination, CompressionMode.Compress))
            {
                int theByte = source.ReadByte();
                while (theByte != -1)
                {
                    compressor.WriteByte((byte)theByte);
                    theByte = source.ReadByte();
                }
                label2.Text = "Архив был создан:" + info.Name + ".dfl";
            }

            source.Close();
            destination.Close();
        }
Ejemplo n.º 14
0
        static void Deflate_NoRepetition()
        {
            using (Stream s = File.Create("compressed.bin"))
                using (Stream ds = new DeflateStream(s, CompressionMode.Compress))
                    for (byte i = 0; i < 100; i++)
                    {
                        ds.WriteByte(i);
                    }

            Console.WriteLine($"Size on disk: {new FileInfo("compressed.bin").Length}");

            using (Stream s = File.OpenRead("compressed.bin"))
                using (Stream ds = new DeflateStream(s, CompressionMode.Decompress))
                    for (byte i = 0; i < 100; i++)
                    {
                        Console.WriteLine(ds.ReadByte());
                    }                                     // Writes 0 to 99

            File.Delete("compressed.bin");
        }
Ejemplo n.º 15
0
        [Category("StaticLinkedAotNotWorking")]          // Native MPH loading issues
        public void JunkAtTheEnd()
        {
            // Write a deflated stream, then some additional data...
            using (MemoryStream ms = new MemoryStream())
            {
                // The compressed stream
                using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true))
                {
                    stream.WriteByte(1);
                    stream.Flush();
                }
                // Junk
                ms.WriteByte(2);

                ms.Position = 0;
                // Reading: this should not hang
                using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress))
                {
                    byte[] buffer = new byte[512];
                    int    len    = stream.Read(buffer, 0, buffer.Length);
                    Console.WriteLine(len == 1);
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Writes MLArrays into <c>OutputStream</c>
        /// </summary>
        /// <remarks>
        /// Writes MAT-file header and compressed data (<c>miCompressed</c>).
        /// </remarks>
        /// <param name="stream"><c>Stream</c></param>
        /// <param name="data"><c>Collection</c> of <c>MLArray</c> elements.</param>
        /// <param name="compress">Use data compression?</param>
        public MatFileWriter(BinaryWriter stream, ICollection data, bool compress)
        {
            // Write header
            WriteHeader(stream);

            foreach (MLArray matrix in data)
            {
                if (compress)
                {
                    // prepare buffer for MATRIX data
                    MemoryStream memstrm = new MemoryStream();
                    BinaryWriter bw      = new BinaryWriter(memstrm);
                    WriteMatrix(bw, matrix); // Write MATRIX bytes into buffer
                    memstrm.Position = 0;    // Rewind the stream

                    // Compress data to save storage
                    MemoryStream compressed = new MemoryStream();

                    uint s1 = 1, s2 = 0, crc = 0; // Adler-32 CRC
                    using (DeflateStream df = new DeflateStream(compressed, CompressionMode.Compress, true))
                    {
                        int readByte;
                        do
                        {
                            readByte = memstrm.ReadByte();
                            if (readByte != -1)
                            {
                                df.WriteByte((byte)readByte);
                                s1 = (s1 + (byte)readByte) % 0xFFF1;
                                s2 = (s2 + s1) % 0xFFF1;
                            }
                        } while (readByte != -1);
                        crc = (s2 << 16) | s1;
                    }

                    compressed.Position = 0;

                    // zip RFC 1950
                    byte[] compressedBytes = new byte[compressed.Length + 6];
                    compressedBytes[0] = 0x78;
                    compressedBytes[1] = 0x9c;

                    for (int i = 2; i < compressedBytes.Length - 4; i++)
                    {
                        compressedBytes[i] = (byte)compressed.ReadByte();
                    }
                    BitConverter.GetBytes(crc).CopyTo(compressedBytes, compressedBytes.Length - 4);

                    // write COMPRESSED tag and compressed data into output channel
                    ByteBuffer buf         = new ByteBuffer(2 * 4 + compressedBytes.Length);
                    buf.PutInt(MatDataTypes.miCOMPRESSED);
                    buf.PutInt(compressedBytes.Length);
                    buf.Put(compressedBytes, 0, compressedBytes.Length);

                    stream.Write(buf.Array());

                    compressed.Close();
                }
                else
                {
                    // Write MATRIX bytes into buffer
                    WriteMatrix(stream, matrix);
                }
            }
            stream.Close();
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Writes MLArrays into <c>OutputStream</c>
        /// </summary>
        /// <remarks>
        /// Writes MAT-file header and compressed data (<c>miCompressed</c>).
        /// </remarks>
        /// <param name="stream"><c>Stream</c></param>
        /// <param name="data"><c>Collection</c> of <c>MLArray</c> elements.</param>
        /// <param name="compress">Use data compression?</param>
        public MatFileWriter(BinaryWriter stream, ICollection data, bool compress)
        {
            // Write header
            WriteHeader(stream);

            foreach (MLArray matrix in data)
            {
                if (compress)
                {
                    // Prepare buffer for MATRIX data
                    MemoryStream memstrm = new MemoryStream();
                    BinaryWriter bw      = new BinaryWriter(memstrm);
                    WriteMatrix(bw, matrix);                  // Write MATRIX bytes into buffer
                    memstrm.Position = 0;                     // Rewind the stream

                    // Compress data to save storage
                    MemoryStream compressed = new MemoryStream();
#if NET20
                    zlib.ZOutputStream zos   = new zlib.ZOutputStream(compressed, -1);
                    byte[]             input = new byte[128];
                    int len = 0;
                    while ((len = memstrm.Read(input, 0, input.Length)) > 0)
                    {
                        zos.Write(input, 0, len);
                    }
                    zos.Flush();
                    zos.Close();  // important to note that the zlib.ZOutputStream needs to close before writting out the data to the Base.stream

                    byte[] compressedBytes = compressed.ToArray();
#endif
#if NET40
                    uint s1 = 1, s2 = 0, crc = 0;                     // Adler-32 CRC
                    using (DeflateStream df = new DeflateStream(compressed, CompressionMode.Compress, true))
                    {
                        int readByte;
                        do
                        {
                            readByte = memstrm.ReadByte();
                            if (readByte != -1)
                            {
                                df.WriteByte((byte)readByte);
                                s1 = (s1 + (byte)readByte) % 0xFFF1;
                                s2 = (s2 + s1) % 0xFFF1;
                            }
                        }while (readByte != -1);
                        crc = (s2 << 16) | s1;
                    }

                    compressed.Position = 0;

                    // zip RFC 1950
                    byte[] compressedBytes = new byte[compressed.Length + 6];
                    compressedBytes[0] = 0x78;
                    compressedBytes[1] = 0x9c;
                    for (int i = 2; i < compressedBytes.Length - 4; i++)
                    {
                        compressedBytes[i] = (byte)compressed.ReadByte();
                    }
                    BitConverter.GetBytes(crc).CopyTo(compressedBytes, compressedBytes.Length - 4);
#endif

                    // write COMPRESSED tag and compressed data into output channel
                    ByteBuffer buf         = new ByteBuffer(2 * 4 + compressedBytes.Length);
                    buf.PutInt(MatDataTypes.miCOMPRESSED);
                    buf.PutInt(compressedBytes.Length);
                    buf.Put(compressedBytes, 0, compressedBytes.Length);

                    stream.Write(buf.Array());

                    compressed.Close();
                }
                else
                {
                    // Write MATRIX bytes into buffer
                    WriteMatrix(stream, matrix);
                }
            }

            stream.Close();
        }
Ejemplo n.º 18
0
        public async Task ZipStreamTest()
        {
            //两个通用的压缩流: GZipStream, DeflateStream
            //两者的区别在于GZipStream会在开头和结尾写入额外的协议信息, 其中包括检测错误CRC
            //它们的构造函数接受底层流参数, 并将数据压缩写入底层流或者从底层流读取数据并解压

            #region 文件数据压缩

            //高密度的非重复的二进制文件压缩效果很差(缺少设计规范的加密数据的压缩比是最差的)

            //高密度的非重复的二进制文件的压缩, 压缩后的文件大小和压缩前的文件大小几乎不变
            //压缩
            using (var sCompress = File.Create("ZipStreamTestCompress.bin"))
                using (var sNoCompress = File.Create("ZipStreamTestNoCompress.bin"))
                    using (var dsCompress = new DeflateStream(sCompress, CompressionMode.Compress))
                        using (var dsNoCompress = new DeflateStream(sNoCompress, CompressionMode.Compress))
                        {
                            for (byte i = 0; i < 100; i++)
                            {
                                dsCompress.WriteByte(i);
                                dsNoCompress.WriteByte(i);
                            }
                        }
            //解压
            using (var s = File.OpenRead("ZipStreamTestCompress.bin"))
                using (var ds = new DeflateStream(s, CompressionMode.Decompress))
                {
                    for (byte i = 0; i < 100; i++)
                    {
                        var b = ds.ReadByte();
                    }
                }

            //高重复的二进制文件的压缩, 由5KB压缩至1KB
            //下例从一个简短的句子中随机抽取1000个单词形成文本流, 并对该文本流进行压缩
            string[] words = "The quick brown fox jumps over the lazy dog".Split(" ");
            var      rand  = new Random();

            using (var sCompress = File.Create("ZipStreamTestCompress.bin"))
                using (var sNoCompress = File.Create("ZipStreamTestNoCompress.bin"))
                    using (var dsCompress = new DeflateStream(sCompress, CompressionMode.Compress))
                        using (var writerCompress = new StreamWriter(dsCompress))
                            using (var writerNoCompress = new StreamWriter(sNoCompress))
                            {
                                for (int i = 0; i < 1000; i++)
                                {
                                    await writerCompress.WriteAsync(words[rand.Next(words.Length)] + " ");

                                    await writerNoCompress.WriteAsync(words[rand.Next(words.Length)] + " ");
                                }
                            }

            #endregion

            #region 内存数据压缩

            //DeflateStream的Dispose是非常标准的关闭流的方法, 它会清理该过程中所有未写入的缓存, 也会关闭对应的底层流
            //如果不希望它关闭对应的底层流, 可以通过向Deflate的构造器中传入额外的标记(leaveOpen)实现

            var bytes = new byte[1000];
            var noCompressionLength = bytes.Length; //1000

            //压缩
            var ms = new MemoryStream();
            using (var ds = new DeflateStream(ms, CompressionMode.Compress))
            //using (var ds = new DeflateStream(ms, CompressionMode.Compress, true))    //不关闭底层流的写法
            {
                ds.Write(bytes, 0, bytes.Length);
            }

            var bytesCompress     = ms.ToArray();
            var compressionLength = bytesCompress.Length;   //11

            //解压
            ms = new MemoryStream(bytesCompress);
            using (var ds = new DeflateStream(ms, CompressionMode.Decompress))
            {
                for (var i = 0; i < 1000; i += ds.Read(bytes, i, 1000 - i))
                {
                    ;
                }
            }

            #endregion
        }
        /// <summary>
        /// Compresses the target image by saving its differences from the source image and
        /// write the result to the input stream.
        /// If the source image is <c>null</c> or not of the same size as the target image, the
        /// target image written to the stream in PNG format.
        /// </summary>
        public static unsafe void CompressByRawBlocks(
            Bitmap target,
            byte[] targetEncoded,
            Bitmap source,
            string sourceId,
            Stream stream,
            int blockSize = 10)
        {
            ArgumentGuard.NotNull(target, nameof(target));
            ArgumentGuard.NotNull(targetEncoded, nameof(targetEncoded));
            ArgumentGuard.NotNull(stream, nameof(stream));

            try
            {
                Size?sourceSize = source?.Size;
                if (sourceSize == null || sourceSize != target.Size)
                {
                    stream.Write(targetEncoded, 0, targetEncoded.Length);
                    return;
                }
            }
            catch (ArgumentException)
            {
                stream.Write(targetEncoded, 0, targetEncoded.Length);
                return;
            }

            var columns = (target.Width / blockSize) + ((target.Width % blockSize) == 0 ? 0 : 1);
            var rows    = (target.Height / blockSize) + ((target.Height % blockSize) == 0 ? 0 : 1);

            WriteHeader(FormatRawBlocks_, sourceId ?? string.Empty, stream);
            CommonUtils.ToBytesBE((short)blockSize, stream);

            BitmapData sbd = null;
            BitmapData tbd = null;

            try
            {
                var rect = new Rectangle(Point.Empty, source.Size);
                sbd = source.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                tbd = target.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                var channelBytes = new byte[blockSize * blockSize];
                using (var compressed = new DeflateStream(stream, CompressionLevel.Optimal, true))
                {
                    fixed(byte *channelBytesPtr = channelBytes)
                    {
                        for (byte channel = 0; channel < 3; ++channel)
                        {
                            var blockNumber = 0;
                            for (int row = 0; row < rows; ++row)
                            {
                                for (int column = 0; column < columns; ++column)
                                {
                                    if (!CompareAndCopyBlockChannelData(
                                            (byte *)sbd.Scan0,
                                            (byte *)tbd.Scan0,
                                            source.Size,
                                            sbd.Stride,
                                            blockSize,
                                            column,
                                            row,
                                            channel,
                                            channelBytesPtr,
                                            out int count))
                                    {
                                        compressed.WriteByte(channel);
                                        CommonUtils.ToBytesBE(blockNumber, compressed);
                                        compressed.Write(channelBytes, 0, count);

                                        if (stream.Position > targetEncoded.Length)
                                        {
                                            stream.Position = 0;
                                            stream.SetLength(targetEncoded.Length);
                                            stream.Write(targetEncoded, 0, targetEncoded.Length);
                                            return;
                                        }
                                    }

                                    ++blockNumber;
                                }
                            }
                        }
                    }

                    compressed.Flush();
                }

                if (stream.Position > targetEncoded.Length)
                {
                    stream.Position = 0;
                    stream.SetLength(targetEncoded.Length);
                    stream.Write(targetEncoded, 0, targetEncoded.Length);
                }
            }
            finally
            {
                if (sbd != null)
                {
                    source.UnlockBits(sbd);
                }

                if (tbd != null)
                {
                    target.UnlockBits(tbd);
                }
            }
        }
Ejemplo n.º 20
0
        //wykorzystujemy strumień pliku w celu przeprowadzenia operacji odczytu, zapisu i wyszukiwania
        static void Main(string[] args)
        {
            // utworzenie w katalogu bieżącym pliku o nazwie test.txt
            using (Stream s = new FileStream("test.txt", FileMode.Create))
            {
                Console.WriteLine(s.CanRead);  // prawda
                Console.WriteLine(s.CanWrite); // prawda
                Console.WriteLine(s.CanSeek);  // prawda
                s.WriteByte(101);
                s.WriteByte(102);
                byte[] block = { 1, 2, 3, 4, 5 };
                s.Write(block, 0, block.Length);                   // zapis bloku 5 bajtów
                Console.WriteLine(s.Length);                       // 7
                Console.WriteLine(s.Position);                     // 7
                s.Position = 0;                                    // przejście z powrotem na początek
                Console.WriteLine(s.ReadByte());                   // 101
                Console.WriteLine(s.ReadByte());                   // 102
                                                                   // odczyt danych ze strumienia i umieszczenie ich w bloku tablicy
                Console.WriteLine(s.Read(block, 0, block.Length)); // 5
                                                                   // przyjmując założenie, że ostatni odczyt zwrócił wartość 5, znajdujemy się
                                                                   // na końcu pliku i dlatego teraz wywołanie Read() zwróci wartość 0
                Console.WriteLine(s.Read(block, 0, block.Length)); // 0
            }

            //to samo ale Async
            Console.WriteLine("Async:");
            AsyncDemo();

            //StreamReader i StreamWriter
            //StreamWriter zapisuje dwa wiersze tekstu w pliku, a następnie egzemplarz StreamReader odczytuje tekst zapisany w pliku
            using (FileStream fss = File.Create("testy.txt"))
                using (TextWriter writer = new StreamWriter(fss))
                {
                    writer.WriteLine("Wiersz1");
                    writer.WriteLine("Wiersz2");
                }
            using (FileStream fs = File.OpenRead("testy.txt"))
                using (TextReader reader = new StreamReader(fs))
                {
                    Console.WriteLine(reader.ReadLine()); // Wiersz1
                    Console.WriteLine(reader.ReadLine()); // Wiersz2
                }
            using (TextWriter writer = File.AppendText("testt.txt"))
                writer.WriteLine("Wiersz3");
            using (TextReader reader = File.OpenText("testt.txt"))
                while (reader.Peek() > -1)
                {
                    Console.WriteLine(reader.ReadLine());
                }                                             // Wiersz1
                                                              // Wiersz2
                                                              // Wiersz3
            using (TextWriter w = File.CreateText("but.txt")) // użycie domyślnego
                w.WriteLine("but—");                          // kodowania znaków UTF-8
            using (Stream s = File.OpenRead("but.txt"))
                for (int b; (b = s.ReadByte()) > -1;)
                {
                    Console.WriteLine(b);
                }
            //kompresja strumienia
            using (Stream s = File.Create("compressed.bin"))
                using (Stream ds = new DeflateStream(s, CompressionMode.Compress))
                    for (byte i = 0; i < 100; i++)
                    {
                        ds.WriteByte(i);
                    }
            using (Stream s = File.OpenRead("compressed.bin"))
                using (Stream ds = new DeflateStream(s, CompressionMode.Decompress))
                    for (byte i = 0; i < 100; i++)
                    {
                        Console.WriteLine(ds.ReadByte());
                    }                     // zapis od 0 do 99
            byte[] data = new byte[1000]; // W przypadku pustej tablicy można się spodziewać

            //kompresja w pamięci
            // uzyskania dobrego współczynnika kompresji!
            var ms = new MemoryStream();

            using (Stream ds = new DeflateStream(ms, CompressionMode.Compress))
                ds.Write(data, 0, data.Length);
            byte[] compressed = ms.ToArray();
            Console.WriteLine(compressed.Length); // 11
                                                  // dekompresja na postać tablicy danych
            ms = new MemoryStream(compressed);
            using (Stream ds = new DeflateStream(ms, CompressionMode.Decompress))
                for (int i = 0; i < 1000; i += ds.Read(data, i, 1000 - i))
                {
                    ;
                }
        }
Ejemplo n.º 21
0
 public void WriteByte(byte value)
 {
     DeflateStreamInstance.WriteByte(value);
 }
Ejemplo n.º 22
0
 public void WriteByte(byte b)
 {
     deflate.WriteByte(b);
     checksum_a = (checksum_a + b) % ADLER_MOD;
     checksum_b = (checksum_b + checksum_a) % ADLER_MOD;
 }