• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#多文件加密压缩与解压还原

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、多文件加密压缩

    public class PackageEntity
    {
        public string PatchName { get; set; }
        public string PatchUrl { get; set; }
        public string FullPath { get; set; }
    }
    public List<PackageEntity>GetAllFiles(string filePath)
    {
        var list = new List<PackageEntity>();
        DirectoryInfo dir = new DirectoryInfo(@"C");
        var dirInfo = dir.GetDirectories();
        var filesInfo = dir.GetFiles();
        foreach (var item in filesInfo)
        {
            list.Add(new PackageEntity { PatchName = item.Name, PatchUrl = item.FullName, FullPath = item.FullName });
        }
  
        return list;
    }
 //计算总条数
            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();

 2、压缩包解压以及文件解密

    public void DeCompressMuti(string zipFileName,string targetPath)
        {
            byte[] fileSize = new byte[4];
            if (!File.Exists(zipFileName)) return;
            using (FileStream fstream = File.Open(zipFileName, FileMode.Open))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (GZipStream zipStream = new GZipStream(fstream, CompressionMode.Decompress))
                    {
                        zipStream.CopyTo(ms);
                    }
                    ms.Position = 0;
                    //获取总条数
                    ms.Read(fileSize, 0, fileSize.Length);
                    int fileCountLength = BitConverter.ToInt32(fileSize, 0);
                    byte[] fileCountBytes = new byte[fileCountLength];
                    ms.Read(fileCountBytes, 0, fileCountBytes.Length);
                    string fileCount = Encoding.UTF8.GetString(fileCountBytes);
                    while(ms.Position==ms.Length)
                    {
                        ms.Read(fileSize, 0, fileSize.Length);
                        int fileNameLength = BitConverter.ToInt32(fileSize, 0);
                        byte[] fileNameBytes = new byte[fileNameLength];
                        ms.Read(fileNameBytes, 0, fileNameBytes.Length);
                        //数据解密
                        fileNameBytes = RSAHelper.RsaDecrypt(fileNameBytes, key);
                        sting fileName = Encoding.UTF8.GetString(fileNameBytes);
                        string fileFullName = targetPath + fileName;
                        //获取文件
                        ms.Read(fileSize, 0, 4);
                        int fileContentLength = BitConverter.ToInt32(fileSize, 0);
                        byte[] fileContentBytes = new byte[fileContentLength];
                        ms.Read(fileContentBytes, 0, fileCountBytes.Length);
                        using (FileStream ff = File.Create(fileFullName))
                        {
                            ff.Write(fileCountBytes, 0, fileContentBytes.Length);
                        }
                    }

                }
            }


        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#模拟登陆并继续访问其他页面发布时间:2022-07-13
下一篇:
CLRviaC#读书笔记5-2为什么不使用Finalize发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap