zoukankan      html  css  js  c++  java
  • C#大文件流式压缩加解密

            public static async Task ZipAsync(string[] srcFiles,string writeFile,int size= 1024 * 1024 * 32, CancellationToken token=default)
            {
                try
                {
                    FileStream zipStream = new FileStream(writeFile, FileMode.OpenOrCreate);
                    using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create))
                    {
                        foreach (string srcFile in srcFiles)
                            await Entry(archive, srcFile, token);
                    }
                    zipStream.Dispose();
                    Console.WriteLine("Zip Success");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Zip Failed" + e);
                }
    
                async Task Entry(ZipArchive archive,string srcFile, CancellationToken ct)
                {
                    try
                    {
                        ZipArchiveEntry fileEntry = archive.CreateEntry(Path.GetFileName(srcFile));
                        FileStream srcStream = new FileStream(srcFile, FileMode.Open);
                        var buffer = new Memory<byte>(new byte[size]);
                        using (Stream entryStream = fileEntry.Open())
                        {
                            int len;
                            while ((len=await srcStream.ReadAsync(buffer, ct)) > 0)
                                await entryStream.WriteAsync(len != size ? buffer.Slice(0, len) : buffer,ct);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Entry Failed" + e);
                    }
                }
            }
    
    
    
            public static void Encrypt(SymmetricAlgorithm provider, string readFile, string writeFile,int size=4096)
            {
                try
                {
                    var buffer=new Span<byte>(new byte[size]);
                    FileStream fsWrite = new FileStream(writeFile, FileMode.Create);
                    FileStream fsRead = new FileStream(readFile, FileMode.Open);
                    ICryptoTransform transform = provider.CreateEncryptor(provider.Key, provider.IV);
                    CryptoStream cs = new CryptoStream(fsWrite, transform, CryptoStreamMode.Write);
                    int len;
                    while ((len = fsRead.Read(buffer)) > 0)
                        cs.Write(len != size ? buffer.Slice(0, len) : buffer);
                    fsRead.Close();
                    cs.Close();
                    fsWrite.Close();
                    Console.WriteLine("Encrypt Success");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Encrypt Failed" + e);
                }
            }
            public static void Decrypt(SymmetricAlgorithm provider, string readFile, string writeFile, int size = 4096)
            {
                try
                {
                    var buffer = new Span<byte>(new byte[size]);
                    FileStream fsRead = new FileStream(readFile, FileMode.Open);
                    FileStream fsWrite = new FileStream(writeFile, FileMode.OpenOrCreate);
                    ICryptoTransform transform = provider.CreateDecryptor(provider.Key, provider.IV);
                    CryptoStream cs = new CryptoStream(fsRead, transform, CryptoStreamMode.Read);
                    int len;
                    while ((len = cs.Read(buffer)) > 0)
                        fsWrite.Write(len != size ? buffer.Slice(0, len) : buffer);
                    fsWrite.Close();
                    cs.Close();
                    fsRead.Close();
                    Console.WriteLine("Decrypt Success");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Decrypt Failed" + e);
                }
            }
            public static async Task EncryptAsync(SymmetricAlgorithm provider, string readFile, string writeFile, int size = 4096, CancellationToken token=default)
            {
                try
                {
                    var buffer = new Memory<byte>(new byte[size]);
                    FileStream fsWrite = new FileStream(writeFile, FileMode.Create);
                    FileStream fsRead = new FileStream(readFile, FileMode.Open);
                    ICryptoTransform transform = provider.CreateEncryptor(provider.Key, provider.IV);
                    CryptoStream cs = new CryptoStream(fsWrite, transform, CryptoStreamMode.Write);
                    int len;
                    while ((len = await fsRead.ReadAsync(buffer, token)) > 0)
                        await cs.WriteAsync(len != size ? buffer.Slice(0, len) : buffer,token);
                    await fsWrite.FlushAsync(token);
                    fsRead.Close();
                    cs.Close();
                    fsWrite.Close();
                    Console.WriteLine("Encrypt Success");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Encrypt Failed" + e);
                }
            }
            public static async Task DecryptAsync(SymmetricAlgorithm provider, string readFile, string writeFile, int size = 4096, CancellationToken token = default)
            {
                try
                {
                    var buffer = new Memory<byte>(new byte[size]);
                    FileStream fsRead = new FileStream(readFile, FileMode.Open);
                    FileStream fsWrite = new FileStream(writeFile, FileMode.OpenOrCreate);
                    ICryptoTransform transform = provider.CreateDecryptor(provider.Key, provider.IV);
                    CryptoStream cs = new CryptoStream(fsRead, transform, CryptoStreamMode.Read);
                    int len;
                    while ((len = await cs.ReadAsync(buffer,token)) > 0)
                        await  fsWrite.WriteAsync(len != size ? buffer.Slice(0, len) : buffer,token);
                    await fsWrite.FlushAsync(token);
                    fsRead.Close();
                    cs.Close();
                    fsWrite.Close();
                    Console.WriteLine("Decrypt Success");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Decrypt Failed" + e);
                }
            }
  • 相关阅读:
    企业局域网的组建
    安装网吧收费系统
    网吧游戏的三层更新
    Java4Android之BlockingQueue
    阿里员工离职潮的背后?
    使用SharePoint管理中心管理服务
    获取联系人【自己定义布局文件与主布局文件相连,数据库内容查找并显示】
    读取两文件,不同的内容存入还有一个文件里
    graph driver-device mapper-01driver初始化
    .net读取异步Post的内容
  • 原文地址:https://www.cnblogs.com/yzpopulation/p/11305516.html
Copyright © 2011-2022 走看看