zoukankan      html  css  js  c++  java
  • 文件转二进制流拆分与组装


    //上传文件转二进制流
    string fileFullPath = Path.Combine(UploadRoot, filePath);
    using (FileStream fs = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
    using (BinaryReader FileReader = new BinaryReader(fs))
    {
    byte[] Bytes;
    var file_size = 8192;
    int fileCount = Convert.ToInt32(Math.Ceiling((double)fs.Length / file_size));
    for (int i = 0; i < fileCount; i++)
    {
    Content Content = new Content();
    Bytes = FileReader.ReadBytes(file_size);
    Content.Id = Guid.NewGuid();
    Content.F_Content = Compress(Bytes);
    Content.F_OrderIndex = i;
    Content.CreateTime = DateTime.Now;
    _iContentAppService.Add(Content, null);
    }
    }
    }

    //组二进制流
    public byte[] GetContent(Guid id)
    {
    List<byte> byteSource = new List<byte>();
    try
    {
    var bAFJContents = bAFJContentAppService.FindByFilter(t => t.F_BAFJ_ID == id, null, null, f => new { f.Id });
    for (int pageNumber = 1; pageNumber <= bAFJContents.Count() / PageSize + 1; pageNumber++)
    {
    var BAFJContent = bAFJContentAppService.FindPagedByFilter(specification.SatisfiedBy(), null, pageNumber, PageSize, order, null);
    foreach (var item in BAFJContent)
    {
    byteSource.AddRange(Depress(item.F_Content));
    }
    }
    }
    catch (Exception ex)
    {
    LoggerFactory.CreateLog().Error(ex.ToString());
    }
    return byteSource.ToArray();
    }


    /// <summary>
    /// 压缩二进制流
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] Compress(byte[] data)
    {
    using (var memoryStream = new MemoryStream())
    {
    using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Compress))
    {
    compressionStream.Write(data, 0, data.Length);
    compressionStream.Flush();
    }
    return memoryStream.ToArray();
    }
    }

    /// <summary>
    /// 解压二进制流
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] Depress(byte[] data)
    {
    using (var memoryStream = new MemoryStream(data))
    using (var outStream = new MemoryStream())
    {
    using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Decompress))
    {
    compressionStream.CopyTo(outStream);
    compressionStream.Flush();
    }
    return outStream.ToArray();
    }
    }

  • 相关阅读:
    一个总成本花费100W的失败项目的小小反省
    从绝望中寻找希望
    一个合格程序员该做的事情——你做好了吗?
    Go to 北京!
    深入浅出Oracle分析函数
    最适合web开发人员使用的速查表
    采用axis2c进行webservice发布的流程4在生成的代码中添加事务处理逻辑
    采用axis2c进行webservice发布的流程1
    rhel6.0及centos6.0 忘记root密码解决办法
    采用axis2c进行webservice发布的流程3通过wsdl文件,自动生成C的服务端代码
  • 原文地址:https://www.cnblogs.com/chengzi00/p/14448003.html
Copyright © 2011-2022 走看看