1. 程式人生 > 實用技巧 >C# 多檔案加密壓縮與解壓還原

C# 多檔案加密壓縮與解壓還原

1、多檔案加密壓縮

 //計算總條數
            byte[] fileCountBytes = Encoding.UTF8.GetBytes(list.Count.ToString());
            byte[] countSizeBytes = BitConverter.GetBytes(fileCountBytes.Length);
            ms.Write(countSizeBytes, 0, countSizeBytes.Length);
            ms.Write(fileCountBytes, 0, fileCountBytes.Length);
            
foreach (var item in list) { if (!File.Exists(item.FullPath)) continue; byte[] fileNameBytes = Encoding.UTF8.GetBytes(item.PathUrl); //rsa加密 fileNameBytes = RSAHelper.RsaEncrpyt(fileNameBytes, key); byte[]sizeBytes = BitConverter.GetBytes(fileNameBytes.Length); ms.Write(sizeBytes,
0, sizeBytes.Length); ms.Write(fileNameBytes, 0, fileNameBytes.Length); byte[] fileContentBytes = File.ReadAllBytes(item.FullPath); ms.Write(BitConverter.GetBytes(fileContentBytes.Length), 0, 4); ms.Write(fileContentBytes, 0, fileContentBytes.Length); } ms.Flush(); ms.Position
= 0; using (FileStream fs = File.Create(saveFullPath)) { using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress)) { ms.CopyTo(zipStream); } } ms.Close();