Пример #1
0
        private static ulong GetCompressedLength(IEnumerable <byte[]> fileChunks)
        {
            ulong result = 0;

            try
            {
                using (MemoryStream outStream = new MemoryStream())
                {
                    using (GZipStream gzip = new GZipStream(outStream, CompressionMode.Compress))
                    {
                        using (MemoryStream inStream = new MemoryStream(Int16.MaxValue))
                        {
                            foreach (byte[] chunk in fileChunks)
                            {
                                inStream.Write(chunk, 0, chunk.Length);
                            }

                            Task copyTask = inStream.CopyToAsync(gzip, chunkSize, CancellationHelper.GetCancellationToken());
                            copyTask.Wait(CancellationHelper.GetCancellationToken());
                            result = (ulong)outStream.Length;
                        }
                    }
                }
            }
            catch
            { }

            return(result);
        }
Пример #2
0
        private static string GetHashFromByteStream <HASHER>(Stream fileStream) where HASHER : HashAlgorithm, new()
        {
            string result = string.Empty;

            try
            {
                using (HASHER hashAlgorithm = new HASHER())
                {
                    long   bytesToHash = fileStream.Length;
                    byte[] buffer      = new byte[bigChunkSize];
                    int    sizeToRead  = buffer.Length;
                    while (bytesToHash > 0)
                    {
                        if (bytesToHash < (long)sizeToRead)
                        {
                            sizeToRead = (int)bytesToHash;
                        }

                        int bytesRead = fileStream.ReadAsync(buffer, 0, sizeToRead, CancellationHelper.GetCancellationToken()).Result;
                        CancellationHelper.ThrowIfCancelled();
                        hashAlgorithm.TransformBlock(buffer, 0, bytesRead, null, 0);
                        bytesToHash -= (long)bytesRead;
                        if (bytesRead == 0)
                        {
                            throw new InvalidOperationException("Unexpected end of stream");                             // or break;
                        }
                    }
                    hashAlgorithm.TransformFinalBlock(buffer, 0, 0);
                    buffer = null;
                    result = ByteArrayConverter.ToHexString(hashAlgorithm.Hash);
                }
            }
            catch { }

            return(result);
        }
Пример #3
0
        private static ulong GetCompressedLength(byte[] fileBytes)
        {
            ulong result = 0;

            try
            {
                using (MemoryStream outStream = new MemoryStream())
                {
                    using (GZipStream gzip = new GZipStream(outStream, CompressionMode.Compress))
                    {
                        using (MemoryStream inStream = new MemoryStream(fileBytes))
                        {
                            Task copyTask = inStream.CopyToAsync(gzip, chunkSize, CancellationHelper.GetCancellationToken());
                            copyTask.Wait(CancellationHelper.GetCancellationToken());
                            result = (ulong)outStream.Length;
                        }
                    }
                }
            }
            catch
            { }

            return(result);
        }