Ejemplo n.º 1
1
        public static int Compress(Stream input, Stream output, int uncompressedSize, Action<long> compressed = null)
        {
            Exceptions.CheckArgumentNull(input, "input");
            Exceptions.CheckArgumentNull(output, "output");
            Exceptions.CheckArgumentOutOfRangeException(uncompressedSize, "uncompressedSize", 0, int.MaxValue);

            byte[] buff = new byte[Math.Min(32 * 1024, uncompressedSize)];
            ZOutputStream writer = new ZOutputStream(output, 6);

            long position = output.Position;

            int readed;
            while (uncompressedSize > 0 && (readed = input.Read(buff, 0, Math.Min(buff.Length, uncompressedSize))) > 0)
            {
                uncompressedSize -= readed;
                writer.Write(buff, 0, readed);
                compressed.NullSafeInvoke(readed);
            }

            writer.finish();

            if (uncompressedSize != 0)
                throw new Exception("Неожиданный конец потока.");

            return (int)(output.Position - position);
        }
Ejemplo n.º 2
0
        public static void Uncompress(Stream input, Stream output, int uncompressedSize, byte[] buff, CancellationToken cancelationToken, Action<long> uncompressed = null)
        {
            Exceptions.CheckArgumentNull(input, "input");
            Exceptions.CheckArgumentNull(output, "output");
            Exceptions.CheckArgumentOutOfRangeException(uncompressedSize, "uncompressedSize", 0, int.MaxValue);

            ZInputStream reader = new ZInputStream(input);

            int readed;
            while (uncompressedSize > 0 && (readed = reader.read(buff, 0, Math.Min(buff.Length, uncompressedSize))) > 0)
            {
                if (cancelationToken.IsCancellationRequested)
                    return;

                uncompressedSize -= readed;
                output.Write(buff, 0, readed);
                uncompressed.NullSafeInvoke(readed);
            }

            if (uncompressedSize != 0)
                throw new Exception("Неожиданный конец потока.");
        }
Ejemplo n.º 3
0
        public static async Task CopyAsync(Stream input, Stream output, ManualResetEvent cancelEvent, Action<long> progress)
        {
            byte[] buff = new byte[32 * 1024];

            int read;
            while ((read = await input.ReadAsync(buff, 0, buff.Length)) != 0)
            {
                if (cancelEvent.WaitOne(0))
                    return;

                await output.WriteAsync(buff, 0, read);
                progress.NullSafeInvoke(read);
            }
        }