示例#1
0
        private static string HashFile(HashAlgorithm hash, string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (RetryFile.Exists(filePath) == false)
            {
                throw new FileNotFoundException("文件不存在:" + filePath);
            }


            using (FileStream fs = RetryFile.OpenRead(filePath)) {
                byte[] buffer = hash.ComputeHash(fs);
                return(buffer.ToHexString());
            }
        }
示例#2
0
        public static void Compress(List <Tuple <string, object> > files, string zipPath)
        {
            if (files == null)
            {
                throw new ArgumentNullException(nameof(files));
            }
            if (string.IsNullOrEmpty(zipPath))
            {
                throw new ArgumentNullException(nameof(zipPath));
            }


            RetryFile.Delete(zipPath);


            using (FileStream file = RetryFile.Create(zipPath)) {
                using (ZipArchive zip = new ZipArchive(file, ZipArchiveMode.Create, true, Encoding.UTF8)) {
                    foreach (Tuple <string, object> tuple in files)
                    {
                        var entry = zip.CreateEntry(tuple.Item1, CompressionLevel.Optimal);

                        Type dataType = tuple.Item2.GetType();

                        if (dataType == typeof(string))
                        {
                            using (var stream = entry.Open()) {
                                using (FileStream fs = RetryFile.OpenRead((string)tuple.Item2)) {
                                    fs.CopyTo(stream);
                                }
                            }
                        }
                        else if (dataType == typeof(byte[]))
                        {
                            using (BinaryWriter writer = new BinaryWriter(entry.Open())) {
                                writer.Write((byte[])tuple.Item2);
                            }
                        }
                        else
                        {
                            // 暂且忽略错误的参数吧。
                        }
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// 快速计算文件哈希值(只计算文件开头部分)
        /// </summary>
        /// <param name="filePath">要计算哈希值的文件路径</param>
        /// <param name="headerLength">读取开头多长的部分,默认值:2M</param>
        /// <returns></returns>
        public static string FastHash(string filePath, int headerLength = 2 * 1024 * 1024)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (RetryFile.Exists(filePath) == false)
            {
                throw new FileNotFoundException("文件不存在:" + filePath);
            }

            if (headerLength <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(headerLength));
            }


            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) {
                using (FileStream file = RetryFile.OpenRead(filePath)) {
                    if (headerLength > file.Length)
                    {
                        headerLength = (int)file.Length;
                    }

                    // 将文件长度转成字节数组
                    byte[] intBytes = BitConverter.GetBytes(file.Length);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(intBytes);
                    }

                    // 申请字节缓冲区
                    byte[] buffer = new byte[headerLength + intBytes.Length];
                    Array.Copy(intBytes, buffer, intBytes.Length);

                    // 读取文件的开头部分
                    file.Read(buffer, intBytes.Length, headerLength);

                    // 计算缓冲区的哈希值
                    byte[] result = md5.ComputeHash(buffer);
                    return(BitConverter.ToString(result).Replace("-", ""));
                }
            }
        }